Fix NPE in uploadvalidator Change I4081375e033 migrated uploadvalidator to use DiffOperations. The implementation uses two different sources: 1) CommitUtils#getChangedContent to get the changed files (regular, executable) and their content. 2) DiffOperations (previously PatchListCache) to get the list of edits. We loop on changed files using the file paths of [1], but we check the edits from [2]. We need to check if the file path exists in DiffOperation's output map. Sometimes DiffOperations skips some files, for example if files that were due to rebase. Bug: Google b/203216894 Change-Id: Ib6dce965de5a2e79b28e1a6f76c36c8049734020
diff --git a/src/main/java/com/googlesource/gerrit/plugins/uploadvalidator/BlockedKeywordValidator.java b/src/main/java/com/googlesource/gerrit/plugins/uploadvalidator/BlockedKeywordValidator.java index a229ee6..20fa0f8 100644 --- a/src/main/java/com/googlesource/gerrit/plugins/uploadvalidator/BlockedKeywordValidator.java +++ b/src/main/java/com/googlesource/gerrit/plugins/uploadvalidator/BlockedKeywordValidator.java
@@ -238,6 +238,9 @@ continue; } } + if (!fileDiffs.containsKey(path)) { + continue; + } checkLineDiffForBlockedKeywords( fileDiffs.get(path).edits().stream() .map(TaggedEdit::jgitEdit)
diff --git a/src/test/java/com/googlesource/gerrit/plugins/uploadvalidator/BlockedKeywordValidatorTest.java b/src/test/java/com/googlesource/gerrit/plugins/uploadvalidator/BlockedKeywordValidatorTest.java index c4d0ac4..96d7e90 100644 --- a/src/test/java/com/googlesource/gerrit/plugins/uploadvalidator/BlockedKeywordValidatorTest.java +++ b/src/test/java/com/googlesource/gerrit/plugins/uploadvalidator/BlockedKeywordValidatorTest.java
@@ -95,6 +95,7 @@ Edit.create(0, 0, 0, numberOfLinesInString(fileContent.getValue())), /* dueToRebase = */ false))); when(mockDiffs.get(fileContent.getKey())).thenReturn(file); + when(mockDiffs.containsKey(fileContent.getKey())).thenReturn(true); } try (RevWalk rw = new RevWalk(repo)) {