Search in sources :

Example 1 with GHPullRequestReview

use of org.kohsuke.github.GHPullRequestReview in project pull-request-workflow by avano.

the class GHClient method getReviews.

/**
 * Gets the reviews for given PR. User can review multiple times, so for each user it returns the latest review.
 *
 * @param pr pull request instance
 * @return map with the user login as key and last review state as value
 */
public Map<GHUser, GHPullRequestReviewState> getReviews(GHPullRequest pr) {
    LOG.trace("PR #{}: Listing reviews", pr.getNumber());
    Map<GHUser, GHPullRequestReviewState> response = new HashMap<>();
    try {
        for (GHPullRequestReview review : pr.listReviews().toList()) {
            response.put(review.getUser(), review.getState());
        }
    } catch (IOException e) {
        LOG.error("PR #{}: Unable to get user from review", pr.getNumber(), e);
    }
    return response;
}
Also used : GHPullRequestReviewState(org.kohsuke.github.GHPullRequestReviewState) HashMap(java.util.HashMap) GHPullRequestReview(org.kohsuke.github.GHPullRequestReview) GHUser(org.kohsuke.github.GHUser) IOException(java.io.IOException)

Example 2 with GHPullRequestReview

use of org.kohsuke.github.GHPullRequestReview 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 3 with GHPullRequestReview

use of org.kohsuke.github.GHPullRequestReview in project pull-request-workflow by avano.

the class ReviewTest method shouldSendReviewSubmittedMessageTest.

@Test
public void shouldSendReviewSubmittedMessageTest() {
    review.handleReviewEvent(jsonBody("reviewSubmitted.json"));
    waitForInvocations(2);
    assertThat(lastDestination()).isEqualTo(Constants.PR_REVIEW_SUBMITTED);
    BusMessage msg = lastMessage();
    assertThat(msg).isNotNull();
    GHPullRequestReview review = msg.get(GHPullRequestReview.class);
    assertThat(review.getState()).isEqualTo(GHPullRequestReviewState.COMMENTED);
    assertThat(msg.getSender().getLogin()).isEqualTo("Codertocat-submitted");
    GHPullRequest pr = msg.get(BusMessage.INFO_PR_KEY, GHPullRequest.class);
    assertThat(pr.getNumber()).isEqualTo(2);
}
Also used : GHPullRequest(org.kohsuke.github.GHPullRequest) BusMessage(com.github.avano.pr.workflow.message.BusMessage) GHPullRequestReview(org.kohsuke.github.GHPullRequestReview) Test(org.junit.jupiter.api.Test) QuarkusTest(io.quarkus.test.junit.QuarkusTest)

Aggregations

GHPullRequestReview (org.kohsuke.github.GHPullRequestReview)3 BusMessage (com.github.avano.pr.workflow.message.BusMessage)2 GHPullRequest (org.kohsuke.github.GHPullRequest)2 GHUser (org.kohsuke.github.GHUser)2 GHClient (com.github.avano.pr.workflow.gh.GHClient)1 Log (com.github.avano.pr.workflow.handler.interceptor.Log)1 CheckRunMessage (com.github.avano.pr.workflow.message.CheckRunMessage)1 LabelsMessage (com.github.avano.pr.workflow.message.LabelsMessage)1 QuarkusTest (io.quarkus.test.junit.QuarkusTest)1 ConsumeEvent (io.quarkus.vertx.ConsumeEvent)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Test (org.junit.jupiter.api.Test)1 GHPullRequestReviewState (org.kohsuke.github.GHPullRequestReviewState)1