From d5d5a404b03509858a2f9b851edb29157f0777b4 Mon Sep 17 00:00:00 2001 From: Nikita Tkachenko Date: Thu, 6 Feb 2025 15:01:51 +0100 Subject: [PATCH] Update CODEOWNERS parser to not log errors on comments with leading whitespace --- .../civisibility/codeowners/EntryBuilder.java | 11 ++++++++--- .../codeowners/EntryBuilderTest.groovy | 17 ++++++++++++++--- 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/dd-java-agent/agent-ci-visibility/src/main/java/datadog/trace/civisibility/codeowners/EntryBuilder.java b/dd-java-agent/agent-ci-visibility/src/main/java/datadog/trace/civisibility/codeowners/EntryBuilder.java index 31c53d2fa97..0483909c2e5 100644 --- a/dd-java-agent/agent-ci-visibility/src/main/java/datadog/trace/civisibility/codeowners/EntryBuilder.java +++ b/dd-java-agent/agent-ci-visibility/src/main/java/datadog/trace/civisibility/codeowners/EntryBuilder.java @@ -47,9 +47,14 @@ public EntryBuilder(CharacterMatcher.Factory characterMatcherFactory, String s) public @Nullable Entry parse() { try { - if (c.length == 0 // empty line - || c[0] == '#' // comment - || c[0] == '[') { // section header + // skip trailing whitespace + while (offset < c.length && Character.isWhitespace(c[offset])) { + offset++; + } + + if (offset == c.length // empty line + || c[offset] == '#' // comment + || c[offset] == '[') { // section header return null; } diff --git a/dd-java-agent/agent-ci-visibility/src/test/groovy/datadog/trace/civisibility/codeowners/EntryBuilderTest.groovy b/dd-java-agent/agent-ci-visibility/src/test/groovy/datadog/trace/civisibility/codeowners/EntryBuilderTest.groovy index 866183de68f..28d260b8240 100644 --- a/dd-java-agent/agent-ci-visibility/src/test/groovy/datadog/trace/civisibility/codeowners/EntryBuilderTest.groovy +++ b/dd-java-agent/agent-ci-visibility/src/test/groovy/datadog/trace/civisibility/codeowners/EntryBuilderTest.groovy @@ -154,14 +154,25 @@ class EntryBuilderTest extends Specification { return result.toString() } - def "test invalid range parsing"() { + def "test invalid entry parsing: #entry"() { setup: def matcherFactory = new CharacterMatcher.Factory() when: - def entry = new EntryBuilder(matcherFactory, "token[z-a] owner").parse() + def parsedEntry = new EntryBuilder(matcherFactory, entry).parse() then: - entry == null + parsedEntry == null + + where: + entry << [ + "token[z-a] owner", + "# comment", + " # comment with a leading space", + "[section header]", + " [section header with a leading space]", + "", + " ", + ] } }