Skip to content

Commit

Permalink
feat: support workflow commands for GitHub Actions
Browse files Browse the repository at this point in the history
  • Loading branch information
lljbash committed Feb 4, 2024
1 parent dbb1cc1 commit 636c50e
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 7 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ options:
exit with a non-zero status. Candidates: error, warn,
info, hint. [default: hint]
--tqdm Show a progress bar (require tqdm).
--github Append workflow commands for GitHub Actions to output.
--git-root GIT_ROOT Root directory of the git repository. Only works with
--github. [default: current directory]
-v, --verbose Print verbose output from clangd.

Find more information on https://github.com/lljbash/clangd-tidy.
Expand Down
61 changes: 54 additions & 7 deletions clangd-tidy
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,18 @@ class DiagnosticCollector:
3: "Information",
4: "Hint",
}

SEVERITY_INT = {
"error": 1,
"warn": 2,
"info": 3,
"hint": 4,
}
SEVERITY_GITHUB = {
1: "error",
2: "warning",
3: "notice",
4: "notice",
}

def __init__(self):
self.diagnostics = {}
Expand Down Expand Up @@ -120,17 +125,42 @@ class DiagnosticCollector:
f" {self.SEVERITY[severity]}" if severity else "",
f" [{code}]" if code else "",
)
line = diagnostic["range"]["start"]["line"] + 1
col = diagnostic["range"]["start"]["character"] + 1
message = diagnostic["message"]
if source is None and code is None:
continue
fancy_output += "- line {}, col {}:{}\n{}\n\n".format(
diagnostic["range"]["start"]["line"] + 1,
diagnostic["range"]["start"]["character"] + 1,
extra_info,
diagnostic["message"],
)
fancy_output += f"- line {line}, col {col}:{extra_info}\n{message}\n\n"
fancy_output += "\n"
return fancy_output

def workflow_commands_for_github_actions(self, git_root: str) -> str:
commands = "::group::{workflow commands}\n"
for file, diagnostics in sorted(self.diagnostics.items()):
if len(diagnostics) == 0:
continue
for diagnostic in diagnostics:
source = diagnostic.get("source", None)
severity = diagnostic.get("severity", None)
code = diagnostic.get("code", None)
extra_info = "{}{}{}".format(
f"{source}" if source else "",
f" {self.SEVERITY[severity]}" if severity else "",
f" [{code}]" if code else "",
)
line = diagnostic["range"]["start"]["line"] + 1
end_line = diagnostic["range"]["end"]["line"] + 1
col = diagnostic["range"]["start"]["character"] + 1
end_col = diagnostic["range"]["end"]["character"] + 1
message = diagnostic["message"]
if source is None and code is None:
continue
command = self.SEVERITY_GITHUB[severity]
rel_file = os.path.relpath(file, git_root)
commands += f"::{command} file={rel_file},line={line},endLine={end_line},col={col},endCol={end_col},title={extra_info}::{message}\n"
commands += "::endgroup::"
return commands


if __name__ == "__main__":
DEFAULT_ALLOW_EXTENSIONS = [
Expand Down Expand Up @@ -191,6 +221,16 @@ if __name__ == "__main__":
parser.add_argument(
"--tqdm", action="store_true", help="Show a progress bar (require tqdm)."
)
parser.add_argument(
"--github",
action="store_true",
help="Append workflow commands for GitHub Actions to output.",
)
parser.add_argument(
"--git-root",
default=os.getcwd(),
help="Root directory of the git repository. Only works with --github. [default: current directory]",
)
parser.add_argument(
"-v", "--verbose", action="store_true", help="Print verbose output from clangd."
)
Expand Down Expand Up @@ -266,6 +306,8 @@ if __name__ == "__main__":
args.tqdm = False

if args.tqdm:
from tqdm import tqdm

with tqdm(total=len(files)) as pbar:
collector.cond.acquire()
while len(collector.diagnostics) < len(files):
Expand All @@ -288,5 +330,10 @@ if __name__ == "__main__":

diagnostics = collector.fancy_diagnostics().strip()
print(diagnostics, file=args.output)
if args.github:
print(
collector.workflow_commands_for_github_actions(args.git_root).strip(),
file=args.output,
)
if collector.check_failed(args.fail_on_severity):
exit(1)

0 comments on commit 636c50e

Please sign in to comment.