下面列出了org.junit.jupiter.params.provider.NullAndEmptySource#com.google.gerrit.extensions.api.changes.ReviewInput 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@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;
}
@NotNull
private ReviewInput.CommentInput buildCommentInput(Comment comment) {
ReviewInput.CommentInput commentInput = new ReviewInput.CommentInput();
commentInput.line = comment.getLine();
commentInput.message = comment.getMessage();
return commentInput;
}
@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);
}
}
@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);
}
@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();
}
@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);
}
@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;
}
@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());
}
@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());
}
@NotNull
private List<ReviewInput.CommentInput> buildFileComments(@NotNull ReviewFile reviewFile) {
return reviewFile.getComments().stream()
.map(this::buildCommentInput)
.collect(Collectors.toList());
}