Search in sources :

Example 1 with RtPagination

use of com.jcabi.github.RtPagination in project wring by yegor256.

the class AgGithub method push.

@Override
public String push(final Events events) throws IOException {
    final Github github = new RtGithub(this.config.getString("token"));
    final String since = DateFormatUtils.formatUTC(DateUtils.addMinutes(new Date(), -Tv.THREE), "yyyy-MM-dd'T'HH:mm:ss'Z'");
    final Request req = github.entry().uri().path("/notifications").back();
    final Iterable<JsonObject> list = new RtPagination<>(req.uri().queryParam("participating", "true").queryParam("since", since).queryParam("all", Boolean.toString(true)).back(), RtPagination.COPYING);
    final Collection<String> done = new LinkedList<>();
    for (final JsonObject event : AgGithub.safe(list)) {
        final String reason = event.getString("reason");
        if (!"mention".equals(reason)) {
            continue;
        }
        this.push(github, event, events);
        done.add(event.getString("id"));
    }
    req.uri().queryParam("last_read_at", since).back().method(Request.PUT).body().set("{}").back().fetch().as(RestResponse.class).assertStatus(HttpURLConnection.HTTP_RESET);
    if (!done.isEmpty()) {
        Logger.info(this, "%d GitHub events for @%s processed: %s", done.size(), github.users().self().login(), done);
    }
    return String.format("%d events for @%s at %s", done.size(), github.users().self().login(), DateFormatUtils.formatUTC(new Date(), "yyyy-MM-dd HH:mm:ss"));
}
Also used : RtGithub(com.jcabi.github.RtGithub) Github(com.jcabi.github.Github) RestResponse(com.jcabi.http.response.RestResponse) Request(com.jcabi.http.Request) JsonObject(javax.json.JsonObject) RtGithub(com.jcabi.github.RtGithub) Date(java.util.Date) LinkedList(java.util.LinkedList) RtPagination(com.jcabi.github.RtPagination)

Example 2 with RtPagination

use of com.jcabi.github.RtPagination in project wring by yegor256.

the class BoCommit method text.

/**
 * Collect all important texts from the issue.
 * @param coords Coords
 * @return Body text
 * @throws IOException If fails
 * @checkstyle ExecutableStatementCountCheck (100 lines)
 */
@SuppressWarnings("PMD.AvoidInstantiatingObjectsInLoops")
public String text(final Coordinates coords) throws IOException {
    final Iterator<JsonObject> comments = new RtPagination<>(this.commit.repo().github().entry().uri().path("repos").path(coords.user()).path(coords.repo()).path("commits").path(this.commit.sha()).path("comments").back(), object -> object).iterator();
    final String self = this.commit.repo().github().users().self().login();
    int seen = this.seen();
    Logger.info(this, "Last seen comment in %s is #%d", this.commit.sha(), seen);
    final Pattern ptn = Pattern.compile(String.format(".*(?<![a-zA -Z0-9-])%s(?![a-zA-Z0-9-]).*", Pattern.quote(String.format("@%s", self))), Pattern.CASE_INSENSITIVE | Pattern.DOTALL | Pattern.MULTILINE);
    final StringBuilder body = new StringBuilder();
    while (comments.hasNext()) {
        final JsonObject comment = comments.next();
        final int number = comment.getInt("id");
        if (number <= seen) {
            continue;
        }
        final String author = comment.getJsonObject("user").getString("login");
        if (author.equals(self)) {
            Logger.info(this, "%s/%d ignored since you're the author", this.commit.sha(), number);
            continue;
        }
        final String cmt = comment.getString("body");
        if (ptn.matcher(cmt).matches()) {
            body.append('@').append(author).append(" at [").append(comment.getString("updated_at")).append("](").append(comment.getString("html_url")).append("): ").append(StringEscapeUtils.escapeHtml4(cmt)).append("\n\n");
            Logger.info(this, "%s/%d accepted: %s", this.commit.sha(), number, new Printable(cmt));
        } else {
            Logger.info(this, "%s/%d ignored: %s", this.commit.sha(), number, new Printable(cmt));
        }
        seen = number;
    }
    this.base.vault().save(this.key(), Optional.of(Integer.toString(seen)));
    Logger.info(this, "Seen comment set to %d for %s", seen, this.commit.sha());
    return body.toString();
}
Also used : JsonObject(javax.json.JsonObject) Iterator(java.util.Iterator) Events(io.wring.model.Events) Printable(io.wring.agents.Printable) IOException(java.io.IOException) StringEscapeUtils(org.apache.commons.text.StringEscapeUtils) RtPagination(com.jcabi.github.RtPagination) Base(io.wring.model.Base) RepoCommit(com.jcabi.github.RepoCommit) Coordinates(com.jcabi.github.Coordinates) Optional(java.util.Optional) Logger(com.jcabi.log.Logger) Pattern(java.util.regex.Pattern) Pattern(java.util.regex.Pattern) JsonObject(javax.json.JsonObject) Printable(io.wring.agents.Printable)

Aggregations

RtPagination (com.jcabi.github.RtPagination)2 JsonObject (javax.json.JsonObject)2 Coordinates (com.jcabi.github.Coordinates)1 Github (com.jcabi.github.Github)1 RepoCommit (com.jcabi.github.RepoCommit)1 RtGithub (com.jcabi.github.RtGithub)1 Request (com.jcabi.http.Request)1 RestResponse (com.jcabi.http.response.RestResponse)1 Logger (com.jcabi.log.Logger)1 Printable (io.wring.agents.Printable)1 Base (io.wring.model.Base)1 Events (io.wring.model.Events)1 IOException (java.io.IOException)1 Date (java.util.Date)1 Iterator (java.util.Iterator)1 LinkedList (java.util.LinkedList)1 Optional (java.util.Optional)1 Pattern (java.util.regex.Pattern)1 StringEscapeUtils (org.apache.commons.text.StringEscapeUtils)1