Skip to content

Commit 50c7453

Browse files
andrewimesonadrienverge
authored andcommitted
Add support for GitHub Annotations output format
Support the format used by GitHub Actions to annotate pull requests with linter failures
1 parent 549b136 commit 50c7453

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

tests/test_cli.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -549,6 +549,20 @@ def test_run_format_colored(self):
549549
self.assertEqual(
550550
(ctx.returncode, ctx.stdout, ctx.stderr), (1, expected_out, ''))
551551

552+
def test_run_format_github(self):
553+
path = os.path.join(self.wd, 'a.yaml')
554+
555+
with RunContext(self) as ctx:
556+
cli.run((path, '--format', 'github'))
557+
expected_out = (
558+
'::error file=%s,line=2,col=4::[trailing-spaces] trailing'
559+
' spaces\n'
560+
'::error file=%s,line=3,col=4::[new-line-at-end-of-file] no'
561+
' new line character at the end of file\n'
562+
% (path, path))
563+
self.assertEqual(
564+
(ctx.returncode, ctx.stdout, ctx.stderr), (1, expected_out, ''))
565+
552566
def test_run_read_from_stdin(self):
553567
# prepares stdin with an invalid yaml string so that we can check
554568
# for its specific error, and be assured that stdin was read

yamllint/cli.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,19 @@ def standard_color(problem, filename):
8585
line += ' \033[2m(%s)\033[0m' % problem.rule
8686
return line
8787

88+
@staticmethod
89+
def github(problem, filename):
90+
line = '::'
91+
line += problem.level
92+
line += ' file=' + filename + ','
93+
line += 'line=' + format(problem.line) + ','
94+
line += 'col=' + format(problem.column)
95+
line += '::'
96+
if problem.rule:
97+
line += '[' + problem.rule + '] '
98+
line += problem.desc
99+
return line
100+
88101

89102
def show_problems(problems, file, args_format, no_warn):
90103
max_level = 0
@@ -96,6 +109,8 @@ def show_problems(problems, file, args_format, no_warn):
96109
continue
97110
if args_format == 'parsable':
98111
print(Format.parsable(problem, file))
112+
elif args_format == 'github':
113+
print(Format.github(problem, file))
99114
elif args_format == 'colored' or \
100115
(args_format == 'auto' and supports_color()):
101116
if first:
@@ -131,7 +146,8 @@ def run(argv=None):
131146
action='store',
132147
help='custom configuration (as YAML source)')
133148
parser.add_argument('-f', '--format',
134-
choices=('parsable', 'standard', 'colored', 'auto'),
149+
choices=('parsable', 'standard', 'colored', 'github',
150+
'auto'),
135151
default='auto', help='format for parsing output')
136152
parser.add_argument('-s', '--strict',
137153
action='store_true',

0 commit comments

Comments
 (0)