use of com.google.gerrit.extensions.common.RevertSubmissionInfo in project gerrit by GerritCodeReview.
the class RevertIT method revertSubmissionUnrelatedWithTwoMergeCommits.
@Test
@GerritConfig(name = "change.submitWholeTopic", value = "true")
public void revertSubmissionUnrelatedWithTwoMergeCommits() throws Exception {
String topic = "topic";
PushOneCommit.Result firstResult = createChange(testRepo, "master", "first change", "a.txt", "message", topic);
approve(firstResult.getChangeId());
testRepo.reset("HEAD~1");
PushOneCommit.Result secondResult = createChange(testRepo, "master", "second change", "b.txt", "message", topic);
approve(secondResult.getChangeId());
testRepo.reset("HEAD~1");
PushOneCommit.Result thirdResult = createChange(testRepo, "master", "third change", "c.txt", "message", topic);
approve(thirdResult.getChangeId());
gApi.changes().id(firstResult.getChangeId()).current().submit();
// put the head on the most recent merge commit.
testRepo.git().fetch().setRefSpecs(new RefSpec("refs/heads/master:merge")).call();
testRepo.reset("merge");
// Create another change that should be ignored. The reverts should be rebased on top of the
// merge commit.
PushOneCommit.Result fourthResult = createChange(testRepo, "master", "fourth change", "d.txt", "message", topic);
approve(fourthResult.getChangeId());
gApi.changes().id(fourthResult.getChangeId()).current().submit();
RevertSubmissionInfo revertSubmissionInfo = gApi.changes().id(secondResult.getChangeId()).revertSubmission();
assertThat(revertSubmissionInfo.revertChanges.stream().map(change -> change.created).distinct().count()).isEqualTo(1);
List<ChangeApi> revertChanges = getChangeApis(revertSubmissionInfo);
Collections.reverse(revertChanges);
assertThat(revertChanges.get(0).current().files().get("c.txt").linesDeleted).isEqualTo(1);
assertThat(revertChanges.get(1).current().files().get("b.txt").linesDeleted).isEqualTo(1);
assertThat(revertChanges.get(2).current().files().get("a.txt").linesDeleted).isEqualTo(1);
String sha1FirstRevert = revertChanges.get(0).current().commit(false).commit;
String sha1SecondRevert = revertChanges.get(1).current().commit(false).commit;
// parent of the first revert is the merged change of previous changes.
assertThat(revertChanges.get(0).current().commit(false).parents.get(0).subject).contains("Merge \"third change\"");
// Next reverts would stack on top of the previous ones.
assertThat(revertChanges.get(1).current().commit(false).parents.get(0).commit).isEqualTo(sha1FirstRevert);
assertThat(revertChanges.get(2).current().commit(false).parents.get(0).commit).isEqualTo(sha1SecondRevert);
assertThat(revertChanges).hasSize(3);
assertThat(gApi.changes().id(revertChanges.get(1).id()).current().related().changes).hasSize(3);
}
use of com.google.gerrit.extensions.common.RevertSubmissionInfo in project gerrit by GerritCodeReview.
the class RevertIT method revertSubmissionWithDependantChange.
@Test
public void revertSubmissionWithDependantChange() throws Exception {
PushOneCommit.Result firstResult = createChange("first change", "a.txt", "message");
PushOneCommit.Result secondResult = createChange("second change", "b.txt", "other");
approve(secondResult.getChangeId());
approve(firstResult.getChangeId());
gApi.changes().id(secondResult.getChangeId()).current().submit();
RevertSubmissionInfo revertSubmissionInfo = gApi.changes().id(firstResult.getChangeId()).revertSubmission();
assertThat(revertSubmissionInfo.revertChanges.stream().map(change -> change.created).distinct().count()).isEqualTo(1);
List<ChangeApi> revertChanges = getChangeApis(revertSubmissionInfo);
Collections.reverse(revertChanges);
String sha1SecondChange = secondResult.getCommit().getName();
String sha1FirstRevert = revertChanges.get(0).current().commit(false).commit;
assertThat(revertChanges.get(0).current().commit(false).parents.get(0).commit).isEqualTo(sha1SecondChange);
assertThat(revertChanges.get(1).current().commit(false).parents.get(0).commit).isEqualTo(sha1FirstRevert);
assertThat(revertChanges.get(0).get().revertOf).isEqualTo(secondResult.getChange().change().getChangeId());
assertThat(revertChanges.get(1).get().revertOf).isEqualTo(firstResult.getChange().change().getChangeId());
assertThat(revertChanges.get(0).current().files().get("b.txt").linesDeleted).isEqualTo(1);
assertThat(revertChanges.get(1).current().files().get("a.txt").linesDeleted).isEqualTo(1);
assertThat(revertChanges).hasSize(2);
assertThat(gApi.changes().id(revertChanges.get(0).id()).current().related().changes).hasSize(2);
}
use of com.google.gerrit.extensions.common.RevertSubmissionInfo in project gerrit by GerritCodeReview.
the class RevertIT method revertSubmissionDifferentRepositories.
@Test
@GerritConfig(name = "change.submitWholeTopic", value = "true")
public void revertSubmissionDifferentRepositories() throws Exception {
projectOperations.newProject().name("secondProject").create();
TestRepository<InMemoryRepository> secondRepo = cloneProject(Project.nameKey("secondProject"), admin);
String topic = "topic";
PushOneCommit.Result firstResult = createChange(testRepo, "master", "first change", "a.txt", "message", topic);
PushOneCommit.Result secondResult = createChange(secondRepo, "master", "second change", "b.txt", "other", topic);
approve(secondResult.getChangeId());
approve(firstResult.getChangeId());
// submit both changes
gApi.changes().id(secondResult.getChangeId()).current().submit();
RevertSubmissionInfo revertSubmissionInfo = gApi.changes().id(secondResult.getChangeId()).revertSubmission();
assertThat(revertSubmissionInfo.revertChanges.stream().map(change -> change.created).distinct().count()).isEqualTo(1);
List<ChangeApi> revertChanges = getChangeApis(revertSubmissionInfo);
// has size 2 because of the same topic, and submitWholeTopic is true.
assertThat(gApi.changes().id(revertChanges.get(0).get()._number).submittedTogether()).hasSize(2);
String sha1SecondChange = secondResult.getCommit().getName();
String sha1FirstChange = firstResult.getCommit().getName();
assertThat(revertChanges.get(0).current().commit(false).parents.get(0).commit).isEqualTo(sha1FirstChange);
assertThat(revertChanges.get(1).current().commit(false).parents.get(0).commit).isEqualTo(sha1SecondChange);
assertThat(revertChanges.get(0).get().revertOf).isEqualTo(firstResult.getChange().change().getChangeId());
assertThat(revertChanges.get(1).get().revertOf).isEqualTo(secondResult.getChange().change().getChangeId());
assertThat(revertChanges.get(0).current().files().get("a.txt").linesDeleted).isEqualTo(1);
assertThat(revertChanges.get(1).current().files().get("b.txt").linesDeleted).isEqualTo(1);
assertThat(revertChanges).hasSize(2);
}
use of com.google.gerrit.extensions.common.RevertSubmissionInfo in project gerrit by GerritCodeReview.
the class RevertIT method revertSubmissionDifferentRepositoriesWithDependantChange.
@Test
@GerritConfig(name = "change.submitWholeTopic", value = "true")
public void revertSubmissionDifferentRepositoriesWithDependantChange() throws Exception {
projectOperations.newProject().name("secondProject").create();
TestRepository<InMemoryRepository> secondRepo = cloneProject(Project.nameKey("secondProject"), admin);
List<PushOneCommit.Result> resultCommits = new ArrayList<>();
String topic = "topic";
resultCommits.add(createChange(secondRepo, "master", "first change", "a.txt", "message", topic));
resultCommits.add(createChange(secondRepo, "master", "second change", "b.txt", "Other message", topic));
resultCommits.add(createChange(testRepo, "master", "main repo change", "a.txt", "message", topic));
for (PushOneCommit.Result result : resultCommits) {
approve(result.getChangeId());
}
// submit all changes
gApi.changes().id(resultCommits.get(1).getChangeId()).current().submit();
RevertSubmissionInfo revertSubmissionInfo = gApi.changes().id(resultCommits.get(1).getChangeId()).revertSubmission();
assertThat(revertSubmissionInfo.revertChanges.stream().map(change -> change.created).distinct().count()).isEqualTo(1);
List<ChangeApi> revertChanges = getChangeApis(revertSubmissionInfo);
assertThat(revertChanges).hasSize(3);
String sha1RevertOfTheSecondChange = revertChanges.get(1).current().commit(false).commit;
String sha1SecondChange = resultCommits.get(1).getCommit().getName();
String sha1ThirdChange = resultCommits.get(2).getCommit().getName();
assertThat(revertChanges.get(0).current().commit(false).parents.get(0).commit).isEqualTo(sha1RevertOfTheSecondChange);
assertThat(revertChanges.get(1).current().commit(false).parents.get(0).commit).isEqualTo(sha1SecondChange);
assertThat(revertChanges.get(2).current().commit(false).parents.get(0).commit).isEqualTo(sha1ThirdChange);
assertThat(revertChanges.get(0).current().files().get("a.txt").linesDeleted).isEqualTo(1);
assertThat(revertChanges.get(1).current().files().get("b.txt").linesDeleted).isEqualTo(1);
assertThat(revertChanges.get(2).current().files().get("a.txt").linesDeleted).isEqualTo(1);
// has size 3 because of the same topic, and submitWholeTopic is true.
assertThat(gApi.changes().id(revertChanges.get(0).get()._number).submittedTogether()).hasSize(3);
for (int i = 0; i < resultCommits.size(); i++) {
assertThat(revertChanges.get(i).get().revertOf).isEqualTo(resultCommits.get(i).getChange().change().getChangeId());
List<ChangeMessageInfo> sourceMessages = new ArrayList<>(gApi.changes().id(resultCommits.get(i).getChangeId()).get().messages);
assertThat(sourceMessages).hasSize(4);
String expectedMessage = String.format("Created a revert of this change as %s", revertChanges.get(i).get().changeId);
assertThat(sourceMessages.get(3).message).isEqualTo(expectedMessage);
// Expected message on the created change: "Uploaded patch set 1."
List<ChangeMessageInfo> messages = revertChanges.get(i).get().messages.stream().collect(toList());
assertThat(messages).hasSize(1);
assertThat(messages.get(0).message).isEqualTo("Uploaded patch set 1.");
assertThat(revertChanges.get(i).get().revertOf).isEqualTo(gApi.changes().id(resultCommits.get(i).getChangeId()).get()._number);
assertThat(revertChanges.get(i).get().topic).startsWith("revert-" + resultCommits.get(0).getChange().change().getSubmissionId());
}
assertThat(gApi.changes().id(revertChanges.get(1).id()).current().related().changes).hasSize(2);
}
use of com.google.gerrit.extensions.common.RevertSubmissionInfo in project gerrit by GerritCodeReview.
the class RevertSubmission method revertSubmission.
private RevertSubmissionInfo revertSubmission(List<ChangeData> changeData, RevertInput revertInput) throws RestApiException, IOException, UpdateException, ConfigInvalidException, StorageException, PermissionBackendException {
Multimap<BranchNameKey, ChangeData> changesPerProjectAndBranch = ArrayListMultimap.create();
changeData.stream().forEach(c -> changesPerProjectAndBranch.put(c.change().getDest(), c));
cherryPickInput = createCherryPickInput(revertInput);
Instant timestamp = TimeUtil.now();
for (BranchNameKey projectAndBranch : changesPerProjectAndBranch.keySet()) {
cherryPickInput.base = null;
Project.NameKey project = projectAndBranch.project();
cherryPickInput.destination = projectAndBranch.branch();
if (revertInput.workInProgress) {
cherryPickInput.notify = firstNonNull(cherryPickInput.notify, NotifyHandling.OWNER);
}
Collection<ChangeData> changesInProjectAndBranch = changesPerProjectAndBranch.get(projectAndBranch);
// Sort the changes topologically.
Iterator<PatchSetData> sortedChangesInProjectAndBranch = sorter.sort(changesInProjectAndBranch).iterator();
Set<ObjectId> commitIdsInProjectAndBranch = changesInProjectAndBranch.stream().map(c -> c.currentPatchSet().commitId()).collect(Collectors.toSet());
revertAllChangesInProjectAndBranch(revertInput, project, sortedChangesInProjectAndBranch, commitIdsInProjectAndBranch, timestamp);
}
results.sort(Comparator.comparing(c -> c.revertOf));
RevertSubmissionInfo revertSubmissionInfo = new RevertSubmissionInfo();
revertSubmissionInfo.revertChanges = results;
return revertSubmissionInfo;
}
Aggregations