use of com.google.gerrit.server.edit.CommitModification in project gerrit by GerritCodeReview.
the class FixReplacementInterpreterTest method treeModificationsTargetCorrectFiles.
@Test
public void treeModificationsTargetCorrectFiles() throws Exception {
FixReplacement fixReplacement = new FixReplacement(filePath1, new Range(1, 6, 3, 2), "Modified content");
FixReplacement fixReplacement2 = new FixReplacement(filePath1, new Range(3, 5, 3, 5), "Second modification");
mockFileContent(filePath1, "First line\nSecond line\nThird line\n");
FixReplacement fixReplacement3 = new FixReplacement(filePath2, new Range(2, 0, 3, 0), "Another modified content");
mockFileContent(filePath2, "1st line\n2nd line\n3rd line\n");
CommitModification commitModification = toCommitModification(fixReplacement, fixReplacement3, fixReplacement2);
List<TreeModification> sortedTreeModifications = getSortedCopy(commitModification.treeModifications());
assertThatList(sortedTreeModifications).element(0).asChangeFileContentModification().filePaths().containsExactly(filePath1);
assertThatList(sortedTreeModifications).element(0).asChangeFileContentModification().newContent().startsWith("First");
assertThatList(sortedTreeModifications).element(1).asChangeFileContentModification().filePaths().containsExactly(filePath2);
assertThatList(sortedTreeModifications).element(1).asChangeFileContentModification().newContent().startsWith("1st");
}
use of com.google.gerrit.server.edit.CommitModification in project gerrit by GerritCodeReview.
the class ApplyFix method apply.
@Override
public Response<EditInfo> apply(FixResource fixResource, Input nothing) throws AuthException, BadRequestException, ResourceConflictException, IOException, ResourceNotFoundException, PermissionBackendException {
RevisionResource revisionResource = fixResource.getRevisionResource();
Project.NameKey project = revisionResource.getProject();
ProjectState projectState = projectCache.get(project).orElseThrow(illegalState(project));
PatchSet patchSet = revisionResource.getPatchSet();
try (Repository repository = gitRepositoryManager.openRepository(project)) {
CommitModification commitModification = fixReplacementInterpreter.toCommitModification(repository, projectState, patchSet.commitId(), fixResource.getFixReplacements());
ChangeEdit changeEdit = changeEditModifier.combineWithModifiedPatchSetTree(repository, revisionResource.getNotes(), patchSet, commitModification);
return Response.ok(changeEditJson.toEditInfo(changeEdit, false));
} catch (InvalidChangeOperationException e) {
throw new ResourceConflictException(e.getMessage());
}
}
use of com.google.gerrit.server.edit.CommitModification in project gerrit by GerritCodeReview.
the class FixReplacementInterpreter method toCommitModification.
/**
* Transforms the given {@code FixReplacement}s into {@code TreeModification}s.
*
* @param repository the affected Git repository
* @param projectState the affected project
* @param patchSetCommitId the patch set which should be modified
* @param fixReplacements the replacements which should be applied
* @return a list of {@code TreeModification}s representing the given replacements
* @throws ResourceNotFoundException if a file to which one of the replacements refers doesn't
* exist
* @throws ResourceConflictException if the replacements can't be transformed into {@code
* TreeModification}s
*/
public CommitModification toCommitModification(Repository repository, ProjectState projectState, ObjectId patchSetCommitId, List<FixReplacement> fixReplacements) throws BadRequestException, ResourceNotFoundException, IOException, ResourceConflictException {
requireNonNull(fixReplacements, "Fix replacements must not be null");
Map<String, List<FixReplacement>> fixReplacementsPerFilePath = fixReplacements.stream().collect(groupingBy(fixReplacement -> fixReplacement.path));
CommitModification.Builder modificationBuilder = CommitModification.builder();
for (Map.Entry<String, List<FixReplacement>> entry : fixReplacementsPerFilePath.entrySet()) {
if (Objects.equals(entry.getKey(), Patch.COMMIT_MSG)) {
String newCommitMessage = getNewCommitMessage(repository, patchSetCommitId, entry.getValue());
modificationBuilder.newCommitMessage(newCommitMessage);
} else {
TreeModification treeModification = toTreeModification(repository, projectState, patchSetCommitId, entry.getKey(), entry.getValue());
modificationBuilder.addTreeModification(treeModification);
}
}
return modificationBuilder.build();
}
use of com.google.gerrit.server.edit.CommitModification in project gerrit by GerritCodeReview.
the class FixReplacementInterpreterTest method replacementsCanModifySeveralFilesInAnyOrder.
@Test
public void replacementsCanModifySeveralFilesInAnyOrder() throws Exception {
FixReplacement fixReplacement1 = new FixReplacement(filePath1, new Range(1, 1, 3, 2), "Modified content");
mockFileContent(filePath1, "First line\nSecond line\nThird line\n");
FixReplacement fixReplacement2 = new FixReplacement(filePath2, new Range(2, 0, 3, 0), "First modification\n");
FixReplacement fixReplacement3 = new FixReplacement(filePath2, new Range(3, 0, 4, 0), "Second modification\n");
mockFileContent(filePath2, "1st line\n2nd line\n3rd line\n");
CommitModification commitModification = toCommitModification(fixReplacement3, fixReplacement1, fixReplacement2);
List<TreeModification> sortedTreeModifications = getSortedCopy(commitModification.treeModifications());
assertThatList(sortedTreeModifications).element(0).asChangeFileContentModification().newContent().isEqualTo("FModified contentird line\n");
assertThatList(sortedTreeModifications).element(1).asChangeFileContentModification().newContent().isEqualTo("1st line\nFirst modification\nSecond modification\n");
}
use of com.google.gerrit.server.edit.CommitModification in project gerrit by GerritCodeReview.
the class FixReplacementInterpreterTest method noReplacementsResultInNoTreeModifications.
@Test
public void noReplacementsResultInNoTreeModifications() throws Exception {
CommitModification commitModification = toCommitModification();
assertThatList(commitModification.treeModifications()).isEmpty();
assertThat(commitModification.newCommitMessage()).isEmpty();
}
Aggregations