Add support for GitHub Annotations output format

Support the format used by GitHub Actions to annotate pull
requests with linter failures
This commit is contained in:
Andrew Imeson 2020-09-22 22:51:43 -04:00 committed by Adrien Vergé
parent 549b136a04
commit 50c7453824
2 changed files with 31 additions and 1 deletions

View File

@ -549,6 +549,20 @@ class CommandLineTestCase(unittest.TestCase):
self.assertEqual(
(ctx.returncode, ctx.stdout, ctx.stderr), (1, expected_out, ''))
def test_run_format_github(self):
path = os.path.join(self.wd, 'a.yaml')
with RunContext(self) as ctx:
cli.run((path, '--format', 'github'))
expected_out = (
'::error file=%s,line=2,col=4::[trailing-spaces] trailing'
' spaces\n'
'::error file=%s,line=3,col=4::[new-line-at-end-of-file] no'
' new line character at the end of file\n'
% (path, path))
self.assertEqual(
(ctx.returncode, ctx.stdout, ctx.stderr), (1, expected_out, ''))
def test_run_read_from_stdin(self):
# prepares stdin with an invalid yaml string so that we can check
# for its specific error, and be assured that stdin was read

View File

@ -85,6 +85,19 @@ class Format(object):
line += ' \033[2m(%s)\033[0m' % problem.rule
return line
@staticmethod
def github(problem, filename):
line = '::'
line += problem.level
line += ' file=' + filename + ','
line += 'line=' + format(problem.line) + ','
line += 'col=' + format(problem.column)
line += '::'
if problem.rule:
line += '[' + problem.rule + '] '
line += problem.desc
return line
def show_problems(problems, file, args_format, no_warn):
max_level = 0
@ -96,6 +109,8 @@ def show_problems(problems, file, args_format, no_warn):
continue
if args_format == 'parsable':
print(Format.parsable(problem, file))
elif args_format == 'github':
print(Format.github(problem, file))
elif args_format == 'colored' or \
(args_format == 'auto' and supports_color()):
if first:
@ -131,7 +146,8 @@ def run(argv=None):
action='store',
help='custom configuration (as YAML source)')
parser.add_argument('-f', '--format',
choices=('parsable', 'standard', 'colored', 'auto'),
choices=('parsable', 'standard', 'colored', 'github',
'auto'),
default='auto', help='format for parsing output')
parser.add_argument('-s', '--strict',
action='store_true',