Search in sources :

Example 1 with GHClient

use of com.github.avano.pr.workflow.gh.GHClient in project pull-request-workflow by avano.

the class ConflictHandler method checkForConflict.

/**
 * Checks if a PR merge caused conflict in other opened PRs, if so a comment is added to each open PR and it's assigned back to the author to fix.
 *
 * @param msg {@link BusMessage} instance
 */
@Log
@ConsumeEvent(Constants.PR_CHECK_CONFLICT)
public void checkForConflict(BusMessage msg) {
    GHClient client = msg.client();
    ConflictMessage cm = msg.get(ConflictMessage.class);
    if (client.getRepositoryConfiguration().conflictMessage() == null || client.getRepositoryConfiguration().conflictMessage().isEmpty()) {
        LOG.debug("Skipping conflict detection - conflict message not set");
        return;
    }
    for (GHPullRequest openPullRequest : cm.getOpenPullRequests()) {
        try {
            openPullRequest.refresh();
            if ("dirty".equals(openPullRequest.getMergeableState())) {
                LOG.info("PR #{}: Caused conflict in PR #{}", cm.getMergedPrId(), openPullRequest.getNumber());
                client.postComment(openPullRequest, client.getRepositoryConfiguration().conflictMessage().replace("<ID>", cm.getMergedPrId() + ""));
                client.assignToAuthor(openPullRequest);
                List<String> removeLabels = new ArrayList<>();
                removeLabels.addAll(client.getRepositoryConfiguration().approvedLabels());
                removeLabels.addAll(client.getRepositoryConfiguration().reviewRequestedLabels());
                eventBus.publish(Constants.EDIT_LABELS, new BusMessage(client, new LabelsMessage(openPullRequest, client.getRepositoryConfiguration().changesRequestedLabels(), removeLabels)));
            } else {
                LOG.trace("PR #{}: No conflict in PR #{}", cm.getMergedPrId(), openPullRequest.getNumber());
            }
        } catch (IOException e) {
            LOG.error("PR #{}: Unable to process PR", openPullRequest.getNumber(), e);
        }
    }
}
Also used : GHPullRequest(org.kohsuke.github.GHPullRequest) GHClient(com.github.avano.pr.workflow.gh.GHClient) BusMessage(com.github.avano.pr.workflow.message.BusMessage) ArrayList(java.util.ArrayList) IOException(java.io.IOException) ConflictMessage(com.github.avano.pr.workflow.message.ConflictMessage) LabelsMessage(com.github.avano.pr.workflow.message.LabelsMessage) Log(com.github.avano.pr.workflow.handler.interceptor.Log) ConsumeEvent(io.quarkus.vertx.ConsumeEvent)

Example 2 with GHClient

use of com.github.avano.pr.workflow.gh.GHClient in project pull-request-workflow by avano.

the class LifecycleHandler method handlePrUpdated.

/**
 * Handles the
 * <a href="https://docs.github.com/en/developers/webhooks-and-events/webhooks/webhook-events-and-payloads#pull_request">pull request</a>
 * synchronized event.
 * <p>
 * When the PR is updated (new commits are pushed to the PR), it dismisses all reviews, clears internal tracking and set the labels and
 * assignees to the correct state.
 * <p>
 * All users with a review on the PR will be assigned to the PR and re-requested for review.
 *
 * @param msg {@link BusMessage} instance
 */
@Log
@ConsumeEvent(Constants.PR_UPDATED)
public void handlePrUpdated(BusMessage msg) {
    GHClient client = msg.client();
    GHPullRequest pr = msg.get(GHPullRequest.class);
    LOG.info("PR #{}: Pull request updated - dismissing all reviews", pr.getNumber());
    // Dismiss all approved/changes requested reviews, since the PR was updated
    try {
        pr.listReviews().toList().stream().filter(r -> r.getState() == GHPullRequestReviewState.APPROVED || r.getState() == GHPullRequestReviewState.CHANGES_REQUESTED).forEach(r -> {
            try {
                r.dismiss(client.getRepositoryConfiguration().reviewDismissMessage());
            } catch (IOException e) {
                LOG.error("PR #{}: Unable to dismiss review: " + e, pr.getNumber());
            }
        });
    } catch (IOException e) {
        LOG.error("PR #{}: Unable to list all pull request reviews: " + e, pr.getNumber());
    }
    // Re-apply labels to current state
    List<String> addLabels = new ArrayList<>();
    List<String> removeLabels = new ArrayList<>();
    Map<GHUser, GHPullRequestReviewState> reviews = client.getReviews(pr);
    if (!reviews.isEmpty()) {
        // Request review from all previous reviewers (except for the author of the PR - if he responds to some comment, it is counted as
        // "commented" review)
        // Set the assignees back to requested reviewers
        // Don't add "review requested labels", as that will be done by the "review requested" event
        List<GHUser> reviewers = reviews.keySet().stream().filter(u -> !u.equals(client.getAuthor(pr))).collect(Collectors.toList());
        client.requestReviewers(pr, reviewers);
        client.setAssignees(pr, reviewers);
    }
    removeLabels.addAll(client.getRepositoryConfiguration().changesRequestedLabels());
    removeLabels.addAll(client.getRepositoryConfiguration().approvedLabels());
    removeLabels.addAll(client.getRepositoryConfiguration().commentedLabels());
    eventBus.publish(Constants.EDIT_LABELS, new BusMessage(client, new LabelsMessage(pr, addLabels, removeLabels)));
}
Also used : BaseHandler(com.github.avano.pr.workflow.handler.base.BaseHandler) GHPullRequest(org.kohsuke.github.GHPullRequest) GHPullRequestReviewState(org.kohsuke.github.GHPullRequestReviewState) IOException(java.io.IOException) ConsumeEvent(io.quarkus.vertx.ConsumeEvent) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) Constants(com.github.avano.pr.workflow.config.Constants) List(java.util.List) GHUser(org.kohsuke.github.GHUser) GHClient(com.github.avano.pr.workflow.gh.GHClient) LabelsMessage(com.github.avano.pr.workflow.message.LabelsMessage) Map(java.util.Map) BusMessage(com.github.avano.pr.workflow.message.BusMessage) Log(com.github.avano.pr.workflow.handler.interceptor.Log) GHPullRequestReviewState(org.kohsuke.github.GHPullRequestReviewState) GHClient(com.github.avano.pr.workflow.gh.GHClient) BusMessage(com.github.avano.pr.workflow.message.BusMessage) ArrayList(java.util.ArrayList) IOException(java.io.IOException) GHUser(org.kohsuke.github.GHUser) LabelsMessage(com.github.avano.pr.workflow.message.LabelsMessage) GHPullRequest(org.kohsuke.github.GHPullRequest) Log(com.github.avano.pr.workflow.handler.interceptor.Log) ConsumeEvent(io.quarkus.vertx.ConsumeEvent)

Example 3 with GHClient

use of com.github.avano.pr.workflow.gh.GHClient in project pull-request-workflow by avano.

the class ReviewSubmittedHandler method handleReview.

/**
 * Handles the
 * <a href="https://docs.github.com/en/developers/webhooks-and-events/webhooks/webhook-events-and-payloads#pull_request_review">pull request review</a> submitted event.
 * <p>
 * When the user provided a review, he is removed from the assignee list.
 * <p>
 * When the PR is approved and there are no changes-requested reviews from other reviewers, approved label is added to the PR and it is tried
 * to merge the PR, otherwise the approved review is ignored.
 * <p>
 * When changes are requested, the PR is assigned back to author and corresponding label is added and approved label is removed.
 * <p>
 * When the PR is commented (not only PR's "comment only" action, but also all conversation), commented label is applied and if the author of
 * the comment is in the assignees list, he is removed (as he provided a "review")
 *
 * @param msg {@link BusMessage} instance
 */
@Log
@ConsumeEvent(Constants.PR_REVIEW_SUBMITTED)
public void handleReview(BusMessage msg) {
    GHClient client = msg.client();
    GHPullRequestReview review = msg.get(GHPullRequestReview.class);
    GHPullRequest pr = msg.get(BusMessage.INFO_PR_KEY, GHPullRequest.class);
    List<String> addLabels = new ArrayList<>();
    List<String> removeLabels = new ArrayList<>();
    switch(review.getState()) {
        case APPROVED:
            LOG.info("PR #{}: Approved by {}", pr.getNumber(), msg.getSender().getLogin());
            List<GHUser> assignees = new ArrayList<>(pr.getAssignees());
            if (assignees.contains(msg.getSender())) {
                assignees.remove(msg.getSender());
                LOG.info("PR #{}: Removing {} from assignees", pr.getNumber(), msg.getSender().getLogin());
                client.setAssignees(pr, assignees);
            } else {
                LOG.debug("PR #{}: {} wasn't in assignees list and provided a review", pr.getNumber(), msg.getSender().getLogin());
            }
            // Only add approved label if there are no "changes required" reviews left
            if (!client.changesRequested(pr)) {
                addLabels.addAll(client.getRepositoryConfiguration().approvedLabels());
                removeLabels.addAll(client.getRepositoryConfiguration().changesRequestedLabels());
                eventBus.publish(Constants.CHECK_RUN_CREATE, new BusMessage(client, new CheckRunMessage(pr, GHCheckRun.Status.COMPLETED, GHCheckRun.Conclusion.SUCCESS)));
            }
            // Review was provided, dismiss review requested label
            removeLabels.addAll(client.getRepositoryConfiguration().reviewRequestedLabels());
            eventBus.publish(Constants.EDIT_LABELS, new BusMessage(client, new LabelsMessage(pr, addLabels, removeLabels)));
            // Don't merge here, it will be handled by the checkrun event
            break;
        case CHANGES_REQUESTED:
            LOG.info("PR #{}: Changes requested by {}", pr.getNumber(), msg.getSender().getLogin());
            client.assignToAuthor(pr);
            eventBus.publish(Constants.CHECK_RUN_CREATE, new BusMessage(client, new CheckRunMessage(pr, GHCheckRun.Status.COMPLETED, GHCheckRun.Conclusion.FAILURE)));
            removeLabels.addAll(client.getRepositoryConfiguration().approvedLabels());
            removeLabels.addAll(client.getRepositoryConfiguration().reviewRequestedLabels());
            eventBus.publish(Constants.EDIT_LABELS, new BusMessage(client, new LabelsMessage(pr, client.getRepositoryConfiguration().changesRequestedLabels(), removeLabels)));
            break;
        case COMMENTED:
            LOG.info("PR #{}: Commented by {}", pr.getNumber(), msg.getSender().getLogin());
            addLabels.addAll(client.getRepositoryConfiguration().commentedLabels());
            eventBus.publish(Constants.EDIT_LABELS, new BusMessage(client, new LabelsMessage(pr, addLabels, removeLabels)));
            break;
        default:
            LOG.info("PR #{}: Ignoring review state {}", pr.getNumber(), review.getState().name().toLowerCase());
            break;
    }
}
Also used : GHPullRequest(org.kohsuke.github.GHPullRequest) GHClient(com.github.avano.pr.workflow.gh.GHClient) BusMessage(com.github.avano.pr.workflow.message.BusMessage) CheckRunMessage(com.github.avano.pr.workflow.message.CheckRunMessage) GHPullRequestReview(org.kohsuke.github.GHPullRequestReview) ArrayList(java.util.ArrayList) GHUser(org.kohsuke.github.GHUser) LabelsMessage(com.github.avano.pr.workflow.message.LabelsMessage) Log(com.github.avano.pr.workflow.handler.interceptor.Log) ConsumeEvent(io.quarkus.vertx.ConsumeEvent)

Example 4 with GHClient

use of com.github.avano.pr.workflow.gh.GHClient in project pull-request-workflow by avano.

the class JsonEventHandler method init.

/**
 * Inits the client and if a configuration for given repository exists, parses the payload into its class.
 * @param event json event
 * @param eventClass class to parse the event to
 * @return true if successful, false otherwise
 */
protected boolean init(JsonObject event, Class<E> eventClass) {
    final GHClient client = clientProvider.get();
    final String repository = event.getJsonObject(Constants.JSON_REPOSITORY).getString(Constants.JSON_REPOSITORY_NAME);
    if (!client.init(repository)) {
        return false;
    } else {
        this.client = client;
        this.event = client.parseEvent(event, eventClass);
        if (this.event == null) {
            return false;
        }
    }
    return true;
}
Also used : GHClient(com.github.avano.pr.workflow.gh.GHClient)

Example 5 with GHClient

use of com.github.avano.pr.workflow.gh.GHClient in project pull-request-workflow by avano.

the class MergeHandler method mergePullRequest.

/**
 * Set the reviewers as assignees and merge the pull request.
 *
 * @param msg {@link BusMessage} instance
 */
private void mergePullRequest(BusMessage msg) {
    GHClient client = msg.client();
    GHPullRequest pr = msg.get(GHPullRequest.class);
    try {
        if (!Constants.DEPENDABOT_NAME.equals(client.getAuthor(pr).getLogin())) {
            // Assign the PR to all users who provided a review, so that it will be visible who was involved
            Set<GHUser> reviewers = client.getReviews(pr).keySet();
            reviewers.remove(pr.getUser());
            LOG.info("PR #{}: Setting assignees to: {}", pr.getNumber(), reviewers.stream().map(GHPerson::getLogin).collect(Collectors.joining(", ")));
            client.setAssignees(pr, reviewers);
        }
        // Save open PRs so that we can check later if merging this PR caused a conflict in some other PR
        List<GHPullRequest> mergeableOpenPullRequests = client.listOpenPullRequests().stream().filter(pullRequest -> {
            try {
                // Filter out this PR and all that are not mergeable
                return pr.getNumber() != pullRequest.getNumber() && pullRequest.getMergeable() != null && pullRequest.getMergeable();
            } catch (IOException e) {
                LOG.error("PR #{}: Unable to determine mergeable state", pullRequest.getNumber());
            }
            return false;
        }).collect(Collectors.toList());
        LOG.info("PR #{}: Merging", pr.getNumber());
        pr.merge(client.getRepositoryConfiguration().mergeMessage(), null, client.getRepositoryConfiguration().mergeMethod());
        LOG.info("PR #{}: Merged", pr.getNumber());
        if (!mergeableOpenPullRequests.isEmpty()) {
            eventBus.publish(Constants.PR_CHECK_CONFLICT, new BusMessage(client, new ConflictMessage(pr.getNumber(), mergeableOpenPullRequests)));
        }
    } catch (IOException e) {
        LOG.error("PR #{}: Unable to process merge", pr.getNumber(), e);
    }
}
Also used : BaseHandler(com.github.avano.pr.workflow.handler.base.BaseHandler) GHPullRequest(org.kohsuke.github.GHPullRequest) ConflictMessage(com.github.avano.pr.workflow.message.ConflictMessage) GHPullRequestReviewState(org.kohsuke.github.GHPullRequestReviewState) Collection(java.util.Collection) Set(java.util.Set) IOException(java.io.IOException) ConsumeEvent(io.quarkus.vertx.ConsumeEvent) Collectors(java.util.stream.Collectors) ApprovalStrategy(com.github.avano.pr.workflow.config.ApprovalStrategy) GHPerson(org.kohsuke.github.GHPerson) Constants(com.github.avano.pr.workflow.config.Constants) List(java.util.List) GHUser(org.kohsuke.github.GHUser) GHClient(com.github.avano.pr.workflow.gh.GHClient) Map(java.util.Map) BusMessage(com.github.avano.pr.workflow.message.BusMessage) GHLabel(org.kohsuke.github.GHLabel) Log(com.github.avano.pr.workflow.handler.interceptor.Log) GHPullRequest(org.kohsuke.github.GHPullRequest) GHClient(com.github.avano.pr.workflow.gh.GHClient) BusMessage(com.github.avano.pr.workflow.message.BusMessage) GHPerson(org.kohsuke.github.GHPerson) GHUser(org.kohsuke.github.GHUser) IOException(java.io.IOException) ConflictMessage(com.github.avano.pr.workflow.message.ConflictMessage)

Aggregations

GHClient (com.github.avano.pr.workflow.gh.GHClient)8 Log (com.github.avano.pr.workflow.handler.interceptor.Log)7 BusMessage (com.github.avano.pr.workflow.message.BusMessage)7 ConsumeEvent (io.quarkus.vertx.ConsumeEvent)7 GHPullRequest (org.kohsuke.github.GHPullRequest)7 LabelsMessage (com.github.avano.pr.workflow.message.LabelsMessage)5 IOException (java.io.IOException)4 GHUser (org.kohsuke.github.GHUser)4 Constants (com.github.avano.pr.workflow.config.Constants)3 BaseHandler (com.github.avano.pr.workflow.handler.base.BaseHandler)3 CheckRunMessage (com.github.avano.pr.workflow.message.CheckRunMessage)3 ConflictMessage (com.github.avano.pr.workflow.message.ConflictMessage)3 ArrayList (java.util.ArrayList)3 List (java.util.List)3 Map (java.util.Map)3 Collectors (java.util.stream.Collectors)3 GHPullRequestReviewState (org.kohsuke.github.GHPullRequestReviewState)3 ApprovalStrategy (com.github.avano.pr.workflow.config.ApprovalStrategy)2 Collection (java.util.Collection)2 Set (java.util.Set)2