Fix matchers-only OWNERS config
When using OWNERS only with matchers (e.g. file-suffix), the owners
constraint does not need to be applied to files in the same directory
that do have owners associated.
Change-Id: I2f4fac8d836d60cd90e8b4f5c4e100594e0551d1
diff --git a/owners-common/src/main/java/com/vmware/gerrit/owners/common/OwnersMap.java b/owners-common/src/main/java/com/vmware/gerrit/owners/common/OwnersMap.java
index 2d56806..5167d19 100644
--- a/owners-common/src/main/java/com/vmware/gerrit/owners/common/OwnersMap.java
+++ b/owners-common/src/main/java/com/vmware/gerrit/owners/common/OwnersMap.java
@@ -56,6 +56,10 @@
return fileOwners;
}
public void addFileOwners(String file, Set<Id> owners) {
+ if(owners.isEmpty()) {
+ return;
+ }
+
Set<Id> set = fileOwners.get(file);
if(set!=null) {
// add new owners removing duplicates
diff --git a/owners-common/src/test/java/com/vmware/gerrit/owners/common/Config.java b/owners-common/src/test/java/com/vmware/gerrit/owners/common/Config.java
index ba045d8..499b152 100644
--- a/owners-common/src/test/java/com/vmware/gerrit/owners/common/Config.java
+++ b/owners-common/src/test/java/com/vmware/gerrit/owners/common/Config.java
@@ -19,6 +19,7 @@
import static org.easymock.EasyMock.expect;
import java.io.IOException;
+import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
@@ -62,6 +63,10 @@
.anyTimes();
}
+ void creatingPatch(String... fileNames) {
+ creatingPatchList(Arrays.asList(fileNames));
+ }
+
void creatingPatchList(List<String> names) {
patchList = PowerMock.createMock(PatchList.class);
List<PatchListEntry> entries =
@@ -88,9 +93,11 @@
MatcherConfig... matchers) {
StringBuilder sb = new StringBuilder();
sb.append("inherited: " + inherited + "\n");
- sb.append("owners: \n");
- for (String owner : owners) {
- sb.append("- " + owner + "\n");
+ if (owners.length > 0) {
+ sb.append("owners: \n");
+ for (String owner : owners) {
+ sb.append("- " + owner + "\n");
+ }
}
if (matchers.length > 0) {
sb.append("matchers: \n");
diff --git a/owners-common/src/test/java/com/vmware/gerrit/owners/common/RegexTest.java b/owners-common/src/test/java/com/vmware/gerrit/owners/common/RegexTest.java
index 9599672..56e99a2 100644
--- a/owners-common/src/test/java/com/vmware/gerrit/owners/common/RegexTest.java
+++ b/owners-common/src/test/java/com/vmware/gerrit/owners/common/RegexTest.java
@@ -14,6 +14,8 @@
package com.vmware.gerrit.owners.common;
+import static com.google.common.truth.Truth.assertThat;
+import static com.google.common.truth.Truth8.assertThat;
import static com.vmware.gerrit.owners.common.MatcherConfig.exactMatcher;
import static com.vmware.gerrit.owners.common.MatcherConfig.partialRegexMatcher;
import static com.vmware.gerrit.owners.common.MatcherConfig.regexMatcher;
@@ -215,4 +217,35 @@
assertEquals(1,set4.size()); // only 1 because a is class and alfa owner
assertTrue(set4.contains(ACCOUNT_A_ID));
}
-}
+
+ @Test
+ public void testMatchersOnlyConfig() throws Exception {
+ replayAll();
+
+ Optional<OwnersConfig> ownersConfigOpt =
+ getOwnersConfig(createConfig(false, new String[0],
+ suffixMatcher(".txt", ACCOUNT_B)));
+
+ assertThat(ownersConfigOpt).named("ownersConfig").isPresent();
+ OwnersConfig ownersConfig = ownersConfigOpt.get();
+
+ assertThat(ownersConfig.getOwners()).isEmpty();
+ assertThat(ownersConfig.getMatchers()).isNotEmpty();
+ }
+
+ @Test
+ public void testkRegexShouldMatchOnlyOnSuffix() throws Exception {
+ String configString =
+ createConfig(false, new String[0], suffixMatcher(".sql", ACCOUNT_B));
+
+ expectConfig("OWNERS", configString);
+ expectNoConfig("project/OWNERS");
+ creatingPatch("project/file.sql", "another.txt");
+ replayAll();
+
+ PathOwners owners = new PathOwners(accounts, repository, patchList);
+
+ Set<String> ownedFiles = owners.getFileOwners().keySet();
+ assertThat(ownedFiles).containsExactly("project/file.sql");
+ }
+}
\ No newline at end of file