Allow to specify the commit message of a new patchset of the test API

Change-Id: I9568fba0138ccccb16e64fc154ca11c412ed2a1e
This commit is contained in:
Alice Kober-Sotzek
2020-08-14 15:12:51 +02:00
parent 907b99179b
commit c5aabdd1e7
3 changed files with 75 additions and 2 deletions

View File

@@ -348,7 +348,7 @@ public class ChangeOperationsImpl implements ChangeOperations {
ChangeNotes changeNotes,
TestPatchsetCreation patchsetCreation,
Timestamp now)
throws IOException {
throws IOException, BadRequestException {
ObjectId oldPatchsetCommitId = changeNotes.getCurrentPatchSet().commitId();
RevCommit oldPatchsetCommit = repository.parseCommit(oldPatchsetCommitId);
@@ -356,7 +356,10 @@ public class ChangeOperationsImpl implements ChangeOperations {
createNewTree(
repository, revWalk, oldPatchsetCommitId, patchsetCreation.treeModifications());
String commitMessage = oldPatchsetCommit.getFullMessage();
String commitMessage =
correctCommitMessage(
changeNotes.getChange().getKey().get(),
patchsetCreation.commitMessage().orElseGet(oldPatchsetCommit::getFullMessage));
ImmutableList<ObjectId> parentCommitIds = getParents(oldPatchsetCommit);
PersonIdent author = getAuthor(oldPatchsetCommit);
@@ -364,6 +367,19 @@ public class ChangeOperationsImpl implements ChangeOperations {
return createCommit(objectInserter, tree, parentCommitIds, author, committer, commitMessage);
}
private String correctCommitMessage(String oldChangeId, String desiredCommitMessage)
throws BadRequestException {
String commitMessage = CommitMessageUtil.checkAndSanitizeCommitMessage(desiredCommitMessage);
// Remove initial 'I' and treat the rest as ObjectId. This is not the cleanest approach but
// unfortunately, we don't seem to have other utility code which takes the string-based
// change-id and ensures that it is part of the commit message.
ObjectId id = ObjectId.fromString(oldChangeId.substring(1));
commitMessage = ChangeIdUtil.insertId(commitMessage, id, false);
return commitMessage;
}
private PersonIdent getAuthor(RevCommit oldPatchsetCommit) {
return Optional.ofNullable(oldPatchsetCommit.getAuthorIdent()).orElse(serverIdent);
}

View File

@@ -19,11 +19,14 @@ import com.google.common.collect.ImmutableList;
import com.google.gerrit.acceptance.testsuite.ThrowingFunction;
import com.google.gerrit.entities.PatchSet;
import com.google.gerrit.server.edit.tree.TreeModification;
import java.util.Optional;
/** Initial attributes of the patchset. If not provided, arbitrary values will be used. */
@AutoValue
public abstract class TestPatchsetCreation {
public abstract Optional<String> commitMessage();
public abstract ImmutableList<TreeModification> treeModifications();
abstract ThrowingFunction<TestPatchsetCreation, PatchSet.Id> patchsetCreator();
@@ -36,6 +39,8 @@ public abstract class TestPatchsetCreation {
@AutoValue.Builder
public abstract static class Builder {
public abstract Builder commitMessage(String commitMessage);
/** Modified file of the patchset. The file content is specified via the returned builder. */
public FileContentBuilder<Builder> file(String filePath) {
return new FileContentBuilder<>(this, filePath, treeModificationsBuilder()::add);