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(