-
Notifications
You must be signed in to change notification settings - Fork 1
github_annotation: Refactor shebang from bash to python #4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
99864da
8b0e7a3
ea080c9
c1ab27c
09077b9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,87 @@ | ||||||||||||
#!/usr/bin/env python3 | ||||||||||||
|
||||||||||||
import os | ||||||||||||
import subprocess | ||||||||||||
import json | ||||||||||||
import sys | ||||||||||||
import shlex | ||||||||||||
from typing import Any | ||||||||||||
|
||||||||||||
|
||||||||||||
def emit_annotation_for_clippy(results: list[dict[str, Any]], with_annotation: bool): | ||||||||||||
total_count = 0 | ||||||||||||
limit = 10 | ||||||||||||
|
||||||||||||
severenty_map = { | ||||||||||||
"help": "notice", | ||||||||||||
"note": "notice", | ||||||||||||
"warning": "warning", | ||||||||||||
"error": "error", | ||||||||||||
} | ||||||||||||
for item in results: | ||||||||||||
if total_count >= limit: | ||||||||||||
break | ||||||||||||
|
||||||||||||
message = item.get("message") | ||||||||||||
if not message: | ||||||||||||
continue | ||||||||||||
|
||||||||||||
spans = message.get("spans") or [] | ||||||||||||
primary_span = next((span for span in spans if span.get("is_primary")), None) | ||||||||||||
if not primary_span: | ||||||||||||
continue | ||||||||||||
|
||||||||||||
level = severenty_map.get(message.get("level"), "error") | ||||||||||||
title = message.get("message", "") | ||||||||||||
rendered_message = message.get("rendered", "") | ||||||||||||
|
||||||||||||
file_name = (primary_span["file_name"],) | ||||||||||||
line_start = (primary_span["line_start"],) | ||||||||||||
line_end = (primary_span["line_end"],) | ||||||||||||
column_start = (primary_span["column_start"],) | ||||||||||||
column_end = (primary_span["column_end"],) | ||||||||||||
|
||||||||||||
line_info = f"line={line_start},endLine={line_end},title={title}" | ||||||||||||
|
||||||||||||
column_info = "" | ||||||||||||
if ( | ||||||||||||
line_start == line_end | ||||||||||||
and column_end is not None | ||||||||||||
and column_start is not None | ||||||||||||
): | ||||||||||||
column_info = f"col={column_start},endColumn={column_end}," | ||||||||||||
if with_annotation: | ||||||||||||
print( | ||||||||||||
f"::{level} file={file_name},{column_info}{line_info}::{rendered_message}" | ||||||||||||
) | ||||||||||||
total_count += 1 | ||||||||||||
|
||||||||||||
return 1 if total_count > 0 else 0 | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||
|
||||||||||||
|
||||||||||||
def main(): | ||||||||||||
cmd = ["cargo"] | ||||||||||||
|
||||||||||||
cargo_command = os.getenv("INPUT_CARGO_COMMAND", "").strip() | ||||||||||||
with_annotation = ( | ||||||||||||
os.getenv("INPUT_WITH_ANNOTATION", "true").strip().lower() == "true" | ||||||||||||
) | ||||||||||||
|
||||||||||||
if cargo_command: | ||||||||||||
args = shlex.split(cargo_command) | ||||||||||||
cmd.extend(args) | ||||||||||||
|
||||||||||||
if "--message-format=json" not in cmd: | ||||||||||||
cmd.append("--message-format=json") | ||||||||||||
|
||||||||||||
proc = subprocess.run(cmd, capture_output=with_annotation) | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If the idea is to parse the output of cargo to determine the exit status, then shouldn't we always capture the output, regardless of |
||||||||||||
clippy_results = [ | ||||||||||||
json.loads(line) for line in proc.stdout.splitlines() if line.strip() | ||||||||||||
] | ||||||||||||
|
||||||||||||
result = emit_annotation_for_clippy(clippy_results, with_annotation) | ||||||||||||
sys.exit(result) | ||||||||||||
Comment on lines
+82
to
+83
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think you can simplify this a bit, by only parsing annotations when
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, clippy will not return a failure code unless There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Makes sense. Thank you! |
||||||||||||
|
||||||||||||
|
||||||||||||
if __name__ == "__main__": | ||||||||||||
main() |
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.