Search in sources :

Example 1 with CommitModification

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");
}
Also used : CommitModification(com.google.gerrit.server.edit.CommitModification) TreeModification(com.google.gerrit.server.edit.tree.TreeModification) FixReplacement(com.google.gerrit.entities.FixReplacement) Range(com.google.gerrit.entities.Comment.Range) Test(org.junit.Test)

Example 2 with CommitModification

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());
    }
}
Also used : Project(com.google.gerrit.entities.Project) InvalidChangeOperationException(com.google.gerrit.server.project.InvalidChangeOperationException) Repository(org.eclipse.jgit.lib.Repository) ChangeEdit(com.google.gerrit.server.edit.ChangeEdit) ResourceConflictException(com.google.gerrit.extensions.restapi.ResourceConflictException) CommitModification(com.google.gerrit.server.edit.CommitModification) RevisionResource(com.google.gerrit.server.change.RevisionResource) ProjectState(com.google.gerrit.server.project.ProjectState) PatchSet(com.google.gerrit.entities.PatchSet)

Example 3 with CommitModification

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();
}
Also used : ResourceNotFoundException(com.google.gerrit.extensions.restapi.ResourceNotFoundException) Patch(com.google.gerrit.entities.Patch) Inject(com.google.inject.Inject) Collectors.groupingBy(java.util.stream.Collectors.groupingBy) RawInputUtil(com.google.gerrit.common.RawInputUtil) ChangeFileContentModification(com.google.gerrit.server.edit.tree.ChangeFileContentModification) BadRequestException(com.google.gerrit.extensions.restapi.BadRequestException) BinaryResult(com.google.gerrit.extensions.restapi.BinaryResult) ImmutableList(com.google.common.collect.ImmutableList) FileContentUtil(com.google.gerrit.server.change.FileContentUtil) MagicFile(com.google.gerrit.server.patch.MagicFile) Objects.requireNonNull(java.util.Objects.requireNonNull) Map(java.util.Map) FixReplacement(com.google.gerrit.entities.FixReplacement) TreeModification(com.google.gerrit.server.edit.tree.TreeModification) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) ProjectState(com.google.gerrit.server.project.ProjectState) IOException(java.io.IOException) ObjectId(org.eclipse.jgit.lib.ObjectId) Objects(java.util.Objects) CommitModification(com.google.gerrit.server.edit.CommitModification) List(java.util.List) ResourceConflictException(com.google.gerrit.extensions.restapi.ResourceConflictException) ObjectReader(org.eclipse.jgit.lib.ObjectReader) Repository(org.eclipse.jgit.lib.Repository) Range(com.google.gerrit.entities.Comment.Range) Singleton(com.google.inject.Singleton) CommitModification(com.google.gerrit.server.edit.CommitModification) ImmutableList(com.google.common.collect.ImmutableList) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) List(java.util.List) TreeModification(com.google.gerrit.server.edit.tree.TreeModification) Map(java.util.Map)

Example 4 with CommitModification

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");
}
Also used : CommitModification(com.google.gerrit.server.edit.CommitModification) TreeModification(com.google.gerrit.server.edit.tree.TreeModification) FixReplacement(com.google.gerrit.entities.FixReplacement) Range(com.google.gerrit.entities.Comment.Range) Test(org.junit.Test)

Example 5 with CommitModification

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();
}
Also used : CommitModification(com.google.gerrit.server.edit.CommitModification) Test(org.junit.Test)

Aggregations

CommitModification (com.google.gerrit.server.edit.CommitModification)6 Range (com.google.gerrit.entities.Comment.Range)4 FixReplacement (com.google.gerrit.entities.FixReplacement)4 TreeModification (com.google.gerrit.server.edit.tree.TreeModification)4 Test (org.junit.Test)4 ResourceConflictException (com.google.gerrit.extensions.restapi.ResourceConflictException)2 ProjectState (com.google.gerrit.server.project.ProjectState)2 Repository (org.eclipse.jgit.lib.Repository)2 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)1 RawInputUtil (com.google.gerrit.common.RawInputUtil)1 Patch (com.google.gerrit.entities.Patch)1 PatchSet (com.google.gerrit.entities.PatchSet)1 Project (com.google.gerrit.entities.Project)1 BadRequestException (com.google.gerrit.extensions.restapi.BadRequestException)1 BinaryResult (com.google.gerrit.extensions.restapi.BinaryResult)1 ResourceNotFoundException (com.google.gerrit.extensions.restapi.ResourceNotFoundException)1 FileContentUtil (com.google.gerrit.server.change.FileContentUtil)1 RevisionResource (com.google.gerrit.server.change.RevisionResource)1 ChangeEdit (com.google.gerrit.server.edit.ChangeEdit)1