Fix code owner status if global owner is a reviewer If a global code owner is a reviewer the code owner status is at least PENDING. Signed-off-by: Edwin Kempin <ekempin@google.com> Change-Id: I2f1b5c1605b24fa3825b37bca9424d63bcd5e5a9
diff --git a/java/com/google/gerrit/plugins/codeowners/backend/CodeOwnerApprovalCheck.java b/java/com/google/gerrit/plugins/codeowners/backend/CodeOwnerApprovalCheck.java index e5a2a39..e7d6acd 100644 --- a/java/com/google/gerrit/plugins/codeowners/backend/CodeOwnerApprovalCheck.java +++ b/java/com/google/gerrit/plugins/codeowners/backend/CodeOwnerApprovalCheck.java
@@ -353,7 +353,8 @@ enableImplicitApprovalFromUploader, patchSetUploader)) { codeOwnerStatus = CodeOwnerStatus.APPROVED; - } else if (isPendingBootstrappingMode(branch.project(), absolutePath, reviewerAccountIds)) { + } else if (isPendingBootstrappingMode( + branch.project(), absolutePath, globalCodeOwnerAccountIds, reviewerAccountIds)) { codeOwnerStatus = CodeOwnerStatus.PENDING; } @@ -420,7 +421,10 @@ } private boolean isPendingBootstrappingMode( - Project.NameKey projectName, Path absolutePath, ImmutableSet<Account.Id> reviewerAccountIds) { + Project.NameKey projectName, + Path absolutePath, + ImmutableSet<Account.Id> globalCodeOwnerAccountIds, + ImmutableSet<Account.Id> reviewerAccountIds) { if (reviewerAccountIds.stream() .anyMatch(reviewerAccountId -> isProjectOwner(projectName, reviewerAccountId))) { // At least one of the reviewers is a project owner and thus a code owner. @@ -428,7 +432,11 @@ return true; } - // TODO: check if a global code owner is a reviewer + if (isPending(absolutePath, globalCodeOwnerAccountIds, reviewerAccountIds)) { + // At least one of the reviewers is a global code owner. + logger.atFine().log("%s is owned by a reviewer who is a global owner", absolutePath); + return true; + } return false; } @@ -462,7 +470,10 @@ } else { logger.atFine().log("%s was not approved by a global code owner", absolutePath); - // TODO check if a global code owner is a reviewer + if (isPending(absolutePath, globalCodeOwnerAccountIds, reviewerAccountIds)) { + logger.atFine().log("%s is owned by a reviewer who is a global owner", absolutePath); + codeOwnerStatus.set(CodeOwnerStatus.PENDING); + } codeOwnerConfigHierarchy.visit( branch,
diff --git a/javatests/com/google/gerrit/plugins/codeowners/backend/CodeOwnerApprovalCheckTest.java b/javatests/com/google/gerrit/plugins/codeowners/backend/CodeOwnerApprovalCheckTest.java index 90d9500..69e5f97 100644 --- a/javatests/com/google/gerrit/plugins/codeowners/backend/CodeOwnerApprovalCheckTest.java +++ b/javatests/com/google/gerrit/plugins/codeowners/backend/CodeOwnerApprovalCheckTest.java
@@ -1034,6 +1034,86 @@ } @Test + @GerritConfig(name = "plugin.code-owners.globalCodeOwner", value = "bot@example.com") + public void globalCodeOwnerAsReviewer() throws Exception { + testGlobalCodeOwnerAsReviewer(false); + } + + @Test + @GerritConfig(name = "plugin.code-owners.globalCodeOwner", value = "bot@example.com") + public void globalCodeOwnerAsReviewer_bootstrappingMode() throws Exception { + testGlobalCodeOwnerAsReviewer(true); + } + + private void testGlobalCodeOwnerAsReviewer(boolean bootstrappingMode) throws Exception { + // Create a bot user that is a global code owner. + TestAccount bot = accountCreator.create("bot", "bot@example.com", "Bot", null); + + if (!bootstrappingMode) { + // Create a code owner config file so that we are not in the bootstrapping mode. + codeOwnerConfigOperations + .newCodeOwnerConfig() + .project(project) + .branch("master") + .folderPath("/foo/") + .addCodeOwnerEmail(admin.email()) + .create(); + } + + // Create a change as a user that is not a code owner. + Path path = Paths.get("/foo/bar.baz"); + String changeId = + createChange(user, "Change Adding A File", JgitPath.of(path).get(), "file content") + .getChangeId(); + + // Verify that the status of the file is INSUFFICIENT_REVIEWERS. + Stream<FileCodeOwnerStatus> fileCodeOwnerStatuses = + codeOwnerApprovalCheck.getFileStatuses(getChangeNotes(changeId)); + FileCodeOwnerStatusSubject fileCodeOwnerStatusSubject = + assertThatStream(fileCodeOwnerStatuses).onlyElement(); + fileCodeOwnerStatusSubject.hasNewPathStatus().value().hasPathThat().isEqualTo(path); + fileCodeOwnerStatusSubject + .hasNewPathStatus() + .value() + .hasStatusThat() + .isEqualTo(CodeOwnerStatus.INSUFFICIENT_REVIEWERS); + + // Add the bot approve as reviewer. + gApi.changes().id(changeId).addReviewer(bot.email()); + + // Check that the status of the file is PENDING now. + requestScopeOperations.setApiUser(admin.id()); + fileCodeOwnerStatuses = codeOwnerApprovalCheck.getFileStatuses(getChangeNotes(changeId)); + fileCodeOwnerStatusSubject = assertThatStream(fileCodeOwnerStatuses).onlyElement(); + fileCodeOwnerStatusSubject.hasNewPathStatus().value().hasPathThat().isEqualTo(path); + fileCodeOwnerStatusSubject + .hasNewPathStatus() + .value() + .hasStatusThat() + .isEqualTo(CodeOwnerStatus.PENDING); + + // Let the bot approve the change. + projectOperations + .project(project) + .forUpdate() + .add(allowLabel("Code-Review").ref("refs/heads/*").group(REGISTERED_USERS).range(-2, +2)) + .update(); + requestScopeOperations.setApiUser(bot.id()); + approve(changeId); + + // Check that the file is approved now. + requestScopeOperations.setApiUser(admin.id()); + fileCodeOwnerStatuses = codeOwnerApprovalCheck.getFileStatuses(getChangeNotes(changeId)); + fileCodeOwnerStatusSubject = assertThatStream(fileCodeOwnerStatuses).onlyElement(); + fileCodeOwnerStatusSubject.hasNewPathStatus().value().hasPathThat().isEqualTo(path); + fileCodeOwnerStatusSubject + .hasNewPathStatus() + .value() + .hasStatusThat() + .isEqualTo(CodeOwnerStatus.APPROVED); + } + + @Test public void parentCodeOwnerConfigsAreConsidered() throws Exception { TestAccount user2 = accountCreator.user2(); TestAccount user3 = accountCreator.create("user3", "user3@example.com", "User3", null);