Fix compilation issue AddReviewerInput was renamed to ReviewerInput [I16aa48a6]. PatchListCache was replaced by DiffOperations [I9d64f29]. Bug: Issue 15855 Change-Id: I81eeadb9fd75452145e892bbb3b1b464d815d3d3
diff --git a/src/main/java/com/googlesource/gerrit/plugins/reviewersbyblame/ReviewersByBlame.java b/src/main/java/com/googlesource/gerrit/plugins/reviewersbyblame/ReviewersByBlame.java index 214c3ee..24121f4 100644 --- a/src/main/java/com/googlesource/gerrit/plugins/reviewersbyblame/ReviewersByBlame.java +++ b/src/main/java/com/googlesource/gerrit/plugins/reviewersbyblame/ReviewersByBlame.java
@@ -22,15 +22,16 @@ import com.google.gerrit.entities.Patch.ChangeType; import com.google.gerrit.entities.PatchSet; import com.google.gerrit.extensions.api.GerritApi; -import com.google.gerrit.extensions.api.changes.AddReviewerInput; import com.google.gerrit.extensions.api.changes.ReviewInput; +import com.google.gerrit.extensions.api.changes.ReviewerInput; import com.google.gerrit.server.account.AccountCache; import com.google.gerrit.server.account.AccountState; import com.google.gerrit.server.account.Emails; -import com.google.gerrit.server.patch.PatchList; -import com.google.gerrit.server.patch.PatchListCache; -import com.google.gerrit.server.patch.PatchListEntry; -import com.google.gerrit.server.patch.PatchListNotAvailableException; +import com.google.gerrit.server.patch.DiffNotAvailableException; +import com.google.gerrit.server.patch.DiffOperations; +import com.google.gerrit.server.patch.DiffOptions; +import com.google.gerrit.server.patch.filediff.FileDiffOutput; +import com.google.gerrit.server.patch.filediff.TaggedEdit; import com.google.inject.Inject; import com.google.inject.assistedinject.Assisted; import java.io.IOException; @@ -41,6 +42,7 @@ import java.util.Map.Entry; import java.util.Optional; import java.util.Set; +import java.util.stream.Collectors; import org.eclipse.jgit.api.BlameCommand; import org.eclipse.jgit.api.errors.GitAPIException; import org.eclipse.jgit.blame.BlameResult; @@ -53,6 +55,7 @@ public class ReviewersByBlame implements Runnable { private static final Logger log = LoggerFactory.getLogger(ReviewersByBlame.class); + private static final int DEFAULT_BASE = 0; private final RevCommit commit; private final Change change; @@ -63,7 +66,7 @@ private final Emails emails; private final AccountCache accountCache; - private final PatchListCache patchListCache; + private final DiffOperations diffOperations; private final GerritApi gApi; public interface Factory { @@ -80,7 +83,7 @@ public ReviewersByBlame( Emails emails, AccountCache accountCache, - PatchListCache patchListCache, + DiffOperations diffOperations, GerritApi gApi, @Assisted RevCommit commit, @Assisted Change change, @@ -90,7 +93,7 @@ @Assisted String ignoreFileRegEx) { this.emails = emails; this.accountCache = accountCache; - this.patchListCache = patchListCache; + this.diffOperations = diffOperations; this.gApi = gApi; this.commit = commit; this.change = change; @@ -103,10 +106,13 @@ @Override public void run() { Map<Account, Integer> reviewers = Maps.newHashMap(); - PatchList patchList; + Map<String, FileDiffOutput> stringFileDiffOutputMap; try { - patchList = patchListCache.get(change, ps); - } catch (PatchListNotAvailableException ex) { + stringFileDiffOutputMap = + diffOperations.listModifiedFilesAgainstParent( + change.getProject(), ps.commitId(), DEFAULT_BASE, DiffOptions.DEFAULTS); + + } catch (DiffNotAvailableException ex) { log.error("Couldn't load patchlist for change {}", change.getKey(), ex); return; } @@ -114,13 +120,13 @@ if (commit.getParentCount() != 1) { return; } - for (PatchListEntry entry : patchList.getPatches()) { + for (FileDiffOutput entry : stringFileDiffOutputMap.values()) { BlameResult blameResult; - if ((entry.getChangeType() == ChangeType.MODIFIED - || entry.getChangeType() == ChangeType.DELETED) - && (ignoreFileRegEx.isEmpty() || !entry.getNewName().matches(ignoreFileRegEx)) + if ((entry.changeType() == ChangeType.MODIFIED || entry.changeType() == ChangeType.DELETED) + && (ignoreFileRegEx.isEmpty() || !entry.newPath().orElse("").matches(ignoreFileRegEx)) && (blameResult = computeBlame(entry, commit.getParent(0))) != null) { - List<Edit> edits = entry.getEdits(); + List<Edit> edits = + entry.edits().stream().map(TaggedEdit::jgitEdit).collect(Collectors.toList()); reviewers.putAll(getReviewersForPatch(edits, blameResult)); } } @@ -139,9 +145,9 @@ ReviewInput in = new ReviewInput(); in.reviewers = new ArrayList<>(topReviewers.size()); for (Account.Id account : topReviewers) { - AddReviewerInput addReviewerInput = new AddReviewerInput(); - addReviewerInput.reviewer = account.toString(); - in.reviewers.add(addReviewerInput); + ReviewerInput reviewerInput = new ReviewerInput(); + reviewerInput.reviewer = account.toString(); + in.reviewers.add(reviewerInput); } gApi.changes().id(change.getChangeId()).current().review(in); } catch (Exception ex) { @@ -212,14 +218,22 @@ * Compute the blame data for the parent, we are not interested in the specific commit but the * parent, since we only want to know the last person that edited this specific part of the code. * - * @param entry {@link PatchListEntry} + * @param entry {@link FileDiffOutput} * @param parent Parent {@link RevCommit} * @return Result of blame computation, null if the computation fails */ - private BlameResult computeBlame(PatchListEntry entry, RevCommit parent) { + private BlameResult computeBlame(FileDiffOutput entry, RevCommit parent) { + if (entry.changeType() == ChangeType.ADDED) { + // This file is not present in the parent commit, so there is no blame + return null; + } + + // For all other ChangeType cases this file exists in the old commit and therefore we + // use the entry.oldPath() which is then guaranteed to be set + BlameCommand blameCommand = new BlameCommand(repo); blameCommand.setStartCommit(parent); - blameCommand.setFilePath(entry.getNewName()); + blameCommand.setFilePath(entry.oldPath().get()); try { BlameResult blameResult = blameCommand.call(); blameResult.computeAll();