Skip to content

Commit 6c05060

Browse files
authored
[BRE-541] Handling steps where no comment is added to uses (#44)
1 parent 66afb91 commit 6c05060

File tree

3 files changed

+82
-4
lines changed

3 files changed

+82
-4
lines changed

src/bitwarden_workflow_linter/models/step.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,13 @@ def init(cls: Self, idx: int, job: str, data: CommentedMap) -> Self:
3939
new_step.key = idx
4040
new_step.job = job
4141

42-
if "uses" in data.ca.items and data.ca.items["uses"][2]:
43-
new_step.uses_comment = data.ca.items["uses"][2].value.replace("\n", "")
42+
if new_step.uses:
43+
if "uses" in data.ca.items and data.ca.items["uses"][2]:
44+
new_step.uses_comment = data.ca.items["uses"][2].value.replace("\n", "")
45+
new_step.uses_version = new_step.uses_comment.split(" ")[-1]
4446
if "@" in new_step.uses:
4547
new_step.uses_path, new_step.uses_ref = new_step.uses.split("@")
46-
new_step.uses_version = new_step.uses_comment.split(" ")[-1]
48+
else:
49+
new_step.uses_path = new_step.uses
4750

4851
return new_step

tests/rules/test_step_approved.py

+9
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,11 @@ def fixture_incorrect_workflow():
7070
- name: Checkout Branch
7171
uses: joseph-flinn/action-DNE@main
7272
73+
- name: Checkout Branch with Ref
74+
uses: notbitwarden/subfolder/action-DNE@main
75+
with:
76+
ref: main
77+
7378
"""
7479
return WorkflowBuilder.build(workflow=yaml.load(workflow), from_file=False)
7580

@@ -98,6 +103,10 @@ def test_rule_on_incorrect_workflow(rule, incorrect_workflow):
98103
assert result is False
99104
assert "New Action detected" in message
100105

106+
result, message = rule.fn(incorrect_workflow.jobs["job-key"].steps[1])
107+
assert result is False
108+
assert "New Action detected" in message
109+
101110

102111
def test_fail_compatibility(rule, correct_workflow):
103112
finding = rule.execute(correct_workflow)

tests/test_step.py

+67-1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,45 @@ def fixture_uses_step():
3636
return Step.init(0, "default", step_yaml)
3737

3838

39+
@pytest.fixture(name="uses_step_no_comments")
40+
def fixture_uses_step_no_comments():
41+
step_str = """\
42+
name: Download Artifacts
43+
uses: bitwarden/download-artifacts@main
44+
with:
45+
workflow: upload-test-artifacts.yml
46+
artifacts: artifact
47+
path: artifact
48+
branch: main
49+
50+
"""
51+
yaml = YAML()
52+
step_yaml = yaml.load(step_str)
53+
return Step.init(0, "default", step_yaml)
54+
55+
56+
@pytest.fixture(name="uses_step_no_ref")
57+
def fixture_uses_step_no_ref():
58+
step_str = """\
59+
name: Run Local Workflow
60+
uses: ./.github/workflows/_local.yml
61+
"""
62+
yaml = YAML()
63+
step_yaml = yaml.load(step_str)
64+
return Step.init(0, "default", step_yaml)
65+
66+
67+
@pytest.fixture(name="uses_step_no_ref_with_comments")
68+
def fixture_uses_step_no_ref_with_comments():
69+
step_str = """\
70+
name: Run Local Workflow
71+
uses: ./.github/workflows/_local.yml # A comment
72+
"""
73+
yaml = YAML()
74+
step_yaml = yaml.load(step_str)
75+
return Step.init(0, "default", step_yaml)
76+
77+
3978
def test_step_default(default_step):
4079
assert default_step.key == 0
4180
assert default_step.job == "default"
@@ -74,5 +113,32 @@ def test_step_keyword_field(uses_step):
74113
def test_step_comment(uses_step):
75114
assert uses_step.key == 0
76115
assert uses_step.job == "default"
77-
assert uses_step.uses_comment is not None
116+
assert uses_step.uses_ref == "main"
117+
assert uses_step.uses_version == "v1.0.0"
78118
assert uses_step.uses_comment == "# v1.0.0"
119+
120+
121+
def test_step_no_comments(uses_step_no_comments):
122+
assert uses_step_no_comments.key == 0
123+
assert uses_step_no_comments.job == "default"
124+
assert uses_step_no_comments.uses_ref == "main"
125+
assert uses_step_no_comments.uses_version is None
126+
assert uses_step_no_comments.uses_comment is None
127+
128+
129+
def test_step_no_ref(uses_step_no_ref):
130+
assert uses_step_no_ref.key == 0
131+
assert uses_step_no_ref.job == "default"
132+
assert uses_step_no_ref.uses_ref is None
133+
assert uses_step_no_ref.uses_version is None
134+
assert uses_step_no_ref.uses_comment is None
135+
136+
137+
def test_step_no_ref_with_comments(uses_step_no_ref_with_comments):
138+
assert uses_step_no_ref_with_comments.key == 0
139+
assert uses_step_no_ref_with_comments.job == "default"
140+
assert uses_step_no_ref_with_comments.uses_ref is None
141+
assert (
142+
uses_step_no_ref_with_comments.uses_version == "comment"
143+
) # We are not currently validating the version matches a specific format
144+
assert uses_step_no_ref_with_comments.uses_comment == "# A comment"

0 commit comments

Comments
 (0)