Skip to content

Commit fb59cf5

Browse files
committed
Resolve #91 -- Add Github workflow message support for GitHub Actions
1 parent 0146df5 commit fb59cf5

File tree

3 files changed

+52
-2
lines changed

3 files changed

+52
-2
lines changed

relint/__main__.py

+13-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
11
import argparse
2+
import os
23
import subprocess # nosec
34
import sys
45
import warnings
56

67
from rich.progress import track
78

89
from relint.config import load_config
9-
from relint.parse import lint_file, match_with_diff_changes, parse_diff, print_culprits
10+
from relint.parse import (
11+
lint_file,
12+
match_with_diff_changes,
13+
parse_diff,
14+
print_culprits,
15+
print_github_actions_output,
16+
)
1017

1118

1219
def parse_args(args=None):
@@ -90,7 +97,11 @@ def main(args=None):
9097
changed_content = parse_diff(output)
9198
matches = match_with_diff_changes(changed_content, matches)
9299

93-
exit_code = print_culprits(matches, args)
100+
GITHUB_ACTIONS = os.getenv("GITHUB_ACTIONS") == "true"
101+
if GITHUB_ACTIONS:
102+
exit_code = print_github_actions_output(matches, args)
103+
else:
104+
exit_code = print_culprits(matches, args)
94105
exit(exit_code)
95106

96107

relint/parse.py

+20
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from __future__ import annotations
22

33
import collections
4+
import os
45

56
try:
67
import regex as re
@@ -93,6 +94,25 @@ def split_diff_content_by_filename(output: str) -> {str: str}:
9394
return content_by_filename
9495

9596

97+
def print_github_actions_output(matches, args):
98+
exit_code = 0
99+
for filename, test, match, line_number in matches:
100+
exit_code = test.error if exit_code == 0 else exit_code
101+
start_line_no = match.string[: match.start()].count("\n") + 1
102+
end_line_no = match.string[: match.end()].count("\n") + 1
103+
col = match.start() - match.string.rfind("\n", 0, match.start())
104+
col_end = match.end() - match.string.rfind("\n", 0, match.end())
105+
106+
print(
107+
f"::{'error' if test.error else 'warning'} file={filename},"
108+
f"line={start_line_no},endLine={end_line_no},col={col},colEnd={col_end},"
109+
f"title={test.name}::{test.hint}".replace(
110+
"\n", "%0A"
111+
)
112+
)
113+
return exit_code
114+
115+
96116
def print_culprits(matches, args):
97117
exit_code = 0
98118
messages = []

tests/test_main.py

+19
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,25 @@ def test_main_execution_with_error(self, capsys, tmpdir, fixture_dir):
4444
assert "❱ 1 # FIXME do something" in out
4545
assert exc_info.value.code == 1
4646

47+
def test_main_execution_with_error__github_workflow_output(
48+
self, monkeypatch, capsys, tmpdir, fixture_dir
49+
):
50+
monkeypatch.setenv("GITHUB_ACTIONS", "true")
51+
with (fixture_dir / ".relint.yml").open() as fs:
52+
config = fs.read()
53+
tmpdir.join(".relint.yml").write(config)
54+
tmpdir.join("dummy.py").write("# FIXME do something")
55+
with tmpdir.as_cwd():
56+
with pytest.raises(SystemExit) as exc_info:
57+
main(["dummy.py"])
58+
59+
out, _ = capsys.readouterr()
60+
assert (
61+
"::error file=dummy.py,line=1,endLine=1,col=3,colEnd=8,title=No fixme (warning)::### This is a multiline hint%0AFix it right away!%0A%0AYou can use code blocks too, like Python:%0A%0A"
62+
in out
63+
)
64+
assert exc_info.value.code == 1
65+
4766
@pytest.mark.parametrize("args", [[], ["--summarize"]])
4867
def test_main_execution_without_hint(self, args, capsys, tmpdir, fixture_dir):
4968
with (fixture_dir / ".relint.yml").open() as fs:

0 commit comments

Comments
 (0)