Skip to content

Commit d3ad273

Browse files
wiegandmgreenbonebot
authored andcommitted
Fix: Be more strict when matching copyrights
Ensure that only matches containing at least a creation year and a copyright holder are considered valid matches. This avoids treating partial matches like `Copyright string xyz` in code as copyright statements.
1 parent 95d0fa0 commit d3ad273

File tree

2 files changed

+24
-8
lines changed

2 files changed

+24
-8
lines changed

pontos/updateheader/updateheader.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -90,14 +90,15 @@ def _find_copyright(
9090
"""Match the line for the copyright_regex"""
9191
copyright_match = re.search(copyright_regex, line)
9292
if copyright_match:
93-
return (
94-
True,
95-
CopyrightMatch(
96-
creation_year=copyright_match.group(2),
97-
modification_year=copyright_match.group(3),
98-
company=copyright_match.group(4),
99-
),
100-
)
93+
if copyright_match.group(2) and copyright_match.group(4):
94+
return (
95+
True,
96+
CopyrightMatch(
97+
creation_year=copyright_match.group(2),
98+
modification_year=copyright_match.group(3),
99+
company=copyright_match.group(4),
100+
),
101+
)
101102
return False, None
102103

103104

tests/updateheader/test_header.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,14 @@ def test_find_copyright(self):
7373
"# This program is free software: "
7474
"you can redistribute it and/or modify"
7575
)
76+
not_a_copyright_line = """
77+
package product
78+
79+
type FooProduct struct {
80+
Version string `json:"version"`
81+
SPDXLicenseID string `json:"spdx-license-identifier"`
82+
Copyright string `json:"copyright"`
83+
""" # noqa: E501
7684

7785
# Full match
7886
found, match = find_copyright(
@@ -101,6 +109,13 @@ def test_find_copyright(self):
101109
self.assertFalse(found)
102110
self.assertIsNone(match)
103111

112+
# Looks like a copyright, but isn't
113+
found, match = find_copyright(
114+
copyright_regex=self.regex, line=not_a_copyright_line
115+
)
116+
self.assertFalse(found)
117+
self.assertIsNone(match)
118+
104119
def test_find_spdx_copyright(self):
105120
test_line = "# SPDX-FileCopyrightText: 1995-2021 Greenbone AG"
106121
test2_line = "# SPDX-FileCopyrightText: 1995 Greenbone AG"

0 commit comments

Comments
 (0)