Support comment lines in find-owner code owner configuration files Lines starting with '#' represent comments [1] and should be ignored. According to [1] using '#' at the line beginning is the only possibility to include comments (comments cannot appear at the end of a syntax line and using '//' is not supported). At the moment detecting comments doesn't make a big difference as they would be detected as invalid lines and then be ignored. But later we likely want to fail for invalid lines, but not for comments, hence it makes sense to differentiate both cases. For now the only benefit is that a warning will only be logged for invalid lines but not for comment lines. [1] https://gerrit.googlesource.com/plugins/find-owners/+/master/src/main/resources/Documentation/syntax.md Change-Id: Ie99b79576076a02f7fd7ac6e3b933a6cf2d5f116 Signed-off-by: Edwin Kempin <ekempin@google.com>
diff --git a/java/com/google/gerrit/plugins/codeowners/backend/findowners/CodeOwnerConfigParser.java b/java/com/google/gerrit/plugins/codeowners/backend/findowners/CodeOwnerConfigParser.java index f156365..45cec8f 100644 --- a/java/com/google/gerrit/plugins/codeowners/backend/findowners/CodeOwnerConfigParser.java +++ b/java/com/google/gerrit/plugins/codeowners/backend/findowners/CodeOwnerConfigParser.java
@@ -32,10 +32,14 @@ * following syntax elements are supported: * * <ul> + * <li>comment: a line can be a comment (comments must start with '#') * <li>code owner emails: a line can be the email of a code owner * </ul> * - * <p>Invalid lines and invalid emails are silently ignored. + * <p>Comment lines, invalid lines and invalid emails are silently ignored. + * + * <p>Comments cannot appear as part of syntax lines, but only as separate lines (e.g. using + * 'foo.bar@example.com # Foo Bar' would be invalid). */ @Singleton class CodeOwnerConfigParser { @@ -60,7 +64,7 @@ * Parses a {@link CodeOwnerConfig} from a string that represents the content of an {@code OWNERS} * file. * - * <p>Invalid lines and invalid emails are silently ignored. + * <p>Comment lines, invalid lines and invalid emails are silently ignored. * * @param codeOwnerConfigKey the key of the code owner config that should be parsed * @param codeOwnerConfigFileContent string that represents the content of an {@code OWNERS} file @@ -71,6 +75,7 @@ Streams.stream(Splitter.on('\n').split(codeOwnerConfigFileContent)) .map(String::trim) + .filter(line -> !isComment(line)) .filter(this::isEmail) .distinct() .forEach(codeOwnerConfig::addCodeOwnerEmail); @@ -78,6 +83,10 @@ return codeOwnerConfig.build(); } + private static boolean isComment(String trimmedLine) { + return trimmedLine.startsWith("#"); + } + private boolean isEmail(String trimmedLine) { if (!emailValidator.isValid(trimmedLine)) { logger.atInfo().log("Skipping line that is not an email: %s", trimmedLine);
diff --git a/javatests/com/google/gerrit/plugins/codeowners/backend/findowners/CodeOwnerConfigParserTest.java b/javatests/com/google/gerrit/plugins/codeowners/backend/findowners/CodeOwnerConfigParserTest.java index 2944966..cfaecaa 100644 --- a/javatests/com/google/gerrit/plugins/codeowners/backend/findowners/CodeOwnerConfigParserTest.java +++ b/javatests/com/google/gerrit/plugins/codeowners/backend/findowners/CodeOwnerConfigParserTest.java
@@ -115,6 +115,22 @@ assertThat(codeOwnerConfig).hasCodeOwnersEmailsThat().containsExactly(EMAIL_1, EMAIL_2); } + @Test + public void codeOwnerConfigWithCommentLines_CommentLinesAreIgnored() throws Exception { + CodeOwnerConfig codeOwnerConfig = + parse(String.format("# a@test.com\n%s\n # b@test.com\n%s\n#c@test.com", EMAIL_1, EMAIL_2)); + assertThat(codeOwnerConfig).hasCodeOwnersEmailsThat().containsExactly(EMAIL_1, EMAIL_2); + } + + @Test + public void + codeOwnerConfigWithInlineComments_LinesWithInlineCommentsAreConsideredAsInvalidAndAreIgnored() + throws Exception { + CodeOwnerConfig codeOwnerConfig = + parse(String.format("foo.bar@test.com # Foo Bar\n%s\n%s", EMAIL_1, EMAIL_2)); + assertThat(codeOwnerConfig).hasCodeOwnersEmailsThat().containsExactly(EMAIL_1, EMAIL_2); + } + /** * Parses the given code owner config. *