Search in sources :

Example 6 with RevertSubmissionInfo

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);
}
Also used : RevertSubmissionInfo(com.google.gerrit.extensions.common.RevertSubmissionInfo) RefSpec(org.eclipse.jgit.transport.RefSpec) ChangeApi(com.google.gerrit.extensions.api.changes.ChangeApi) PushOneCommit(com.google.gerrit.acceptance.PushOneCommit) GerritConfig(com.google.gerrit.acceptance.config.GerritConfig) Test(org.junit.Test) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest)

Example 7 with RevertSubmissionInfo

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);
}
Also used : RevertSubmissionInfo(com.google.gerrit.extensions.common.RevertSubmissionInfo) ChangeApi(com.google.gerrit.extensions.api.changes.ChangeApi) PushOneCommit(com.google.gerrit.acceptance.PushOneCommit) Test(org.junit.Test) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest)

Example 8 with RevertSubmissionInfo

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);
}
Also used : RevertSubmissionInfo(com.google.gerrit.extensions.common.RevertSubmissionInfo) InMemoryRepository(org.eclipse.jgit.internal.storage.dfs.InMemoryRepository) ChangeApi(com.google.gerrit.extensions.api.changes.ChangeApi) PushOneCommit(com.google.gerrit.acceptance.PushOneCommit) GerritConfig(com.google.gerrit.acceptance.config.GerritConfig) Test(org.junit.Test) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest)

Example 9 with RevertSubmissionInfo

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);
}
Also used : InMemoryRepository(org.eclipse.jgit.internal.storage.dfs.InMemoryRepository) ChangeApi(com.google.gerrit.extensions.api.changes.ChangeApi) ArrayList(java.util.ArrayList) ChangeMessageInfo(com.google.gerrit.extensions.common.ChangeMessageInfo) PushOneCommit(com.google.gerrit.acceptance.PushOneCommit) RevertSubmissionInfo(com.google.gerrit.extensions.common.RevertSubmissionInfo) GerritConfig(com.google.gerrit.acceptance.config.GerritConfig) Test(org.junit.Test) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest)

Example 10 with RevertSubmissionInfo

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;
}
Also used : ArrayListMultimap(com.google.common.collect.ArrayListMultimap) Arrays(java.util.Arrays) CommitMessageUtil(com.google.gerrit.server.util.CommitMessageUtil) ObjectInserter(org.eclipse.jgit.lib.ObjectInserter) ProjectCache(com.google.gerrit.server.project.ProjectCache) Inject(com.google.inject.Inject) BooleanCondition.and(com.google.gerrit.extensions.conditions.BooleanCondition.and) PermissionBackend(com.google.gerrit.server.permissions.PermissionBackend) UpdateException(com.google.gerrit.server.update.UpdateException) RestModifyView(com.google.gerrit.extensions.restapi.RestModifyView) RevWalk(org.eclipse.jgit.revwalk.RevWalk) BatchUpdate(com.google.gerrit.server.update.BatchUpdate) Matcher(java.util.regex.Matcher) RefNames(com.google.gerrit.entities.RefNames) AuthException(com.google.gerrit.extensions.restapi.AuthException) UiAction(com.google.gerrit.extensions.webui.UiAction) CREATE_CHANGE(com.google.gerrit.server.permissions.RefPermission.CREATE_CHANGE) REVERT(com.google.gerrit.server.permissions.ChangePermission.REVERT) RevertSubmissionInfo(com.google.gerrit.extensions.common.RevertSubmissionInfo) Collection(java.util.Collection) ChangeMessages(com.google.gerrit.server.change.ChangeMessages) Set(java.util.Set) Instant(java.time.Instant) Collectors(java.util.stream.Collectors) BranchNameKey(com.google.gerrit.entities.BranchNameKey) CommitUtil(com.google.gerrit.server.git.CommitUtil) NotifyHandling(com.google.gerrit.extensions.api.changes.NotifyHandling) ChangeData(com.google.gerrit.server.query.change.ChangeData) List(java.util.List) ChangeReverted(com.google.gerrit.server.extensions.events.ChangeReverted) InternalChangeQuery(com.google.gerrit.server.query.change.InternalChangeQuery) ChangeJson(com.google.gerrit.server.change.ChangeJson) MoreObjects.firstNonNull(com.google.common.base.MoreObjects.firstNonNull) RandomStringUtils(org.apache.commons.lang3.RandomStringUtils) BatchUpdateOp(com.google.gerrit.server.update.BatchUpdateOp) Pattern(java.util.regex.Pattern) FluentLogger(com.google.common.flogger.FluentLogger) ChangeMessagesUtil(com.google.gerrit.server.ChangeMessagesUtil) Iterables(com.google.common.collect.Iterables) PermissionBackendException(com.google.gerrit.server.permissions.PermissionBackendException) WalkSorter(com.google.gerrit.server.change.WalkSorter) ConfigInvalidException(org.eclipse.jgit.errors.ConfigInvalidException) RevCommit(org.eclipse.jgit.revwalk.RevCommit) ChangePermission(com.google.gerrit.server.permissions.ChangePermission) Multimap(com.google.common.collect.Multimap) Response(com.google.gerrit.extensions.restapi.Response) MessageFormat(java.text.MessageFormat) ArrayList(java.util.ArrayList) Strings(com.google.common.base.Strings) MessageIdGenerator(com.google.gerrit.server.mail.send.MessageIdGenerator) ChangeResource(com.google.gerrit.server.change.ChangeResource) PostUpdateContext(com.google.gerrit.server.update.PostUpdateContext) Objects.requireNonNull(java.util.Objects.requireNonNull) ChangeInfo(com.google.gerrit.extensions.common.ChangeInfo) RevertedSender(com.google.gerrit.server.mail.send.RevertedSender) Change(com.google.gerrit.entities.Change) ContributorAgreementsChecker(com.google.gerrit.server.project.ContributorAgreementsChecker) RestApiException(com.google.gerrit.extensions.restapi.RestApiException) ChangeUtil(com.google.gerrit.server.ChangeUtil) ChangeContext(com.google.gerrit.server.update.ChangeContext) PatchSetData(com.google.gerrit.server.change.WalkSorter.PatchSetData) ProjectCache.illegalState(com.google.gerrit.server.project.ProjectCache.illegalState) CurrentUser(com.google.gerrit.server.CurrentUser) RevertInput(com.google.gerrit.extensions.api.changes.RevertInput) NotifyResolver(com.google.gerrit.server.change.NotifyResolver) Sequences(com.google.gerrit.server.notedb.Sequences) Iterator(java.util.Iterator) StorageException(com.google.gerrit.exceptions.StorageException) ProjectState(com.google.gerrit.server.project.ProjectState) NoSuchProjectException(com.google.gerrit.server.project.NoSuchProjectException) ChangeNotes(com.google.gerrit.server.notedb.ChangeNotes) IOException(java.io.IOException) ObjectId(org.eclipse.jgit.lib.ObjectId) Provider(com.google.inject.Provider) GitRepositoryManager(com.google.gerrit.server.git.GitRepositoryManager) ResourceConflictException(com.google.gerrit.extensions.restapi.ResourceConflictException) RevisionResource(com.google.gerrit.server.change.RevisionResource) Project(com.google.gerrit.entities.Project) CherryPickInput(com.google.gerrit.extensions.api.changes.CherryPickInput) TimeUtil(com.google.gerrit.server.util.time.TimeUtil) Result(com.google.gerrit.server.restapi.change.CherryPickChange.Result) Comparator(java.util.Comparator) PatchSetUtil(com.google.gerrit.server.PatchSetUtil) ObjectReader(org.eclipse.jgit.lib.ObjectReader) Repository(org.eclipse.jgit.lib.Repository) PatchSetData(com.google.gerrit.server.change.WalkSorter.PatchSetData) ObjectId(org.eclipse.jgit.lib.ObjectId) Instant(java.time.Instant) ChangeData(com.google.gerrit.server.query.change.ChangeData) Project(com.google.gerrit.entities.Project) RevertSubmissionInfo(com.google.gerrit.extensions.common.RevertSubmissionInfo) BranchNameKey(com.google.gerrit.entities.BranchNameKey)

Aggregations

RevertSubmissionInfo (com.google.gerrit.extensions.common.RevertSubmissionInfo)10 AbstractDaemonTest (com.google.gerrit.acceptance.AbstractDaemonTest)9 Test (org.junit.Test)9 PushOneCommit (com.google.gerrit.acceptance.PushOneCommit)7 ChangeApi (com.google.gerrit.extensions.api.changes.ChangeApi)7 GerritConfig (com.google.gerrit.acceptance.config.GerritConfig)6 RevertInput (com.google.gerrit.extensions.api.changes.RevertInput)3 ArrayList (java.util.ArrayList)3 InMemoryRepository (org.eclipse.jgit.internal.storage.dfs.InMemoryRepository)2 MoreObjects.firstNonNull (com.google.common.base.MoreObjects.firstNonNull)1 Strings (com.google.common.base.Strings)1 ArrayListMultimap (com.google.common.collect.ArrayListMultimap)1 Iterables (com.google.common.collect.Iterables)1 Multimap (com.google.common.collect.Multimap)1 FluentLogger (com.google.common.flogger.FluentLogger)1 BranchNameKey (com.google.gerrit.entities.BranchNameKey)1 Change (com.google.gerrit.entities.Change)1 Project (com.google.gerrit.entities.Project)1 RefNames (com.google.gerrit.entities.RefNames)1 StorageException (com.google.gerrit.exceptions.StorageException)1