Skip to content

Commit

Permalink
Run black over the code
Browse files Browse the repository at this point in the history
  • Loading branch information
joseph-flinn committed Jan 9, 2024
1 parent 819ca78 commit 060a33d
Show file tree
Hide file tree
Showing 10 changed files with 45 additions and 44 deletions.
1 change: 0 additions & 1 deletion lint-workflow/src/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,4 +187,3 @@ def update(self, filename: str) -> None:
updated_actions[action.name] = latest_release

self.save_actions(updated_actions, filename)

5 changes: 2 additions & 3 deletions lint-workflow/src/lint.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class LinterCmd:
def __init__(self, settings: Settings = None) -> None:
"""Command to lint GitHub Action Workflow files
This class contains logic to lint workflows that are passed in.
This class contains logic to lint workflows that are passed in.
Supporting logic is supplied to:
- build out the list of Rules desired
- select and validate the workflow files to lint
Expand Down Expand Up @@ -149,8 +149,7 @@ def run(self, input_files: list[str], strict: bool = False) -> int:

if len(input_files) > 0:
return_code = reduce(
lambda a, b: a if a > b else b,
map(self.lint_file, files)
lambda a, b: a if a > b else b, map(self.lint_file, files)
)

if return_code == 1 and not strict:
Expand Down
1 change: 1 addition & 0 deletions lint-workflow/src/load.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

class WorkflowBuilderError(Exception):
"""Custom Exception to indicate an error with the WorkflowBuilder."""

pass


Expand Down
1 change: 1 addition & 0 deletions lint-workflow/src/rule.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

class Rule:
"""Base class of a Rule to extend to create a linting Rule."""

message: str = "error"
on_fail: str = "error"
compatibility: List[Union[Workflow, Job, Step]] = [Workflow, Job, Step]
Expand Down
2 changes: 1 addition & 1 deletion lint-workflow/src/rules/step_approved.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def fn(self, obj: Step) -> Tuple[bool, str]:
run: echo "test"
In this example, 'actions/checkout' must be on the pre-approved list
and the metadata must match in order to succeed. The other three
and the metadata must match in order to succeed. The other three
Steps will be skipped.
"""
if self.skip(obj):
Expand Down
4 changes: 2 additions & 2 deletions lint-workflow/src/rules/step_pinned.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def fn(self, obj: Step) -> Tuple[bool, str]:
break the pipelines or be the entry point to a supply chain attack.
Pinning internal Actions to branches allow for less updates as work
is done on those repos. This is mainly to support our Action
is done on those repos. This is mainly to support our Action
monorepo architecture of our Actions.
Example:
Expand All @@ -52,7 +52,7 @@ def fn(self, obj: Step) -> Tuple[bool, str]:
- name: Test Run Action
run: echo "test"
In this example, 'actions/checkout' must be pinned to the full commit
In this example, 'actions/checkout' must be pinned to the full commit
of the tag while 'bitwarden/gh-actions/get-keyvault-secrets' must be
pinned to 'main'. The other two Steps will be skipped.
"""
Expand Down
8 changes: 5 additions & 3 deletions lint-workflow/src/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@ class Colors:
@dataclass
class LintLevel:
"""Class to contain the numeric level and color of linting."""

code: int
color: Colors


class LintLevels(LintLevel, Enum):
"""Collection of the different types of LintLevels available."""

NONE = 0, Colors.white
WARNING = 1, Colors.yellow
ERROR = 2, Colors.red
Expand All @@ -35,9 +37,7 @@ class LintFinding:
"""Represents a problem detected by linting."""

def __init__(
self,
description: str = "<no description>",
level: LintLevel = None
self, description: str = "<no description>", level: LintLevel = None
) -> None:
self.description = description
self.level = level
Expand All @@ -54,6 +54,7 @@ def __str__(self) -> str:
@dataclass
class Action:
"""Collection of the metadata associated with a GitHub Action."""

name: str
version: str = ""
sha: str = ""
Expand Down Expand Up @@ -89,6 +90,7 @@ def __ne__(self, other: Self) -> bool:

class SettingsError(Exception):
"""Custom Exception to indicate an error with loading Settings."""

pass


Expand Down
1 change: 0 additions & 1 deletion lint-workflow/tests/rules/test_name_capitalized.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ def missing_name_workflow():
return WorkflowBuilder.build(yaml=yaml.load(workflow), from_file=False)



@pytest.fixture
def rule():
return RuleNameCapitalized()
Expand Down
44 changes: 28 additions & 16 deletions lint-workflow/tests/test_lint.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,31 @@ def settings():
def test_get_max_error_level(settings):
linter = LinterCmd(settings=settings)

assert linter.get_max_error_level([
LintFinding(level=LintLevels.WARNING),
LintFinding(level=LintLevels.WARNING)
]) == 1

assert linter.get_max_error_level([
LintFinding(level=LintLevels.ERROR),
LintFinding(level=LintLevels.ERROR)
]) == 2

assert linter.get_max_error_level([
LintFinding(level=LintLevels.ERROR),
LintFinding(level=LintLevels.ERROR),
LintFinding(level=LintLevels.WARNING),
LintFinding(level=LintLevels.WARNING)
]) == 2
assert (
linter.get_max_error_level(
[
LintFinding(level=LintLevels.WARNING),
LintFinding(level=LintLevels.WARNING),
]
)
== 1
)

assert (
linter.get_max_error_level(
[LintFinding(level=LintLevels.ERROR), LintFinding(level=LintLevels.ERROR)]
)
== 2
)

assert (
linter.get_max_error_level(
[
LintFinding(level=LintLevels.ERROR),
LintFinding(level=LintLevels.ERROR),
LintFinding(level=LintLevels.WARNING),
LintFinding(level=LintLevels.WARNING),
]
)
== 2
)
22 changes: 5 additions & 17 deletions lint-workflow/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,7 @@


def test_action_eq():
action_def = {
"name": "bitwarden/sm-action",
"version": "1.0.0",
"sha": "some-sha"
}
action_def = {"name": "bitwarden/sm-action", "version": "1.0.0", "sha": "some-sha"}

action_a = Action(**action_def)
action_b = Action(**action_def)
Expand All @@ -25,16 +21,8 @@ def test_action_eq():


def test_action_ne():
action_a = Action(
name = "bitwarden/sm-action",
version = "1.0.0",
sha = "some-sha"
)
action_b = Action(
name = "bitwarden/sm-action",
version = "1.1.0",
sha = "some-other-sha"
)
action_a = Action(name="bitwarden/sm-action", version="1.0.0", sha="some-sha")
action_b = Action(name="bitwarden/sm-action", version="1.1.0", sha="some-other-sha")

assert (action_a == action_b) == False
assert (action_a != action_b) == True
Expand All @@ -48,7 +36,7 @@ def test_lint_level():

def test_lint_finding():
warning = LintFinding(level=LintLevels.WARNING)
assert str(warning) == '\x1b[33mwarning\x1b[0m <no description>'
assert str(warning) == "\x1b[33mwarning\x1b[0m <no description>"

error = LintFinding(level=LintLevels.ERROR)
assert str(error) == '\x1b[31merror\x1b[0m <no description>'
assert str(error) == "\x1b[31merror\x1b[0m <no description>"

0 comments on commit 060a33d

Please sign in to comment.