Skip to content

Commit 7ff7607

Browse files
committed
add the conflicting-versions rule
Signed-off-by: tdruez <tdruez@aboutcode.org>
1 parent 8db8c58 commit 7ff7607

4 files changed

Lines changed: 81 additions & 32 deletions

File tree

‎CHANGELOG.rst‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ Unreleased
99
----------
1010

1111
* feat: expose the ``is_addon`` attribute in the pipelines REST API
12+
* feat: [code-genetics] determine the origin of files from combined origin matches #1929 by @tdruez
1213

1314
v38.2.0 (2026-10-22)
1415
--------------------

‎scanpipe/models.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5181,9 +5181,9 @@ class Status(models.TextChoices):
51815181
CONFIRMED = "confirmed"
51825182
NO_ORIGIN = "no-origin"
51835183

5184+
CONFIRMED_STATUSES = [Status.AUTO_CONFIRMED, Status.CONFIRMED]
51845185
# Set by the determination rules, and replaced each time the rules run.
51855186
AUTOMATIC_STATUSES = [Status.AUTO_CONFIRMED, Status.NEEDS_REVIEW]
5186-
CONFIRMED_STATUSES = [Status.AUTO_CONFIRMED, Status.CONFIRMED]
51875187

51885188
resource = models.OneToOneField(
51895189
CodebaseResource,

‎scanpipe/pipes/origin.py‎

Lines changed: 41 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
from scanpipe.pipes.resolve import convert_spdx_expression
4040

4141
CONFLICTING_CANDIDATES_RULE = "conflicting-candidates"
42+
CONFLICTING_VERSIONS_RULE = "conflicting-versions"
4243
NO_EXACT_MATCH_RULE = "no-exact-match"
4344
SINGLE_TOOL_RULE = "single-tool"
4445
CONFIRMED_BY_TWO_TOOLS_RULE = "confirmed-by-two-tools"
@@ -79,10 +80,19 @@ def get_equivalent_purls(origin_match):
7980
Return the PURLs identifying the ``origin_match`` package: its own PURL and
8081
the equivalent PURLs reported by the tool, such as the SCANOSS ``purls``.
8182
"""
82-
reported_purl = get_package_purl(origin_match.purl)
83-
version = PackageURL.from_string(reported_purl).version
83+
version = PackageURL.from_string(origin_match.purl).version
8484
tool_purls = origin_match.extra_data.get("purls", [])
85-
return {reported_purl, *get_versioned_purls(tool_purls, version)}
85+
return get_versioned_purls([origin_match.purl, *tool_purls], version)
86+
87+
88+
def get_versionless_purl(purl):
89+
"""Return the ``purl`` without version, qualifiers and subpath."""
90+
package_url = PackageURL.from_string(purl)
91+
return PackageURL(
92+
type=package_url.type,
93+
namespace=package_url.namespace,
94+
name=package_url.name,
95+
).to_string()
8696

8797

8898
@dataclass
@@ -129,6 +139,10 @@ def purl(self):
129139
]
130140
return (common_reported_purls or reported_purls)[0]
131141

142+
@property
143+
def versionless_purls(self):
144+
return {get_versionless_purl(purl) for purl in self.equivalent_purls}
145+
132146
def is_same_package(self, other_origin):
133147
return bool(self.equivalent_purls & other_origin.equivalent_purls)
134148

@@ -204,35 +218,42 @@ def is_directory_origin(file_origins, directory_origins):
204218
)
205219

206220

207-
def get_status_and_rule(origin):
208-
"""Return the determination status and rule of a single candidate ``origin``."""
221+
def is_same_package_versions(origins):
222+
"""Return True when the ``origins`` are versions of the same package."""
223+
return bool(set.intersection(*(origin.versionless_purls for origin in origins)))
224+
225+
226+
def get_rule(origins):
227+
"""Return the determination rule of the candidate ``origins`` of a resource."""
228+
if len(origins) > 1:
229+
if is_same_package_versions(origins):
230+
return CONFLICTING_VERSIONS_RULE
231+
return CONFLICTING_CANDIDATES_RULE
232+
233+
origin = origins[0]
209234
if not origin.has_exact_match:
210-
return OriginDetermination.Status.NEEDS_REVIEW, NO_EXACT_MATCH_RULE
235+
return NO_EXACT_MATCH_RULE
211236

212237
# A single tool can miss the other candidates: MatchCode.io stops at the
213238
# first package matched, and SCANOSS reports one component per file.
214239
if len(origin.tools) < 2:
215-
return OriginDetermination.Status.NEEDS_REVIEW, SINGLE_TOOL_RULE
240+
return SINGLE_TOOL_RULE
216241

217-
return OriginDetermination.Status.AUTO_CONFIRMED, CONFIRMED_BY_TWO_TOOLS_RULE
242+
return CONFIRMED_BY_TWO_TOOLS_RULE
218243

219244

220245
def make_origin_determination(project, resource, origins):
221246
"""Return an unsaved OriginDetermination for the ``resource`` ``origins``."""
222-
if len(origins) > 1:
223-
return OriginDetermination(
224-
project=project,
225-
resource=resource,
226-
status=OriginDetermination.Status.NEEDS_REVIEW,
227-
rule=CONFLICTING_CANDIDATES_RULE,
228-
)
247+
rule = get_rule(origins)
248+
status = OriginDetermination.Status.NEEDS_REVIEW
249+
if rule == CONFIRMED_BY_TWO_TOOLS_RULE:
250+
status = OriginDetermination.Status.AUTO_CONFIRMED
229251

230-
origin = origins[0]
231-
status, rule = get_status_and_rule(origin)
252+
has_single_candidate = len(origins) == 1
232253
return OriginDetermination(
233254
project=project,
234255
resource=resource,
235-
purl=origin.purl,
256+
purl=origins[0].purl if has_single_candidate else "",
236257
status=status,
237258
rule=rule,
238259
)
@@ -266,6 +287,8 @@ def iter_origin_determinations(project, origin_matches):
266287
yield make_origin_determination(project, resource, file_origins)
267288
continue
268289

290+
# The file matches count for the directory: an exact file match by one tool
291+
# confirms the directory match by another tool.
269292
for file_origin in file_origins:
270293
get_same_package_origins(file_origin, directory_origins)[0].merge(
271294
file_origin

‎scanpipe/tests/pipes/test_origin.py‎

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,10 @@ def test_scanpipe_pipes_origin_get_equivalent_purls(self):
132132
expected = {"pkg:pypi/six@1.17.0"}
133133
self.assertEqual(expected, origin.get_equivalent_purls(origin_match))
134134

135+
def test_scanpipe_pipes_origin_get_versionless_purl(self):
136+
purl = "pkg:pypi/six@1.17.0?file_name=six-1.17.0.tar.gz#six.py"
137+
self.assertEqual("pkg:pypi/six", origin.get_versionless_purl(purl))
138+
135139
def test_scanpipe_pipes_origin_origin_from_origin_match(self):
136140
origin_match = make_unsaved_origin_match(
137141
"pkg:github/benjaminp/six@1.17.0", tool="scanoss", purls=SCANOSS_SIX_PURLS
@@ -158,6 +162,10 @@ def test_scanpipe_pipes_origin_origin_purl(self):
158162
scanoss_origin.merge(make_origin("pkg:pypi/six@1.17.0"))
159163
self.assertEqual("pkg:pypi/six@1.17.0", scanoss_origin.purl)
160164

165+
def test_scanpipe_pipes_origin_origin_versionless_purls(self):
166+
expected = {"pkg:github/benjaminp/six", "pkg:pypi/six", "pkg:conda/six"}
167+
self.assertEqual(expected, make_scanoss_six_origin().versionless_purls)
168+
161169
def test_scanpipe_pipes_origin_origin_is_same_package(self):
162170
scanoss_origin = make_scanoss_six_origin()
163171
self.assertTrue(
@@ -270,23 +278,33 @@ def test_scanpipe_pipes_origin_is_directory_origin(self):
270278
)
271279
self.assertFalse(origin.is_directory_origin([ms_origin], []))
272280

273-
def test_scanpipe_pipes_origin_get_status_and_rule(self):
274-
needs_review = OriginDetermination.Status.NEEDS_REVIEW
281+
def test_scanpipe_pipes_origin_is_same_package_versions(self):
282+
six_17_origin = make_scanoss_six_origin()
283+
six_16_origin = make_origin("pkg:pypi/six@1.16.0")
284+
urllib3_origin = make_origin("pkg:pypi/urllib3@1.26.20")
275285

276-
snippet_origin = make_origin("pkg:npm/lodash@4.17.21", match_type="snippet")
277-
expected = (needs_review, origin.NO_EXACT_MATCH_RULE)
278-
self.assertEqual(expected, origin.get_status_and_rule(snippet_origin))
286+
self.assertTrue(origin.is_same_package_versions([six_17_origin, six_16_origin]))
287+
self.assertFalse(
288+
origin.is_same_package_versions([six_16_origin, urllib3_origin])
289+
)
279290

291+
def test_scanpipe_pipes_origin_get_rule(self):
280292
six_origin = make_origin("pkg:pypi/six@1.17.0")
281-
expected = (needs_review, origin.SINGLE_TOOL_RULE)
282-
self.assertEqual(expected, origin.get_status_and_rule(six_origin))
293+
six_16_origin = make_origin("pkg:pypi/six@1.16.0")
294+
urllib3_origin = make_origin("pkg:pypi/urllib3@1.26.20")
295+
snippet_origin = make_origin("pkg:npm/lodash@4.17.21", match_type="snippet")
296+
297+
rule = origin.get_rule([six_origin, urllib3_origin])
298+
self.assertEqual(origin.CONFLICTING_CANDIDATES_RULE, rule)
299+
rule = origin.get_rule([six_origin, six_16_origin])
300+
self.assertEqual(origin.CONFLICTING_VERSIONS_RULE, rule)
301+
rule = origin.get_rule([snippet_origin])
302+
self.assertEqual(origin.NO_EXACT_MATCH_RULE, rule)
303+
self.assertEqual(origin.SINGLE_TOOL_RULE, origin.get_rule([six_origin]))
283304

284305
six_origin.merge(make_scanoss_six_origin())
285-
expected = (
286-
OriginDetermination.Status.AUTO_CONFIRMED,
287-
origin.CONFIRMED_BY_TWO_TOOLS_RULE,
288-
)
289-
self.assertEqual(expected, origin.get_status_and_rule(six_origin))
306+
rule = origin.get_rule([six_origin])
307+
self.assertEqual(origin.CONFIRMED_BY_TWO_TOOLS_RULE, rule)
290308

291309
def test_scanpipe_pipes_origin_make_origin_determination(self):
292310
resource = make_resource_file(self.project1, path="six.py")
@@ -308,6 +326,13 @@ def test_scanpipe_pipes_origin_make_origin_determination(self):
308326
self.assertEqual("needs-review", determination.status)
309327
self.assertEqual(origin.CONFLICTING_CANDIDATES_RULE, determination.rule)
310328

329+
six_origin.merge(make_scanoss_six_origin())
330+
determination = origin.make_origin_determination(
331+
self.project1, resource, [six_origin]
332+
)
333+
self.assertEqual("pkg:pypi/six@1.17.0", determination.purl)
334+
self.assertEqual("auto-confirmed", determination.status)
335+
311336
def test_scanpipe_pipes_origin_iter_origin_determinations(self):
312337
vendor = make_resource_directory(self.project1, path="vendor")
313338
vendor_ms = make_resource_directory(self.project1, path="vendor/ms")
@@ -555,7 +580,7 @@ def test_scanpipe_pipes_origin_determine_origins_code_genetics_data_set(self):
555580
"src/inih/ini.c",
556581
"",
557582
"needs-review",
558-
origin.CONFLICTING_CANDIDATES_RULE,
583+
origin.CONFLICTING_VERSIONS_RULE,
559584
),
560585
(
561586
"src/inih/ini.h",

0 commit comments

Comments
 (0)