Skip to content

Commit cfcf1c8

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

File tree

4 files changed

+56
-2
lines changed

4 files changed

+56
-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

+17
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,23 @@ def split_diff_content_by_filename(output: str) -> {str: str}:
9393
return content_by_filename
9494

9595

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

tests/conftest.py

+7
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,10 @@
99
def fixture_dir():
1010
"""Return the path to the fixture directory."""
1111
return FIXTURE_DIR
12+
13+
14+
@pytest.fixture(scope="session")
15+
def env(monkeypatch):
16+
"""Remove GITHUB_ACTIONS the environment as it is part of our feature set."""
17+
with monkeypatch.delenv("GITHUB_ACTIONS"):
18+
yield

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+
with (fixture_dir / ".relint.yml").open() as fs:
51+
config = fs.read()
52+
tmpdir.join(".relint.yml").write(config)
53+
tmpdir.join("dummy.py").write("# FIXME do something")
54+
with monkeypatch.setenv("GITHUB_ACTIONS", "true"):
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)