From d446022c2081dc27a8d5c0d142c1712f417a007c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81ngel=20Paredes?= Date: Mon, 28 Oct 2024 11:21:10 +0100 Subject: [PATCH] fix(ci_visibility): strip out comments in codeowners (#11173) ## Description JIRA: https://datadoghq.atlassian.net/browse/SDTEST-1153 We do not strip the comments for the codeowners sections causing [issues like this](https://app.datadoghq.com/ci/test-runs?query=test_level:test&agg_m=count&agg_m_source=base&agg_t=count&citest_explorer_sort=@test.codeowners,desc&cols=@test.status,@test.codeowners,timestamp,@test.suite,@test.name,@duration,@test.service,@git.branch¤tTab=overview&eventStack=AgAAAZLDS54Ikm5TUgAAAAAAAAAYAAAAAEFaTERTNkJYQUFCVDNOMEllNXhGMUxmNwAAACQAAAAAMDE5MmMzNGQtNTM3Mi00NGY5LWI3OGQtM2RjNGMzODdjNjNi&fromUser=false&graphType=flamegraph&index=citest&testId=AgAAAZLDS54Ikm5TUgAAAAAAAAAYAAAAAEFaTERTNkJYQUFCVDNOMEllNXhGMUxmNwAAACQAAAAAMDE5MmMzNGQtNTM3Mi00NGY5LWI3OGQtM2RjNGMzODdjNjNi&trace=AgAAAZLDS54Ikm5TUgAAAAAAAAAYAAAAAEFaTERTNkJYQUFCVDNOMEllNXhGMUxmNwAAACQAAAAAMDE5MmMzNGQtNTM3Mi00NGY5LWI3OGQtM2RjNGMzODdjNjNi&start=1729248608094&end=1729853408094&paused=false) This PR: - Just checks for `#` and strips out to the end of the line ## Checklist - [x] PR author has checked that all the criteria below are met - The PR description includes an overview of the change - The PR description articulates the motivation for the change - The change includes tests OR the PR description describes a testing strategy - The PR description notes risks associated with the change, if any - Newly-added code is easy to change - The change follows the [library release note guidelines](https://ddtrace.readthedocs.io/en/stable/releasenotes.html) - The change includes or references documentation updates if necessary - Backport labels are set (if [applicable](https://ddtrace.readthedocs.io/en/latest/contributing.html#backporting)) ## Reviewer Checklist - [x] Reviewer has checked that all the criteria below are met - Title is accurate - All changes are related to the pull request's stated goal - Avoids breaking [API](https://ddtrace.readthedocs.io/en/stable/versioning.html#interfaces) changes - Testing strategy adequately addresses listed risks - Newly-added code is easy to change - Release note makes sense to a user of the library - If necessary, author has acknowledged and discussed the performance implications of this PR as reported in the benchmarks PR comment - Backport labels are set in a manner that is consistent with the [release branch maintenance policy](https://ddtrace.readthedocs.io/en/latest/contributing.html#backporting) --------- Co-authored-by: Romain Komorn (cherry picked from commit 4dde7b84ba21247265850310dd685d8a1b0f0c20) --- ddtrace/internal/codeowners.py | 9 ++++++--- ...x_codeowners_including_comments-82d9cb733a2c7285.yaml | 3 +++ tests/internal/test_codeowners.py | 3 +++ 3 files changed, 12 insertions(+), 3 deletions(-) create mode 100644 releasenotes/notes/ci_visibility-fix_codeowners_including_comments-82d9cb733a2c7285.yaml diff --git a/ddtrace/internal/codeowners.py b/ddtrace/internal/codeowners.py index fa1e63f7add..ead8e2f31b8 100644 --- a/ddtrace/internal/codeowners.py +++ b/ddtrace/internal/codeowners.py @@ -152,11 +152,14 @@ def parse(self) -> None: patterns = [] for line in f.readlines(): line = line.strip() + + if "#" in line: + # Strip out the comment from the line + line = line.split("#", 1)[0].strip() + if line == "": continue - # Lines starting with '#' are comments. - if line.startswith("#"): - continue + if line.startswith("[") and line.endswith("]"): # found a code owners section continue diff --git a/releasenotes/notes/ci_visibility-fix_codeowners_including_comments-82d9cb733a2c7285.yaml b/releasenotes/notes/ci_visibility-fix_codeowners_including_comments-82d9cb733a2c7285.yaml new file mode 100644 index 00000000000..3fb4bb5abd8 --- /dev/null +++ b/releasenotes/notes/ci_visibility-fix_codeowners_including_comments-82d9cb733a2c7285.yaml @@ -0,0 +1,3 @@ +--- +fixes: + - CI Visibility: "fixes a bug where ``CODEOWNERS`` would incorrectly fail to discard line-level trailing comments (eg: ``@code/owner # my comment`` would result in codeowners being parsed as ``@code/owner``, ``#``, ``my``, and ``comment``)" diff --git a/tests/internal/test_codeowners.py b/tests/internal/test_codeowners.py index fdf3ba28d65..5536eeea7dc 100644 --- a/tests/internal/test_codeowners.py +++ b/tests/internal/test_codeowners.py @@ -9,9 +9,12 @@ def test_invalid_codeowners(testdir): ^[invalid optional section bar.py @bars + # Inline comment case + baz.py @DataDog/the-owner # all that should be ignored """ codeowners_file = testdir.makefile("", CODEOWNERS=codeowners) c = Codeowners(path=codeowners_file.strpath) assert c.of("foo.py") == ["@default"] assert c.of("bar.py") == ["@bars"] + assert c.of("baz.py") == ["@DataDog/the-owner"]