Skip to content

Commit

Permalink
Merge pull request #16 from cmars/fix/component-dry-run-option
Browse files Browse the repository at this point in the history
fix: dry-run option
  • Loading branch information
EricFernandezSnyk authored Aug 2, 2023
2 parents 3dccb70 + 352e68d commit daf8041
Show file tree
Hide file tree
Showing 2 changed files with 177 additions and 17 deletions.
33 changes: 18 additions & 15 deletions snyk_tags/component.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,32 +211,35 @@ def tag(
component=other_component,
**print_format_args,
)
client.remove_project_tag(
org_id,
project["id"],
tag={"key": "component", "value": other_component},
)
if not dry_run:
client.remove_project_tag(
org_id,
project["id"],
tag={"key": "component", "value": other_component},
)

if remove:
if have_component_tag:
fmtr.print(
action="remove tag", component=component, **print_format_args
)
client.remove_project_tag(
org_id,
project["id"],
tag={"key": "component", "value": component},
)
if not dry_run:
client.remove_project_tag(
org_id,
project["id"],
tag={"key": "component", "value": component},
)
else:
if not have_component_tag:
fmtr.print(
action="add tag", component=component, **print_format_args
)
client.add_project_tag(
org_id,
project["id"],
tag={"key": "component", "value": component},
)
if not dry_run:
client.add_project_tag(
org_id,
project["id"],
tag={"key": "component", "value": component},
)
else:
fmtr.print(
action="keep tag", component=component, **print_format_args
Expand Down
161 changes: 159 additions & 2 deletions tests/test_component.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,6 @@ def test_component_tag_match_exclusive(tmpdir, httpx_mock):
],
)
assert result.exit_code == 0
print(result.stdout)
assert (
"""remove other tag "component:other-component" in project id="some-project" name="test\""""
in result.stdout
Expand Down Expand Up @@ -361,7 +360,6 @@ def test_component_tag_match_remove_exclusive(tmpdir, httpx_mock):
str(rules_file),
],
)
print(result.stdout)
assert result.exit_code == 0
assert (
"""remove other tag "component:other-component" in project id="some-project" name="test\""""
Expand All @@ -371,3 +369,162 @@ def test_component_tag_match_remove_exclusive(tmpdir, httpx_mock):
"""remove tag "component:test-component" in project id="some-project" name="test\""""
in result.stdout
)


def test_component_tag_dry_run(tmpdir, httpx_mock):
rules_file = tmpdir.join("rules.yaml")
rules_file.write(
"""
version: 1
rules:
- name: test
projects:
- name: test
component: test-component
"""
)
httpx_mock.add_response(
method="GET",
url=re.compile("^.*/orgs/some-org/projects[?].*"),
json={
"data": [
{
"id": "some-project",
"attributes": {
"name": "test",
"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:test-component" in project id="some-project" name="test\""""
in result.stdout
)


def test_component_tag_dry_run(tmpdir, httpx_mock):
rules_file = tmpdir.join("rules.yaml")
rules_file.write(
"""
version: 1
rules:
- name: test
projects:
- name: test
component: test-component
"""
)
httpx_mock.add_response(
method="GET",
url=re.compile("^.*/orgs/some-org/projects[?].*"),
json={
"data": [
{
"id": "some-project",
"attributes": {
"name": "test",
"tags": [{"key": "component", "value": "test-component"}],
},
},
],
},
)
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",
"--remove",
"--dry-run",
str(rules_file),
],
)
assert result.exit_code == 0
assert (
"""would remove tag "component:test-component" in project id="some-project" name="test\""""
in result.stdout
)


def test_component_tag_dry_run_match_exclusive(tmpdir, httpx_mock):
rules_file = tmpdir.join("rules.yaml")
rules_file.write(
"""
version: 1
rules:
- name: test
projects:
- name: test
component: test-component
"""
)
httpx_mock.add_response(
method="GET",
url=re.compile("^.*/orgs/some-org/projects[?].*"),
json={
"data": [
{
"id": "some-project",
"attributes": {
"name": "test",
"tags": [{"key": "component", "value": "other-component"}],
},
},
],
},
)
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",
"--exclusive",
"--dry-run",
str(rules_file),
],
)
assert result.exit_code == 0
assert (
"""would remove other tag "component:other-component" in project id="some-project" name="test\""""
in result.stdout
)
assert (
"""would add tag "component:test-component" in project id="some-project" name="test\""""
in result.stdout
)

0 comments on commit daf8041

Please sign in to comment.