Skip to content

Commit

Permalink
Merge pull request #28 from cmars/fix/target-matching
Browse files Browse the repository at this point in the history
fix: project target matching
  • Loading branch information
EricFernandezSnyk authored Jan 23, 2024
2 parents 7f11257 + 6a69579 commit aadac11
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 1 deletion.
2 changes: 1 addition & 1 deletion snyk_tags/component.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
65 changes: 65 additions & 0 deletions tests/test_component.py
Original file line number Diff line number Diff line change
Expand Up @@ -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<org>\S+)/(?P<proj>\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(
Expand Down

0 comments on commit aadac11

Please sign in to comment.