Edwin Kempin | ef03eda | 2020-12-02 13:26:36 +0100 | [diff] [blame] | 1 | // Copyright (C) 2020 The Android Open Source Project |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package com.google.gerrit.plugins.codeowners.backend; |
| 16 | |
| 17 | import static com.google.gerrit.plugins.codeowners.testing.FileCodeOwnerStatusSubject.assertThatStream; |
| 18 | |
Edwin Kempin | ffe1d0f | 2021-01-20 14:03:06 +0100 | [diff] [blame] | 19 | import com.google.common.collect.ImmutableMap; |
Chris Poucet | 73b98e9 | 2022-10-14 15:26:48 +0200 | [diff] [blame] | 20 | import com.google.common.collect.ImmutableSet; |
Edwin Kempin | ffe1d0f | 2021-01-20 14:03:06 +0100 | [diff] [blame] | 21 | import com.google.gerrit.acceptance.PushOneCommit; |
Edwin Kempin | ef03eda | 2020-12-02 13:26:36 +0100 | [diff] [blame] | 22 | import com.google.gerrit.acceptance.TestAccount; |
| 23 | import com.google.gerrit.acceptance.config.GerritConfig; |
| 24 | import com.google.gerrit.acceptance.testsuite.request.RequestScopeOperations; |
Edwin Kempin | ef03eda | 2020-12-02 13:26:36 +0100 | [diff] [blame] | 25 | import com.google.gerrit.entities.Change; |
Edwin Kempin | ffe1d0f | 2021-01-20 14:03:06 +0100 | [diff] [blame] | 26 | import com.google.gerrit.entities.PatchSet; |
Edwin Kempin | ef03eda | 2020-12-02 13:26:36 +0100 | [diff] [blame] | 27 | import com.google.gerrit.plugins.codeowners.acceptance.AbstractCodeOwnersTest; |
| 28 | import com.google.gerrit.plugins.codeowners.acceptance.testsuite.CodeOwnerConfigOperations; |
Edwin Kempin | 47d5870 | 2021-01-07 16:06:08 +0100 | [diff] [blame] | 29 | import com.google.gerrit.plugins.codeowners.common.CodeOwnerStatus; |
Edwin Kempin | ef03eda | 2020-12-02 13:26:36 +0100 | [diff] [blame] | 30 | import com.google.gerrit.plugins.codeowners.testing.FileCodeOwnerStatusSubject; |
Edwin Kempin | b4e832a | 2021-01-07 15:22:12 +0100 | [diff] [blame] | 31 | import com.google.gerrit.plugins.codeowners.util.JgitPath; |
Edwin Kempin | ef03eda | 2020-12-02 13:26:36 +0100 | [diff] [blame] | 32 | import com.google.gerrit.server.notedb.ChangeNotes; |
Edwin Kempin | ffe1d0f | 2021-01-20 14:03:06 +0100 | [diff] [blame] | 33 | import com.google.gerrit.truth.ListSubject; |
Edwin Kempin | ef03eda | 2020-12-02 13:26:36 +0100 | [diff] [blame] | 34 | import com.google.inject.Inject; |
| 35 | import java.nio.file.Path; |
| 36 | import java.nio.file.Paths; |
| 37 | import java.util.stream.Stream; |
| 38 | import org.junit.Before; |
| 39 | import org.junit.Test; |
| 40 | |
Edwin Kempin | ffe1d0f | 2021-01-20 14:03:06 +0100 | [diff] [blame] | 41 | /** |
Chris Poucet | 73b98e9 | 2022-10-14 15:26:48 +0200 | [diff] [blame] | 42 | * Tests for {@link CodeOwnerApprovalCheck#getFileStatusesForAccounts(ChangeNotes, PatchSet, |
| 43 | * ImmutableSet)}. |
Edwin Kempin | ffe1d0f | 2021-01-20 14:03:06 +0100 | [diff] [blame] | 44 | */ |
Edwin Kempin | ef03eda | 2020-12-02 13:26:36 +0100 | [diff] [blame] | 45 | public class CodeOwnerApprovalCheckForAccountTest extends AbstractCodeOwnersTest { |
| 46 | @Inject private ChangeNotes.Factory changeNotesFactory; |
| 47 | @Inject private RequestScopeOperations requestScopeOperations; |
| 48 | |
| 49 | private CodeOwnerApprovalCheck codeOwnerApprovalCheck; |
| 50 | private CodeOwnerConfigOperations codeOwnerConfigOperations; |
| 51 | |
| 52 | @Before |
| 53 | public void setUpCodeOwnersPlugin() throws Exception { |
| 54 | codeOwnerApprovalCheck = plugin.getSysInjector().getInstance(CodeOwnerApprovalCheck.class); |
| 55 | codeOwnerConfigOperations = |
| 56 | plugin.getSysInjector().getInstance(CodeOwnerConfigOperations.class); |
| 57 | } |
| 58 | |
| 59 | @Test |
| 60 | public void notApprovedByUser() throws Exception { |
Edwin Kempin | ef03eda | 2020-12-02 13:26:36 +0100 | [diff] [blame] | 61 | Path path = Paths.get("/foo/bar.baz"); |
| 62 | String changeId = |
| 63 | createChange("Change Adding A File", JgitPath.of(path).get(), "file content").getChangeId(); |
Edwin Kempin | ffe1d0f | 2021-01-20 14:03:06 +0100 | [diff] [blame] | 64 | ChangeNotes changeNotes = getChangeNotes(changeId); |
Edwin Kempin | ef03eda | 2020-12-02 13:26:36 +0100 | [diff] [blame] | 65 | |
| 66 | // Verify that the file would not be approved by the user. |
| 67 | Stream<FileCodeOwnerStatus> fileCodeOwnerStatuses = |
Chris Poucet | 73b98e9 | 2022-10-14 15:26:48 +0200 | [diff] [blame] | 68 | codeOwnerApprovalCheck.getFileStatusesForAccounts( |
| 69 | changeNotes, changeNotes.getCurrentPatchSet(), ImmutableSet.of(user.id())); |
Edwin Kempin | ef03eda | 2020-12-02 13:26:36 +0100 | [diff] [blame] | 70 | FileCodeOwnerStatusSubject fileCodeOwnerStatusSubject = |
| 71 | assertThatStream(fileCodeOwnerStatuses).onlyElement(); |
| 72 | fileCodeOwnerStatusSubject.hasNewPathStatus().value().hasPathThat().isEqualTo(path); |
| 73 | fileCodeOwnerStatusSubject |
| 74 | .hasNewPathStatus() |
| 75 | .value() |
| 76 | .hasStatusThat() |
| 77 | .isEqualTo(CodeOwnerStatus.INSUFFICIENT_REVIEWERS); |
Chris Poucet | 73b98e9 | 2022-10-14 15:26:48 +0200 | [diff] [blame] | 78 | fileCodeOwnerStatusSubject.hasNewPathStatus().value().hasOwnersThat().isEmpty(); |
Edwin Kempin | ef03eda | 2020-12-02 13:26:36 +0100 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | @Test |
| 82 | public void approvalFromOtherCodeOwnerHasNoEffect() throws Exception { |
| 83 | TestAccount codeOwner = |
| 84 | accountCreator.create( |
| 85 | "codeOwner", "codeOwner@example.com", "CodeOwner", /* displayName= */ null); |
| 86 | |
| 87 | codeOwnerConfigOperations |
| 88 | .newCodeOwnerConfig() |
| 89 | .project(project) |
| 90 | .branch("master") |
| 91 | .folderPath("/foo/") |
| 92 | .addCodeOwnerEmail(codeOwner.email()) |
| 93 | .create(); |
| 94 | |
| 95 | Path path = Paths.get("/foo/bar.baz"); |
| 96 | String changeId = |
| 97 | createChange("Change Adding A File", JgitPath.of(path).get(), "file content").getChangeId(); |
Edwin Kempin | ffe1d0f | 2021-01-20 14:03:06 +0100 | [diff] [blame] | 98 | ChangeNotes changeNotes = getChangeNotes(changeId); |
Edwin Kempin | ef03eda | 2020-12-02 13:26:36 +0100 | [diff] [blame] | 99 | |
| 100 | // Add a Code-Review+1 (= code owner approval) from the code owner. |
| 101 | requestScopeOperations.setApiUser(codeOwner.id()); |
| 102 | recommend(changeId); |
| 103 | |
| 104 | // Verify that the file would not be approved by the user. |
| 105 | Stream<FileCodeOwnerStatus> fileCodeOwnerStatuses = |
Chris Poucet | 73b98e9 | 2022-10-14 15:26:48 +0200 | [diff] [blame] | 106 | codeOwnerApprovalCheck.getFileStatusesForAccounts( |
| 107 | changeNotes, changeNotes.getCurrentPatchSet(), ImmutableSet.of(user.id())); |
Edwin Kempin | ef03eda | 2020-12-02 13:26:36 +0100 | [diff] [blame] | 108 | FileCodeOwnerStatusSubject fileCodeOwnerStatusSubject = |
| 109 | assertThatStream(fileCodeOwnerStatuses).onlyElement(); |
| 110 | fileCodeOwnerStatusSubject.hasNewPathStatus().value().hasPathThat().isEqualTo(path); |
| 111 | fileCodeOwnerStatusSubject |
| 112 | .hasNewPathStatus() |
| 113 | .value() |
| 114 | .hasStatusThat() |
| 115 | .isEqualTo(CodeOwnerStatus.INSUFFICIENT_REVIEWERS); |
Chris Poucet | 73b98e9 | 2022-10-14 15:26:48 +0200 | [diff] [blame] | 116 | fileCodeOwnerStatusSubject.hasNewPathStatus().value().hasOwnersThat().isEmpty(); |
| 117 | } |
| 118 | |
| 119 | @Test |
| 120 | public void approvalFromOtherCodeOwnerAsReviewerIsReturned() throws Exception { |
| 121 | TestAccount codeOwner = |
| 122 | accountCreator.create( |
| 123 | "codeOwner", "codeOwner@example.com", "CodeOwner", /* displayName= */ null); |
| 124 | |
| 125 | codeOwnerConfigOperations |
| 126 | .newCodeOwnerConfig() |
| 127 | .project(project) |
| 128 | .branch("master") |
| 129 | .folderPath("/foo/") |
| 130 | .addCodeOwnerEmail(codeOwner.email()) |
| 131 | .create(); |
| 132 | |
| 133 | Path path = Paths.get("/foo/bar.baz"); |
| 134 | String changeId = |
| 135 | createChange("Change Adding A File", JgitPath.of(path).get(), "file content").getChangeId(); |
| 136 | ChangeNotes changeNotes = getChangeNotes(changeId); |
| 137 | |
| 138 | // Add a Code-Review+1 (= code owner approval) from the code owner. |
| 139 | requestScopeOperations.setApiUser(codeOwner.id()); |
| 140 | recommend(changeId); |
| 141 | |
| 142 | // Verify that the file would not be approved by the user. |
| 143 | Stream<FileCodeOwnerStatus> fileCodeOwnerStatuses = |
| 144 | codeOwnerApprovalCheck.getFileStatusesForAccounts( |
| 145 | changeNotes, |
| 146 | changeNotes.getCurrentPatchSet(), |
| 147 | ImmutableSet.of(codeOwner.id(), user.id())); |
| 148 | FileCodeOwnerStatusSubject fileCodeOwnerStatusSubject = |
| 149 | assertThatStream(fileCodeOwnerStatuses).onlyElement(); |
| 150 | fileCodeOwnerStatusSubject.hasNewPathStatus().value().hasPathThat().isEqualTo(path); |
| 151 | fileCodeOwnerStatusSubject |
| 152 | .hasNewPathStatus() |
| 153 | .value() |
| 154 | .hasStatusThat() |
| 155 | .isEqualTo(CodeOwnerStatus.APPROVED); |
| 156 | fileCodeOwnerStatusSubject.hasNewPathStatus().value().hasOwners(codeOwner); |
| 157 | } |
| 158 | |
| 159 | @Test |
| 160 | @GerritConfig(name = "plugin.code-owners.globalCodeOwner", value = "globalCodeOwner@example.com") |
| 161 | public void approvalFromGlobalCodeOwnerAsReviewerIsReturned() throws Exception { |
| 162 | TestAccount codeOwner = |
| 163 | accountCreator.create( |
| 164 | "codeOwner", "codeOwner@example.com", "CodeOwner", /* displayName= */ null); |
| 165 | TestAccount globalCodeOwner = |
| 166 | accountCreator.create( |
| 167 | "globalCodeOwner", |
| 168 | "globalCodeOwner@example.com", |
| 169 | "GlobalCodeOwner", |
| 170 | /* displayName= */ null); |
| 171 | codeOwnerConfigOperations |
| 172 | .newCodeOwnerConfig() |
| 173 | .project(project) |
| 174 | .branch("master") |
| 175 | .folderPath("/foo/") |
| 176 | .addCodeOwnerEmail(codeOwner.email()) |
| 177 | .create(); |
| 178 | |
| 179 | Path path = Paths.get("/foo/bar.baz"); |
| 180 | String changeId = |
| 181 | createChange("Change Adding A File", JgitPath.of(path).get(), "file content").getChangeId(); |
| 182 | ChangeNotes changeNotes = getChangeNotes(changeId); |
| 183 | |
| 184 | // Add a Code-Review+1 (= code owner approval) from the code owner. |
| 185 | requestScopeOperations.setApiUser(codeOwner.id()); |
| 186 | recommend(changeId); |
| 187 | |
| 188 | Stream<FileCodeOwnerStatus> fileCodeOwnerStatuses = |
| 189 | codeOwnerApprovalCheck.getFileStatusesForAccounts( |
| 190 | changeNotes, |
| 191 | changeNotes.getCurrentPatchSet(), |
| 192 | ImmutableSet.of(codeOwner.id(), user.id(), globalCodeOwner.id())); |
| 193 | FileCodeOwnerStatusSubject fileCodeOwnerStatusSubject = |
| 194 | assertThatStream(fileCodeOwnerStatuses).onlyElement(); |
| 195 | fileCodeOwnerStatusSubject.hasNewPathStatus().value().hasPathThat().isEqualTo(path); |
| 196 | fileCodeOwnerStatusSubject |
| 197 | .hasNewPathStatus() |
| 198 | .value() |
| 199 | .hasStatusThat() |
| 200 | .isEqualTo(CodeOwnerStatus.APPROVED); |
| 201 | fileCodeOwnerStatusSubject.hasNewPathStatus().value().hasOwners(codeOwner, globalCodeOwner); |
Edwin Kempin | ef03eda | 2020-12-02 13:26:36 +0100 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | @Test |
| 205 | public void approvedByUser() throws Exception { |
| 206 | codeOwnerConfigOperations |
| 207 | .newCodeOwnerConfig() |
| 208 | .project(project) |
| 209 | .branch("master") |
| 210 | .folderPath("/foo/") |
| 211 | .addCodeOwnerEmail(user.email()) |
| 212 | .create(); |
| 213 | |
| 214 | Path path = Paths.get("/foo/bar.baz"); |
| 215 | String changeId = |
| 216 | createChange("Change Adding A File", JgitPath.of(path).get(), "file content").getChangeId(); |
Edwin Kempin | ffe1d0f | 2021-01-20 14:03:06 +0100 | [diff] [blame] | 217 | ChangeNotes changeNotes = getChangeNotes(changeId); |
Edwin Kempin | ef03eda | 2020-12-02 13:26:36 +0100 | [diff] [blame] | 218 | |
| 219 | // Verify that the file would be approved by the user. |
| 220 | Stream<FileCodeOwnerStatus> fileCodeOwnerStatuses = |
Chris Poucet | 73b98e9 | 2022-10-14 15:26:48 +0200 | [diff] [blame] | 221 | codeOwnerApprovalCheck.getFileStatusesForAccounts( |
| 222 | changeNotes, changeNotes.getCurrentPatchSet(), ImmutableSet.of(user.id())); |
Edwin Kempin | ef03eda | 2020-12-02 13:26:36 +0100 | [diff] [blame] | 223 | FileCodeOwnerStatusSubject fileCodeOwnerStatusSubject = |
| 224 | assertThatStream(fileCodeOwnerStatuses).onlyElement(); |
| 225 | fileCodeOwnerStatusSubject.hasNewPathStatus().value().hasPathThat().isEqualTo(path); |
| 226 | fileCodeOwnerStatusSubject |
| 227 | .hasNewPathStatus() |
| 228 | .value() |
| 229 | .hasStatusThat() |
| 230 | .isEqualTo(CodeOwnerStatus.APPROVED); |
Chris Poucet | 73b98e9 | 2022-10-14 15:26:48 +0200 | [diff] [blame] | 231 | fileCodeOwnerStatusSubject.hasNewPathStatus().value().hasOwners(user); |
Edwin Kempin | ef03eda | 2020-12-02 13:26:36 +0100 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | @Test |
Edwin Kempin | ffe1d0f | 2021-01-20 14:03:06 +0100 | [diff] [blame] | 235 | public void approvedByUser_forPatchSets() throws Exception { |
| 236 | codeOwnerConfigOperations |
| 237 | .newCodeOwnerConfig() |
| 238 | .project(project) |
| 239 | .branch("master") |
| 240 | .folderPath("/foo/") |
| 241 | .addCodeOwnerEmail(user.email()) |
| 242 | .create(); |
| 243 | |
| 244 | Path path1 = Paths.get("/foo/bar.baz"); |
| 245 | String changeId = |
| 246 | createChange("Change Adding A File", JgitPath.of(path1).get(), "file content") |
| 247 | .getChangeId(); |
| 248 | |
| 249 | // amend change and add another file |
| 250 | Path path2 = Paths.get("/foo/baz.bar"); |
| 251 | PushOneCommit push = |
| 252 | pushFactory.create( |
| 253 | admin.newIdent(), |
| 254 | testRepo, |
| 255 | "subject", |
| 256 | ImmutableMap.of( |
| 257 | JgitPath.of(path1).get(), "file content", JgitPath.of(path2).get(), "file content"), |
| 258 | changeId); |
| 259 | push.to("refs/for/master").assertOkStatus(); |
| 260 | |
| 261 | ChangeNotes changeNotes = getChangeNotes(changeId); |
| 262 | |
| 263 | // Verify that the file in patch set 1 would be approved by the user. |
| 264 | Stream<FileCodeOwnerStatus> fileCodeOwnerStatuses = |
Chris Poucet | 73b98e9 | 2022-10-14 15:26:48 +0200 | [diff] [blame] | 265 | codeOwnerApprovalCheck.getFileStatusesForAccounts( |
Edwin Kempin | ffe1d0f | 2021-01-20 14:03:06 +0100 | [diff] [blame] | 266 | changeNotes, |
| 267 | changeNotes.getPatchSets().get(PatchSet.id(changeNotes.getChangeId(), 1)), |
Chris Poucet | 73b98e9 | 2022-10-14 15:26:48 +0200 | [diff] [blame] | 268 | ImmutableSet.of(user.id())); |
Edwin Kempin | ffe1d0f | 2021-01-20 14:03:06 +0100 | [diff] [blame] | 269 | FileCodeOwnerStatusSubject fileCodeOwnerStatusSubject = |
| 270 | assertThatStream(fileCodeOwnerStatuses).onlyElement(); |
| 271 | fileCodeOwnerStatusSubject.hasNewPathStatus().value().hasPathThat().isEqualTo(path1); |
| 272 | fileCodeOwnerStatusSubject |
| 273 | .hasNewPathStatus() |
| 274 | .value() |
| 275 | .hasStatusThat() |
| 276 | .isEqualTo(CodeOwnerStatus.APPROVED); |
Chris Poucet | 73b98e9 | 2022-10-14 15:26:48 +0200 | [diff] [blame] | 277 | fileCodeOwnerStatusSubject.hasNewPathStatus().value().hasOwners(user); |
Edwin Kempin | ffe1d0f | 2021-01-20 14:03:06 +0100 | [diff] [blame] | 278 | // Verify that both files in patch set 2 would be approved by the user. |
| 279 | fileCodeOwnerStatuses = |
Chris Poucet | 73b98e9 | 2022-10-14 15:26:48 +0200 | [diff] [blame] | 280 | codeOwnerApprovalCheck.getFileStatusesForAccounts( |
Edwin Kempin | ffe1d0f | 2021-01-20 14:03:06 +0100 | [diff] [blame] | 281 | changeNotes, |
| 282 | changeNotes.getPatchSets().get(PatchSet.id(changeNotes.getChangeId(), 2)), |
Chris Poucet | 73b98e9 | 2022-10-14 15:26:48 +0200 | [diff] [blame] | 283 | ImmutableSet.of(user.id())); |
Edwin Kempin | ffe1d0f | 2021-01-20 14:03:06 +0100 | [diff] [blame] | 284 | ListSubject<FileCodeOwnerStatusSubject, FileCodeOwnerStatus> fileCodeOwnerStatusListSubject = |
| 285 | assertThatStream(fileCodeOwnerStatuses); |
| 286 | fileCodeOwnerStatusListSubject.hasSize(2); |
| 287 | fileCodeOwnerStatusSubject = fileCodeOwnerStatusListSubject.element(0); |
| 288 | fileCodeOwnerStatusSubject.hasNewPathStatus().value().hasPathThat().isEqualTo(path1); |
| 289 | fileCodeOwnerStatusSubject |
| 290 | .hasNewPathStatus() |
| 291 | .value() |
| 292 | .hasStatusThat() |
| 293 | .isEqualTo(CodeOwnerStatus.APPROVED); |
Chris Poucet | 73b98e9 | 2022-10-14 15:26:48 +0200 | [diff] [blame] | 294 | fileCodeOwnerStatusSubject.hasNewPathStatus().value().hasOwners(user); |
Edwin Kempin | ffe1d0f | 2021-01-20 14:03:06 +0100 | [diff] [blame] | 295 | fileCodeOwnerStatusSubject = fileCodeOwnerStatusListSubject.element(1); |
| 296 | fileCodeOwnerStatusSubject.hasNewPathStatus().value().hasPathThat().isEqualTo(path2); |
| 297 | fileCodeOwnerStatusSubject |
| 298 | .hasNewPathStatus() |
| 299 | .value() |
| 300 | .hasStatusThat() |
| 301 | .isEqualTo(CodeOwnerStatus.APPROVED); |
Chris Poucet | 73b98e9 | 2022-10-14 15:26:48 +0200 | [diff] [blame] | 302 | fileCodeOwnerStatusSubject.hasNewPathStatus().value().hasOwners(user); |
Edwin Kempin | ffe1d0f | 2021-01-20 14:03:06 +0100 | [diff] [blame] | 303 | } |
| 304 | |
Edwin Kempin | ef03eda | 2020-12-02 13:26:36 +0100 | [diff] [blame] | 305 | @GerritConfig(name = "plugin.code-owners.fallbackCodeOwners", value = "ALL_USERS") |
| 306 | @Test |
| 307 | public void approvedByFallbackCodeOwner() throws Exception { |
Edwin Kempin | ef03eda | 2020-12-02 13:26:36 +0100 | [diff] [blame] | 308 | Path path = Paths.get("/foo/bar.baz"); |
| 309 | String changeId = |
| 310 | createChange("Change Adding A File", JgitPath.of(path).get(), "file content").getChangeId(); |
Edwin Kempin | ffe1d0f | 2021-01-20 14:03:06 +0100 | [diff] [blame] | 311 | ChangeNotes changeNotes = getChangeNotes(changeId); |
Edwin Kempin | ef03eda | 2020-12-02 13:26:36 +0100 | [diff] [blame] | 312 | |
| 313 | // Verify that the file would be approved by the user since the user is a fallback code owner. |
| 314 | Stream<FileCodeOwnerStatus> fileCodeOwnerStatuses = |
Chris Poucet | 73b98e9 | 2022-10-14 15:26:48 +0200 | [diff] [blame] | 315 | codeOwnerApprovalCheck.getFileStatusesForAccounts( |
| 316 | changeNotes, changeNotes.getCurrentPatchSet(), ImmutableSet.of(user.id())); |
Edwin Kempin | ef03eda | 2020-12-02 13:26:36 +0100 | [diff] [blame] | 317 | FileCodeOwnerStatusSubject fileCodeOwnerStatusSubject = |
| 318 | assertThatStream(fileCodeOwnerStatuses).onlyElement(); |
| 319 | fileCodeOwnerStatusSubject.hasNewPathStatus().value().hasPathThat().isEqualTo(path); |
| 320 | fileCodeOwnerStatusSubject |
| 321 | .hasNewPathStatus() |
| 322 | .value() |
| 323 | .hasStatusThat() |
| 324 | .isEqualTo(CodeOwnerStatus.APPROVED); |
Chris Poucet | 73b98e9 | 2022-10-14 15:26:48 +0200 | [diff] [blame] | 325 | fileCodeOwnerStatusSubject.hasNewPathStatus().value().hasOwners(user); |
Edwin Kempin | ef03eda | 2020-12-02 13:26:36 +0100 | [diff] [blame] | 326 | } |
| 327 | |
| 328 | @GerritConfig(name = "plugin.code-owners.fallbackCodeOwners", value = "ALL_USERS") |
| 329 | @Test |
| 330 | public void notApprovedByFallbackCodeOwnerIfCodeOwerIsDefined() throws Exception { |
| 331 | TestAccount codeOwner = |
| 332 | accountCreator.create( |
| 333 | "codeOwner", "codeOwner@example.com", "CodeOwner", /* displayName= */ null); |
| 334 | |
| 335 | codeOwnerConfigOperations |
| 336 | .newCodeOwnerConfig() |
| 337 | .project(project) |
| 338 | .branch("master") |
| 339 | .folderPath("/foo/") |
| 340 | .addCodeOwnerEmail(codeOwner.email()) |
| 341 | .create(); |
| 342 | |
| 343 | Path path = Paths.get("/foo/bar.baz"); |
| 344 | String changeId = |
| 345 | createChange("Change Adding A File", JgitPath.of(path).get(), "file content").getChangeId(); |
Edwin Kempin | ffe1d0f | 2021-01-20 14:03:06 +0100 | [diff] [blame] | 346 | ChangeNotes changeNotes = getChangeNotes(changeId); |
Edwin Kempin | ef03eda | 2020-12-02 13:26:36 +0100 | [diff] [blame] | 347 | |
| 348 | // Verify that the file would not be approved by the user since fallback code owners do not |
| 349 | // apply. |
| 350 | Stream<FileCodeOwnerStatus> fileCodeOwnerStatuses = |
Chris Poucet | 73b98e9 | 2022-10-14 15:26:48 +0200 | [diff] [blame] | 351 | codeOwnerApprovalCheck.getFileStatusesForAccounts( |
| 352 | changeNotes, changeNotes.getCurrentPatchSet(), ImmutableSet.of(user.id())); |
Edwin Kempin | ef03eda | 2020-12-02 13:26:36 +0100 | [diff] [blame] | 353 | FileCodeOwnerStatusSubject fileCodeOwnerStatusSubject = |
| 354 | assertThatStream(fileCodeOwnerStatuses).onlyElement(); |
| 355 | fileCodeOwnerStatusSubject.hasNewPathStatus().value().hasPathThat().isEqualTo(path); |
| 356 | fileCodeOwnerStatusSubject |
| 357 | .hasNewPathStatus() |
| 358 | .value() |
| 359 | .hasStatusThat() |
| 360 | .isEqualTo(CodeOwnerStatus.INSUFFICIENT_REVIEWERS); |
Chris Poucet | 73b98e9 | 2022-10-14 15:26:48 +0200 | [diff] [blame] | 361 | fileCodeOwnerStatusSubject.hasNewPathStatus().value().hasOwnersThat().isEmpty(); |
Edwin Kempin | ef03eda | 2020-12-02 13:26:36 +0100 | [diff] [blame] | 362 | } |
| 363 | |
Edwin Kempin | ef03eda | 2020-12-02 13:26:36 +0100 | [diff] [blame] | 364 | private ChangeNotes getChangeNotes(String changeId) throws Exception { |
| 365 | return changeNotesFactory.create(project, Change.id(gApi.changes().id(changeId).get()._number)); |
| 366 | } |
| 367 | } |