Add title of reverted change in RevertSubmission Currently, changes being reverted through the RevertSubmission endpoint have the same commit message. This will still be the case, except for the title. Now, the title of all changes created by RevertSubmission will be "Revert <Change_Reverted>". All other information in the commit message will remain. Change-Id: I77949ad9f2d87f75f76da18c31a11bacee093b00
diff --git a/Documentation/rest-api-changes.txt b/Documentation/rest-api-changes.txt index f99973a..4b7a659 100644 --- a/Documentation/rest-api-changes.txt +++ b/Documentation/rest-api-changes.txt
@@ -1515,6 +1515,8 @@ Creates open revert changes for all of the changes of a certain submission. +The subject of each revert change will be "Revert <subject-of-reverted-change". + Details for the revert can be specified in the request body inside a link:#revert-input[ RevertInput] The topic of all created revert changes will be `revert-{submission_id}-{random_string_of_size_10}`.
diff --git a/java/com/google/gerrit/server/restapi/change/RevertSubmission.java b/java/com/google/gerrit/server/restapi/change/RevertSubmission.java index be33405..df4c83e 100644 --- a/java/com/google/gerrit/server/restapi/change/RevertSubmission.java +++ b/java/com/google/gerrit/server/restapi/change/RevertSubmission.java
@@ -250,6 +250,8 @@ if (cherryPickInput.base == null) { cherryPickInput.base = getBase(changeNotes, commitIdsInProjectAndBranch).name(); } + + revertInput.message = getMessage(revertInput, changeNotes); // This is the code in case this is the first revert of this project + branch, and the // revert would be on top of the change being reverted. if (cherryPickInput.base.equals(changeNotes.getCurrentPatchSet().commitId().getName())) { @@ -271,13 +273,7 @@ commitUtil.createRevertCommit(revertInput.message, changeNotes, user.get()); // TODO (paiking): As a future change, the revert should just be done directly on the // target rather than just creating a commit and then cherry-picking it. - cherryPickInput.message = - revertInput.message != null - ? revertInput.message - : MessageFormat.format( - ChangeMessages.get().revertChangeDefaultMessage, - changeNotes.getChange().getSubject(), - changeNotes.getCurrentPatchSet().commitId().name()); + cherryPickInput.message = revertInput.message; ObjectId generatedChangeId = Change.generateChangeId(); Change.Id cherryPickRevertChangeId = Change.id(seq.nextChangeId()); if (groupName == null) { @@ -315,6 +311,18 @@ return revertSubmissionInfo; } + private String getMessage(RevertInput revertInput, ChangeNotes changeNotes) { + String subject = changeNotes.getChange().getSubject(); + if (revertInput.message == null) { + return MessageFormat.format( + ChangeMessages.get().revertChangeDefaultMessage, + subject, + changeNotes.getCurrentPatchSet().commitId().name()); + } + + return String.format("Revert \"%s\"\n\n%s", subject, revertInput.message); + } + /** * This function finds the base that the first revert in a project + branch should be based on. It * searches using BFS for the first commit that is either: 1. Has 2 or more parents, and has as
diff --git a/javatests/com/google/gerrit/acceptance/api/change/RevertIT.java b/javatests/com/google/gerrit/acceptance/api/change/RevertIT.java index 61dd31a..f1ea319 100644 --- a/javatests/com/google/gerrit/acceptance/api/change/RevertIT.java +++ b/javatests/com/google/gerrit/acceptance/api/change/RevertIT.java
@@ -672,13 +672,36 @@ @Test public void revertSubmissionWithSetMessage() throws Exception { - String result = createChange().getChangeId(); + String result = createChange("change", "a.txt", "message").getChangeId(); gApi.changes().id(result).current().review(ReviewInput.approve()); gApi.changes().id(result).current().submit(); RevertInput revertInput = new RevertInput(); - revertInput.message = "Message from input"; - assertThat(gApi.changes().id(result).revertSubmission(revertInput).revertChanges.get(0).subject) - .isEqualTo(revertInput.message); + String commitMessage = "Message from input"; + revertInput.message = commitMessage; + ChangeInfo revertChange = + gApi.changes().id(result).revertSubmission(revertInput).revertChanges.get(0); + assertThat(revertChange.subject).isEqualTo("Revert \"change\""); + assertThat(gApi.changes().id(revertChange.id).current().commit(false).message) + .isEqualTo( + String.format( + "Revert \"change\"\n\n%s\n\nChange-Id: %s\n", + commitMessage, revertChange.changeId)); + } + + @Test + public void revertSubmissionWithoutMessage() throws Exception { + String result = createChange("change", "a.txt", "message").getChangeId(); + gApi.changes().id(result).current().review(ReviewInput.approve()); + gApi.changes().id(result).current().submit(); + RevertInput revertInput = new RevertInput(); + ChangeInfo revertChange = + gApi.changes().id(result).revertSubmission(revertInput).revertChanges.get(0); + assertThat(revertChange.subject).isEqualTo("Revert \"change\""); + assertThat(gApi.changes().id(revertChange.id).current().commit(false).message) + .isEqualTo( + String.format( + "Revert \"change\"\n\nThis reverts commit %s.\n\nChange-Id: %s\n", + gApi.changes().id(result).get().currentRevision, revertChange.changeId)); } @Test