Skip to content

Commit 40c514a

Browse files
committed
simplify warning/failure logging
1 parent f757ad2 commit 40c514a

File tree

1 file changed

+26
-54
lines changed

1 file changed

+26
-54
lines changed

gateway/action_tags.py

Lines changed: 26 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,12 @@ def log(self, message: str) -> None:
3535
print(message)
3636
self.logs.append(message)
3737

38-
def failure(self, message: str) -> None:
38+
def failure(self, message: str, indent: str) -> None:
39+
self.log(f"{indent}{message}")
3940
self.failures.append(message)
4041

41-
def warning(self, message: str) -> None:
42+
def warning(self, message: str, indent: str) -> None:
43+
self.log(f"{indent}{message}")
4244
self.warnings.append(message)
4345

4446
def has_failures(self) -> bool:
@@ -106,15 +108,15 @@ def verify_actions(actions: Path | ActionsYAML | str, log_to_console: bool = Tru
106108
* Issue a warning and stop if the name is like `docker:*` (not implemented)
107109
* Issue an error and stop if the name doesn't start with an `OWNER/REPO` pattern.
108110
* Each expired entry is just skipped
109-
* If there is a wildcard reference and a SHA reference, issue an error.
111+
* If there is a wildcard reference and an SHA reference, issue an error.
110112
111113
Then, for each reference for an action:
112114
* If no `tag` is specified, let GH resolve the commit SHA.
113115
Emit a warning to add the value of the `tag` attribute if the SHA can be resolved.
114116
Otherwise, emit an error.
115117
* If `tag` is specified:
116118
* Add the SHA to the set of requested-shas-by-tag
117-
* Call GH's "matching-refs" endpoint for the 'tag' value
119+
* Call GitHub's "matching-refs" endpoint for the 'tag' value
118120
* Emit en error if the object type is not a tag or commit.
119121
* Also resolve 'tag' object types to 'commit' object types.
120122
* Add each returned SHA to the set of valid-shas-by-tag.
@@ -159,26 +161,20 @@ def verify_actions(actions: Path | ActionsYAML | str, log_to_console: bool = Tru
159161
# noinspection PyTypedDict
160162
ignore_gh_api_errors = details and 'ignore_gh_api_errors' in details and details['ignore_gh_api_errors'] == True
161163
if ignore_gh_api_errors:
162-
m = f"ignore_gh_api_errors is set to true: will ignore GH API errors for action {name} ref '{ref}'"
163-
result.log(f" .. ⚡ {m}")
164-
result.warning(m)
164+
result.warning(f"ignore_gh_api_errors is set to true: will ignore GH API errors for action {name} ref '{ref}'", " ..")
165165

166166
if ref == '*':
167167
# "wildcard" SHA - what would we...
168168
result.log(f" .. detected wildcard ref")
169169
if len(requested_shas_by_tag) > 0 and not has_wildcard_msg_emitted:
170-
m = f"GitHub action {name} references a wildcard SHA but also has specific SHAs"
171-
result.log(f" .. ⚡ {m}")
172-
result.warning(m)
170+
result.warning(f"GitHub action {name} references a wildcard SHA but also has specific SHAs", " ..")
173171
has_wildcard_msg_emitted = True
174172
has_wildcard = True
175173
continue
176174
elif re.match(re_git_sha, ref):
177175
result.log(f" .. detected entry with Git SHA '{ref}'")
178176
if has_wildcard and not has_wildcard_msg_emitted:
179-
m = f"GitHub action {name} references a wildcard SHA but also has specific SHAs"
180-
result.log(f" .. ⚡ {m}")
181-
result.warning(m)
177+
result.warning(f"GitHub action {name} references a wildcard SHA but also has specific SHAs", " ..")
182178
has_wildcard_msg_emitted = True
183179

184180
if not details or not 'tag' in details:
@@ -187,22 +183,16 @@ def verify_actions(actions: Path | ActionsYAML | str, log_to_console: bool = Tru
187183
response = _gh_get_commit_object(owner_repo, ref)
188184
match response.status:
189185
case 200:
190-
m = f"GitHub action {name} references existing commit SHA '{ref}' but does not specify the tag name for it."
191-
result.log(f" .. ⚡ {m}")
192-
result.warning(m)
186+
result.warning(f"GitHub action {name} references existing commit SHA '{ref}' but does not specify the tag name for it.", " ..")
193187
case 404:
194-
m = f"GitHub action {name} references non existing commit SHA '{ref}': HTTP/{response.status}: {response.reason}, API URL: {response.req_url}"
195-
result.log(f" .. ❌ {m}")
196-
result.failure(m)
188+
result.failure(f"GitHub action {name} references non existing commit SHA '{ref}': HTTP/{response.status}: {response.reason}, API URL: {response.req_url}", " ..")
197189
case _:
198190
m = f"Failed to fetch Git SHA '{ref}' from GitHub repo 'https://github.com/{owner_repo}': HTTP/{response.status}: {response.reason}, API URL: {response.req_url}\n{response.body}"
199191
if ignore_gh_api_errors:
200192
has_ignored_api_errors = True
201-
result.log(f" .. ⚡ {m}")
202-
result.warning(m)
193+
result.warning(m, " ..")
203194
else:
204-
result.log(f" .. ❌ {m}")
205-
result.failure(m)
195+
result.failure(m, " ..")
206196
else:
207197
tag: str = details.get('tag')
208198
result.log(f" .. collecting Git SHAs for tag {tag}")
@@ -242,34 +232,24 @@ def verify_actions(actions: Path | ActionsYAML | str, log_to_console: bool = Tru
242232
m = f"Failed to fetch details for Git tag '{tag}' from GitHub repo 'https://github.com/{owner_repo}': HTTP/{response2.status}: {response2.reason}, API URL: {response2.req_url}\n{response2.body}"
243233
if ignore_gh_api_errors:
244234
has_ignored_api_errors = True
245-
result.log(f" .. ⚡ {m}")
246-
result.warning(m)
235+
result.warning(m, " ..")
247236
else:
248-
result.log(f" .. ❌ {m}")
249-
result.failure(m)
237+
result.failure(m, " ..")
250238
case "commit":
251239
valid_shas_for_tag.add(tag_object_sha)
252240
case "branch":
253-
m = f"Branch references mentioned for Git tag '{tag}' for GitHub action {name}"
254-
result.log(f" .. ❌ {m}")
255-
result.failure(m)
241+
result.failure(f"Branch references mentioned for Git tag '{tag}' for GitHub action {name}", " ..")
256242
case _:
257-
m = f"Invalid Git object type '{tag_object['type']}' for Git tag '{tag}' in GitHub repo 'https://github.com/{owner_repo}'"
258-
result.log(f" .. ❌ {m}")
259-
result.failure(m)
243+
result.failure(f"Invalid Git object type '{tag_object['type']}' for Git tag '{tag}' in GitHub repo 'https://github.com/{owner_repo}'", " ..")
260244
case _:
261245
m = f"Failed to fetch matching Git tags for '{tag}' from GitHub repo 'https://github.com/{owner_repo}': HTTP/{response.status}: {response.reason}, API URL: {response.req_url}\n{response.body}"
262246
if ignore_gh_api_errors:
263-
result.log(f" .. ⚡ {m}")
264-
result.warning(m)
247+
result.warning(m, " ..")
265248
has_ignored_api_errors = True
266249
else:
267-
result.log(f" .. ❌ {m}")
268-
result.failure(m)
250+
result.failure(m, " ..")
269251
else:
270-
m = f"GitHub action {name} references an invalid Git SHA '{ref}'"
271-
result.log(f" .. ❌ {m}")
272-
result.failure(m)
252+
result.failure(f"GitHub action {name} references an invalid Git SHA '{ref}'", " ..")
273253

274254
for req_tag, req_shas in requested_shas_by_tag.items():
275255
result.log(f" .. checking tag '{req_tag}'")
@@ -279,32 +259,24 @@ def verify_actions(actions: Path | ActionsYAML | str, log_to_console: bool = Tru
279259
if not valid_shas:
280260
m = f"GitHub action {name} references Git tag '{req_tag}' via SHAs '{req_shas}' but no SHAs for tag could be found - does the Git tag exist?"
281261
if has_ignored_api_errors:
282-
result.warning(m)
283-
result.log(f" ⚡ {m}")
262+
result.warning(m, "")
284263
else:
285-
result.failure(m)
286-
result.log(f" ❌ {m}")
264+
result.failure(m, "")
287265
elif req_shas.isdisjoint(valid_shas):
288266
m = f"GitHub action {name} references Git tag '{req_tag}' via SHAs '{req_shas}' but none of those matches the valid SHAs '{valid_shas}'"
289-
result.failure(m)
290-
result.log(f" ❌ {m}")
267+
result.failure(m, "")
291268
else:
292269
result.log(f" ✅ GitHub action {name} definition for tag '{req_tag}' is good!")
293270

294271
elif re.match(re_github_actions_repo_wildcard, name):
295-
m =f"Ignoring '{name}' because it uses a GitHub repository wildcard ..."
296-
result.warning(m)
297-
result.log(f"⚡ {m}")
272+
result.warning(f"Ignoring '{name}' because it uses a GitHub repository wildcard ...", "")
298273

299274
elif re.match(re_docker_image, name):
300-
m =f"Ignoring '{name}' because it references a Docker image ..."
301-
result.warning(m)
302-
result.log(f"⚡ {m}")
275+
result.warning(f"Ignoring '{name}' because it references a Docker image ...", "")
303276

304277
else:
305278
m = f"Cannot determine action kind for '{name}'"
306-
result.failure(m)
307-
result.log(f"❌ {m}")
279+
result.failure(m, "")
308280

309281
if on_gha():
310282
if result.has_failures() or result.has_warnings():

0 commit comments

Comments
 (0)