com.google.gerrit.extensions.api.changes.ReviewInput Java Examples

The following examples show how to use com.google.gerrit.extensions.api.changes.ReviewInput. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ReviewInputBuilder.java    From sputnik with Apache License 2.0 5 votes vote down vote up
@NotNull
public ReviewInput toReviewInput(@NotNull Review review, @Nullable String tag) {
    ReviewInput reviewInput = new ReviewInput();
    reviewInput.message = Joiner.on(MESSAGE_SEPARATOR).join(review.getMessages());
    reviewInput.labels = new HashMap<>(review.getScores());
    if (StringUtils.isNotBlank(tag)) {
        reviewInput.tag = tag;
    }
    reviewInput.comments = review.getFiles().stream()
            .collect(Collectors.toMap(ReviewFile::getReviewFilename, this::buildFileComments));
    return reviewInput;
}
 
Example #2
Source File: ReviewInputBuilder.java    From sputnik with Apache License 2.0 5 votes vote down vote up
@NotNull
private ReviewInput.CommentInput buildCommentInput(Comment comment) {
    ReviewInput.CommentInput commentInput = new ReviewInput.CommentInput();
    commentInput.line = comment.getLine();
    commentInput.message = comment.getMessage();
    return commentInput;
}
 
Example #3
Source File: GerritFacade.java    From sputnik with Apache License 2.0 5 votes vote down vote up
@Override
public void publish(@NotNull Review review) {
    try {
        log.debug("Set review in Gerrit: {}", review);
        ReviewInput reviewInput = new ReviewInputBuilder().toReviewInput(review, gerritPatchset.getTag());
        gerritApi.changes()
                .id(gerritPatchset.getChangeId())
                .revision(gerritPatchset.getRevisionId())
                .review(reviewInput);
    } catch (Throwable e) {
        throw new GerritException("Error when setting review", e);
    }
}
 
Example #4
Source File: ReviewInputBuilderTest.java    From sputnik with Apache License 2.0 5 votes vote down vote up
@Test
void shouldBuildReviewInput() {
    Configuration config = ConfigurationBuilder.initFromResource("test.properties");
    Review review = ReviewBuilder.buildReview(config);

    ReviewInput reviewInput = reviewInputBuilder.toReviewInput(review, TAG);

    assertThat(reviewInput.message).isEqualTo("Total 8 violations found");
    assertThat(reviewInput.comments).hasSize(4);
    assertThat(reviewInput.tag).isEqualTo(TAG);
    assertThat(reviewInput.comments.get("filename1")).hasSize(2);
    assertThat(reviewInput.comments.get("filename1").get(0).message).isEqualTo("test1");
    assertThat(reviewInput.labels.get("Code-Review")).isEqualTo((short) 1);
}
 
Example #5
Source File: ReviewInputBuilderTest.java    From sputnik with Apache License 2.0 5 votes vote down vote up
@ParameterizedTest
@NullAndEmptySource
void shouldNotSetEmptyOrNullTag(String tag) {
    Configuration config = ConfigurationBuilder.initFromResource("test.properties");
    Review review = ReviewBuilder.buildReview(config);

    ReviewInput reviewInput = reviewInputBuilder.toReviewInput(review, tag);

    assertThat(reviewInput.tag).isNull();
}
 
Example #6
Source File: RevisionApiRestClientTest.java    From gerrit-rest-java-client with Apache License 2.0 5 votes vote down vote up
@Test(dataProvider = "TestCases")
public void testReview(RevisionApiTestCase testCase) throws Exception {
    GerritRestClient gerritRestClient = new GerritRestClientBuilder()
            .expectPost(
                    testCase.reviewUrl,
                    "{\"message\":\"Looks good!\",\"labels\":{\"Code-Review\":2},\"strict_labels\":true,\"omit_duplicate_comments\":false}"
            )
            .expectGetGson()
            .get();
    ChangesRestClient changesRestClient = getChangesRestClient(gerritRestClient);

    ReviewInput reviewInput = new ReviewInput();
    reviewInput.label("Code-Review", 2).message("Looks good!");

    String revision = testCase.revision;
    if (revision.equals("current")) {
        changesRestClient.id(CHANGE_ID)
                .current()
                .review(reviewInput);
    } else {
        changesRestClient.id(CHANGE_ID)
                .revision(revision)
                .review(reviewInput);
    }

    EasyMock.verify(gerritRestClient);
}
 
Example #7
Source File: GerritReviewStep.java    From gerrit-code-review-plugin with Apache License 2.0 4 votes vote down vote up
@Override
protected Void run() throws Exception {
  GerritApi gerritApi =
      new GerritApiBuilder().stepContext(getContext()).requireAuthentication().build();
  if (gerritApi == null) {
    return null;
  }

  GerritChange change = new GerritChange(getContext());
  if (change.valid()) {
    ReviewInput reviewInput = new ReviewInput().message(message);
    boolean notifyOwner = false;
    if (labels == null && label != null) {
      labels = Collections.singletonMap(label, score);
    }
    listener
        .getLogger()
        .format(
            "Gerrit review change %d/%d labels %s (%s)%n",
            change.getChangeId(), change.getRevision(), labels, message);
    if (labels != null) {
      for (Map.Entry<String, Integer> l : labels.entrySet()) {
        reviewInput.label(l.getKey(), l.getValue());
        if (l.getValue() < 0) {
          notifyOwner = true;
        }
      }
    }
    reviewInput.drafts = ReviewInput.DraftHandling.PUBLISH;
    reviewInput.tag = "autogenerated:jenkins";
    if (notifyOwner) {
      reviewInput.notify = NotifyHandling.OWNER;
    }
    gerritApi
        .changes()
        .id(change.getChangeId())
        .revision(change.getRevision())
        .review(reviewInput);
  }
  return null;
}
 
Example #8
Source File: GerritReviewStepTest.java    From gerrit-code-review-plugin with Apache License 2.0 4 votes vote down vote up
@Test
public void gerritReviewStepInvokeTest() throws Exception {
  int changeId = 4321;
  int revision = 1;
  String label = "Verfied";
  int score = -1;
  String message = "Does not work";
  String branch = String.format("%02d/%d/%d", changeId % 100, changeId, revision);

  UsernamePasswordCredentialsImpl c =
      new UsernamePasswordCredentialsImpl(
          CredentialsScope.GLOBAL, "cid", "cid", "USERNAME", "PASSWORD");
  CredentialsProvider.lookupStores(j.jenkins)
      .iterator()
      .next()
      .addCredentials(Domain.global(), c);
  WorkflowJob p = j.jenkins.createProject(WorkflowJob.class, "p");
  p.setDefinition(
      new CpsFlowDefinition(
          String.format(
              ""
                  + "node {\n"
                  + "  withEnv([\n"
                  + "    'GERRIT_API_URL=https://%s:%s/a/project',\n"
                  + "    'GERRIT_API_INSECURE_HTTPS=true',\n"
                  + "    'GERRIT_CREDENTIALS_ID=cid',\n"
                  + "    'BRANCH_NAME=%s',\n"
                  + "  ]) {\n"
                  + "    gerritReview labels: ['%s': %s], message: '%s'\n"
                  + "  }\n"
                  + "}",
              g.getClient().remoteAddress().getHostString(),
              g.getClient().remoteAddress().getPort(),
              branch,
              label,
              score,
              message),
          true));

  ReviewInput reviewInput = new ReviewInputForObjectMapper().label(label, score).message(message);
  reviewInput.drafts = ReviewInput.DraftHandling.PUBLISH;
  reviewInput.tag = "autogenerated:jenkins";
  reviewInput.notify = NotifyHandling.OWNER;
  g.getClient()
      .when(
          HttpRequest.request(
                  String.format(
                      "/a/project/a/changes/%s/revisions/%s/review", changeId, revision))
              .withMethod("POST")
              .withBody(JsonBody.json(reviewInput)))
      .respond(
          HttpResponse.response()
              .withStatusCode(200)
              .withBody(JsonBody.json(Collections.emptyMap())));

  WorkflowRun run = j.assertBuildStatusSuccess(p.scheduleBuild2(0));
  String log = JenkinsRule.getLog(run);

  g.getClient()
      .verify(
          HttpRequest.request(
              String.format("/a/project/a/changes/%s/revisions/%s/review", changeId, revision)),
          VerificationTimes.once());
}
 
Example #9
Source File: GerritReviewStepTest.java    From gerrit-code-review-plugin with Apache License 2.0 4 votes vote down vote up
@Test
public void gerritReviewStepInvokeLabelsTest() throws Exception {
  int changeId = 4321;
  int revision = 1;
  String label1 = "Verfied";
  int score1 = -1;
  String label2 = "CI-Review";
  int score2 = -1;
  String message = "Does not work";
  String branch = String.format("%02d/%d/%d", changeId % 100, changeId, revision);

  UsernamePasswordCredentialsImpl c =
      new UsernamePasswordCredentialsImpl(
          CredentialsScope.GLOBAL, "cid", "cid", "USERNAME", "PASSWORD");
  CredentialsProvider.lookupStores(j.jenkins)
      .iterator()
      .next()
      .addCredentials(Domain.global(), c);
  WorkflowJob p = j.jenkins.createProject(WorkflowJob.class, "p");
  p.setDefinition(
      new CpsFlowDefinition(
          String.format(
              ""
                  + "node {\n"
                  + "  withEnv([\n"
                  + "    'GERRIT_API_URL=https://%s:%s/a/project',\n"
                  + "    'GERRIT_API_INSECURE_HTTPS=true',\n"
                  + "    'GERRIT_CREDENTIALS_ID=cid',\n"
                  + "    'BRANCH_NAME=%s',\n"
                  + "  ]) {\n"
                  + "    gerritReview labels: ['%s': %s, '%s': %s], message: '%s'\n"
                  + "  }\n"
                  + "}",
              g.getClient().remoteAddress().getHostString(),
              g.getClient().remoteAddress().getPort(),
              branch,
              label1,
              score1,
              label2,
              score2,
              message),
          true));

  ReviewInput reviewInput =
      new ReviewInputForObjectMapper()
          .label(label1, score1)
          .label(label2, score2)
          .message(message);
  reviewInput.drafts = ReviewInput.DraftHandling.PUBLISH;
  reviewInput.tag = "autogenerated:jenkins";
  reviewInput.notify = NotifyHandling.OWNER;
  g.getClient()
      .when(
          HttpRequest.request(
                  String.format(
                      "/a/project/a/changes/%s/revisions/%s/review", changeId, revision))
              .withMethod("POST")
              .withBody(JsonBody.json(reviewInput)))
      .respond(
          HttpResponse.response()
              .withStatusCode(200)
              .withBody(JsonBody.json(Collections.emptyMap())));

  WorkflowRun run = j.assertBuildStatusSuccess(p.scheduleBuild2(0));
  String log = JenkinsRule.getLog(run);

  g.getClient()
      .verify(
          HttpRequest.request(
              String.format("/a/project/a/changes/%s/revisions/%s/review", changeId, revision)),
          VerificationTimes.once());
}
 
Example #10
Source File: ReviewInputBuilder.java    From sputnik with Apache License 2.0 4 votes vote down vote up
@NotNull
private List<ReviewInput.CommentInput> buildFileComments(@NotNull ReviewFile reviewFile) {
    return reviewFile.getComments().stream()
            .map(this::buildCommentInput)
            .collect(Collectors.toList());
}