From 6a69579b83c2f4cdca6e128664ddfbac1d178cba Mon Sep 17 00:00:00 2001 From: Casey Marshall Date: Mon, 22 Jan 2024 14:43:13 -0600 Subject: [PATCH] fix: project target matching Fixes an issue where related target attributes weren't available for matching. Credits to @thavelock for discovering and helping debug the issue. --- snyk_tags/component.py | 2 +- tests/test_component.py | 65 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+), 1 deletion(-) diff --git a/snyk_tags/component.py b/snyk_tags/component.py index 5ceb27c..b2b0192 100644 --- a/snyk_tags/component.py +++ b/snyk_tags/component.py @@ -194,7 +194,7 @@ def tag( # Clear context as this dict is (re)used in-place with each # execution of the project matcher rules. context.clear() - component = match_fn(project.get("attributes", {})) + component = match_fn(project_obj) if not component: # Rule did not match continue diff --git a/tests/test_component.py b/tests/test_component.py index 5e9f5a6..1e59df8 100644 --- a/tests/test_component.py +++ b/tests/test_component.py @@ -72,6 +72,71 @@ def test_component_tag_match_dry_run(tmpdir, httpx_mock): ) +def test_component_tag_match_target_dry_run(tmpdir, httpx_mock): + rules_file = tmpdir.join("rules.yaml") + rules_file.write( + """ +version: 1 +rules: + - name: test + projects: + - target: + url: + regex: '.*/(?P\S+)/(?P\S+)$' + component: '{org}-{proj}-component' +""" + ) + httpx_mock.add_response( + method="GET", + url=re.compile("^.*/orgs/some-org/projects[?].*"), + json={ + "data": [ + { + "id": "some-project", + "attributes": { + "name": "test", + }, + "relationships": { + "target": { + "data": { + "attributes": { + "display_name": "some-org/java-goof", + "url": "https://github.com/some-org/java-goof", + }, + }, + }, + }, + }, + ], + }, + ) + httpx_mock.add_response( + method="POST", url=re.compile("^.*/org/some-org/project/some-project/tags$") + ) + httpx_mock.add_response( + status_code=400 + ) # catch-all response, otherwise backoff retry will block testing + + result = runner.invoke( + app, + [ + "component", + "tag", + "--org-id", + "some-org", + "--snyktkn", + "some-token", + "--dry-run", + str(rules_file), + ], + ) + assert result.exit_code == 0 + assert ( + """would add tag "component:some-org-java-goof-component" in project id="some-project" name="test\"""" + in result.stdout + ) + + def test_component_tag_match_added(tmpdir, httpx_mock): rules_file = tmpdir.join("rules.yaml") rules_file.write(