use of com.google.gerrit.extensions.api.changes.CherryPickInput in project gerrit by GerritCodeReview.
the class CommitIT method cherryPickWithoutMessageSameBranch.
@Test
public void cherryPickWithoutMessageSameBranch() throws Exception {
String destBranch = "master";
// Create change to cherry-pick
PushOneCommit.Result r = createChange();
ChangeInfo changeToCherryPick = info(r.getChangeId());
RevCommit commitToCherryPick = r.getCommit();
// Cherry-pick without message.
CherryPickInput input = new CherryPickInput();
input.destination = destBranch;
ChangeInfo cherryPickResult = gApi.projects().name(project.get()).commit(commitToCherryPick.name()).cherryPick(input).get();
// Expect that the Change-Id of the cherry-picked commit was used for the cherry-pick change.
// New patch-set to existing change was uploaded.
assertThat(cherryPickResult._number).isEqualTo(changeToCherryPick._number);
assertThat(cherryPickResult.revisions).hasSize(2);
assertThat(cherryPickResult.changeId).isEqualTo(changeToCherryPick.changeId);
assertThat(cherryPickResult.messages).hasSize(2);
// Cherry-pick of is not set, because the source change was not provided.
assertThat(cherryPickResult.cherryPickOfChange).isNull();
assertThat(cherryPickResult.cherryPickOfPatchSet).isNull();
// Expect that the message of the cherry-picked commit was used for the cherry-pick change.
RevisionInfo revInfo = cherryPickResult.revisions.get(cherryPickResult.currentRevision);
assertThat(revInfo).isNotNull();
assertThat(revInfo.commit.message).isEqualTo(commitToCherryPick.getFullMessage());
}
use of com.google.gerrit.extensions.api.changes.CherryPickInput in project gerrit by GerritCodeReview.
the class CommitIT method cherryPickCommitWithChangeIdToClosedChange.
@Test
public void cherryPickCommitWithChangeIdToClosedChange() throws Exception {
String destBranch = "refs/heads/foo";
createBranch(BranchNameKey.create(project, destBranch));
PushOneCommit.Result r = createChange("refs/for/" + destBranch);
ChangeInfo existingDestChange = info(r.getChangeId());
String commitToCherryPick = createChange().getCommit().getName();
gApi.changes().id(existingDestChange.changeId).current().review(ReviewInput.approve());
gApi.changes().id(existingDestChange.changeId).current().submit();
CherryPickInput input = new CherryPickInput();
input.destination = destBranch;
input.message = String.format("it goes to foo branch\n\nChange-Id: %s\n", existingDestChange.changeId);
input.allowConflicts = true;
input.allowEmpty = true;
BadRequestException thrown = assertThrows(BadRequestException.class, () -> gApi.projects().name(project.get()).commit(commitToCherryPick).cherryPick(input));
assertThat(thrown).hasMessageThat().isEqualTo(String.format("Cherry-pick with Change-Id %s could not update the existing change %d in " + "destination branch %s of project %s, because the change was closed (MERGED)", existingDestChange.changeId, existingDestChange._number, destBranch, project.get()));
}
use of com.google.gerrit.extensions.api.changes.CherryPickInput in project gerrit by GerritCodeReview.
the class CommitIT method cherryPickCommitWithSetTopic.
@Test
public void cherryPickCommitWithSetTopic() throws Exception {
String branch = "foo";
RevCommit revCommit = createChange().getCommit();
gApi.projects().name(project.get()).branch(branch).create(new BranchInput());
CherryPickInput input = new CherryPickInput();
input.destination = branch;
input.topic = "topic";
String changeId = gApi.projects().name(project.get()).commit(revCommit.name()).cherryPick(input).get().id;
ChangeInfo changeInfo = gApi.changes().id(changeId).get();
assertThat(changeInfo.topic).isEqualTo(input.topic);
}
use of com.google.gerrit.extensions.api.changes.CherryPickInput in project gerrit by GerritCodeReview.
the class RevisionIT method cherryPickToExistingChange.
@Test
public void cherryPickToExistingChange() throws Exception {
PushOneCommit.Result r1 = pushFactory.create(db, admin.getIdent(), testRepo, SUBJECT, FILE_NAME, "a").to("refs/for/master");
String t1 = project.get() + "~master~" + r1.getChangeId();
BranchInput bin = new BranchInput();
bin.revision = r1.getCommit().getParent(0).name();
gApi.projects().name(project.get()).branch("foo").create(bin);
PushOneCommit.Result r2 = pushFactory.create(db, admin.getIdent(), testRepo, SUBJECT, FILE_NAME, "b", r1.getChangeId()).to("refs/for/foo");
String t2 = project.get() + "~foo~" + r2.getChangeId();
gApi.changes().id(t2).abandon();
CherryPickInput in = new CherryPickInput();
in.destination = "foo";
in.message = r1.getCommit().getFullMessage();
try {
gApi.changes().id(t1).current().cherryPick(in);
fail();
} catch (ResourceConflictException e) {
assertThat(e.getMessage()).isEqualTo("Cannot create new patch set of change " + info(t2)._number + " because it is abandoned");
}
gApi.changes().id(t2).restore();
gApi.changes().id(t1).current().cherryPick(in);
assertThat(get(t2).revisions).hasSize(2);
assertThat(gApi.changes().id(t2).current().file(FILE_NAME).content().asString()).isEqualTo("a");
}
use of com.google.gerrit.extensions.api.changes.CherryPickInput in project gerrit by GerritCodeReview.
the class RevisionIT method cherryPickConflictWithAllowConflicts.
@Test
public void cherryPickConflictWithAllowConflicts() throws Exception {
ObjectId initial = repo().exactRef(HEAD).getLeaf().getObjectId();
// Create a branch and push a commit to it (by-passing review)
String destBranch = "foo";
gApi.projects().name(project.get()).branch(destBranch).create(new BranchInput());
String destContent = "some content";
PushOneCommit push = pushFactory.create(admin.newIdent(), testRepo, PushOneCommit.SUBJECT, ImmutableMap.of(PushOneCommit.FILE_NAME, destContent, "foo.txt", "foo"));
push.to("refs/heads/" + destBranch);
// Create a change on master with a commit that conflicts with the commit on the other branch.
testRepo.reset(initial);
String changeContent = "another content";
push = pushFactory.create(admin.newIdent(), testRepo, PushOneCommit.SUBJECT, ImmutableMap.of(PushOneCommit.FILE_NAME, changeContent, "bar.txt", "bar"));
PushOneCommit.Result r = push.to("refs/for/master%topic=someTopic");
// Verify before the cherry-pick that the change has exactly 1 message.
ChangeApi changeApi = change(r);
assertThat(changeApi.get().messages).hasSize(1);
// Cherry-pick the change to the other branch, that should fail with a conflict.
CherryPickInput in = new CherryPickInput();
in.destination = destBranch;
in.message = "Cherry-Pick";
ResourceConflictException thrown = assertThrows(ResourceConflictException.class, () -> changeApi.revision(r.getCommit().name()).cherryPickAsInfo(in));
assertThat(thrown).hasMessageThat().startsWith("Cherry pick failed: merge conflict");
// Cherry-pick with auto merge should succeed.
in.allowConflicts = true;
ChangeInfo cherryPickChange = changeApi.revision(r.getCommit().name()).cherryPickAsInfo(in);
assertThat(cherryPickChange.containsGitConflicts).isTrue();
assertThat(cherryPickChange.workInProgress).isTrue();
// Verify that subject and topic on the cherry-pick change have been correctly populated.
assertThat(cherryPickChange.subject).contains(in.message);
assertThat(cherryPickChange.topic).isEqualTo("someTopic-" + destBranch);
// Verify that the file content in the cherry-pick change is correct.
// We expect that it has conflict markers to indicate the conflict.
BinaryResult bin = gApi.changes().id(cherryPickChange._number).current().file(PushOneCommit.FILE_NAME).content();
ByteArrayOutputStream os = new ByteArrayOutputStream();
bin.writeTo(os);
String fileContent = new String(os.toByteArray(), UTF_8);
String destSha1 = abbreviateName(projectOperations.project(project).getHead(destBranch), 6);
String changeSha1 = abbreviateName(r.getCommit(), 6);
assertThat(fileContent).isEqualTo("<<<<<<< HEAD (" + destSha1 + " test commit)\n" + destContent + "\n" + "=======\n" + changeContent + "\n" + ">>>>>>> CHANGE (" + changeSha1 + " test commit)\n");
// Get details of cherry-pick change.
ChangeInfo cherryPickChangeWithDetails = gApi.changes().id(cherryPickChange._number).get();
assertThat(cherryPickChangeWithDetails.workInProgress).isTrue();
// Verify that a message has been posted on the cherry-pick change.
assertThat(cherryPickChangeWithDetails.messages).hasSize(1);
Iterator<ChangeMessageInfo> cherryIt = cherryPickChangeWithDetails.messages.iterator();
assertThat(cherryIt.next().message).isEqualTo("Patch Set 1: Cherry Picked from branch master.\n\n" + "The following files contain Git conflicts:\n" + "* " + PushOneCommit.FILE_NAME);
}
Aggregations