Skip to content

Commit f0997dc

Browse files
Eeebrupixman20
andauthored
[BRE-443] - Fix bwwl Linting pre Deployment - work on the bwwl action sub command (#36)
* activate approved actions * [BRE-443] - Fix bwwl Linting pre Deployment - work on the bwwl action sub command Co-authored-by: Andy Pixley <[email protected]> * use regex to escape matrix in name --------- Co-authored-by: Andy Pixley <[email protected]>
1 parent 3c98908 commit f0997dc

File tree

11 files changed

+237
-146
lines changed

11 files changed

+237
-146
lines changed

src/bitwarden_workflow_linter/actions.py

Lines changed: 44 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -57,17 +57,22 @@ def extend_parser(
5757
parser_actions = subparsers.add_parser(
5858
"actions", help="!!BETA!!\nAdd or Update Actions in the pre-approved list."
5959
)
60-
parser_actions.add_argument(
61-
"-o", "--output", action="store", default="actions.json"
62-
)
6360
subparsers_actions = parser_actions.add_subparsers(
6461
required=True, dest="actions_command"
6562
)
66-
subparsers_actions.add_parser("update", help="update action versions")
63+
parser_actions_update = subparsers_actions.add_parser(
64+
"update", help="update action versions"
65+
)
66+
parser_actions_update.add_argument(
67+
"-o", "--output", action="store", default="actions.json", help="output file"
68+
)
6769
parser_actions_add = subparsers_actions.add_parser(
6870
"add", help="add action to approved list"
6971
)
7072
parser_actions_add.add_argument("name", help="action name [git owner/repo]")
73+
parser_actions_add.add_argument(
74+
"-o", "--output", action="store", default="actions.json", help="output file"
75+
)
7176

7277
return subparsers
7378

@@ -127,29 +132,38 @@ def get_latest_version(self, action: Action) -> Action | None:
127132
f"https://api.github.com/repos/{action.name}/releases/latest",
128133
action.name,
129134
)
130-
if not response:
131-
return None
135+
if response is not None and response.status != 404:
136+
tag_name = json.loads(response.data)["tag_name"]
132137

133-
tag_name = json.loads(response.data)["tag_name"]
138+
# Get the URL to the commit for the tag
139+
response = self.get_github_api_response(
140+
f"https://api.github.com/repos/{action.name}/git/ref/tags/{tag_name}",
141+
action.name,
142+
)
134143

135-
# Get the URL to the commit for the tag
136-
response = self.get_github_api_response(
137-
f"https://api.github.com/repos/{action.name}/git/ref/tags/{tag_name}",
138-
action.name,
139-
)
140-
if not response:
141-
return None
144+
if response is None or response.status != 200:
145+
return None
146+
147+
if json.loads(response.data)["object"]["type"] != "commit":
148+
url = json.loads(response.data)["object"]["url"]
149+
# Follow the URL and get the commit sha for tags
150+
response = self.get_github_api_response(url, action.name)
151+
if not response:
152+
return None
142153

143-
if json.loads(response.data)["object"]["type"] == "commit":
144154
sha = json.loads(response.data)["object"]["sha"]
145155
else:
146-
url = json.loads(response.data)["object"]["url"]
147-
# Follow the URL and get the commit sha for tags
148-
response = self.get_github_api_response(url, action.name)
149-
if not response:
156+
# Get tag from latest tag
157+
response = self.get_github_api_response(
158+
f"https://api.github.com/repos/{action.name}/tags",
159+
action.name,
160+
)
161+
162+
if response is None or response.status != 200:
150163
return None
151164

152-
sha = json.loads(response.data)["object"]["sha"]
165+
sha = json.loads(response.data)[0]["commit"]["sha"]
166+
tag_name = json.loads(response.data)[0]["name"]
153167
except KeyError as err:
154168
raise GitHubApiSchemaError(
155169
f"Error with the GitHub API Response Schema for either /releases or"
@@ -182,10 +196,20 @@ def add(self, new_action_name: str, filename: str) -> int:
182196
updated_actions = self.settings.approved_actions
183197
proposed_action = Action(name=new_action_name)
184198

199+
# Remove the action directory if the action is in a multi-actions repo
200+
if len(new_action_name.split("/")) > 2:
201+
modified_action = "/".join(new_action_name.split("/")[:-1])
202+
print(
203+
f" - {new_action_name} \033[{Colors.yellow}modified\033[0m to {modified_action}"
204+
)
205+
proposed_action = Action(name=modified_action)
206+
185207
if self.exists(proposed_action):
186208
latest = self.get_latest_version(proposed_action)
187209
if latest:
188210
updated_actions[latest.name] = latest
211+
else:
212+
print(f" - {new_action_name} \033[{Colors.red}not found\033[0m")
189213

190214
self.save_actions(updated_actions, filename)
191215
return 0

0 commit comments

Comments
 (0)