From 20993f9a429a2011d1d696d504b8ea1e6fac0375 Mon Sep 17 00:00:00 2001 From: lethosor Date: Tue, 30 Jun 2020 14:03:05 -0400 Subject: [PATCH 1/2] script-docs.py: add support for printing errors in GitHub actions format --- travis/lint.py | 2 +- travis/script-docs.py | 40 +++++++++++++++++++++++++++++----------- 2 files changed, 30 insertions(+), 12 deletions(-) diff --git a/travis/lint.py b/travis/lint.py index 51117201c..e1db140c2 100755 --- a/travis/lint.py +++ b/travis/lint.py @@ -105,7 +105,7 @@ class TabLinter(Linter): linters = [cls() for cls in Linter.__subclasses__() if not cls.ignore] def main(): - is_github_actions = os.environ.get('GITHUB_ACTIONS') + is_github_actions = bool(os.environ.get('GITHUB_ACTIONS')) root_path = os.path.abspath(sys.argv[1] if len(sys.argv) > 1 else '.') if not os.path.exists(root_path): print('Nonexistent path: %s' % root_path) diff --git a/travis/script-docs.py b/travis/script-docs.py index 04728e101..b2c8063bf 100755 --- a/travis/script-docs.py +++ b/travis/script-docs.py @@ -1,11 +1,10 @@ #!/usr/bin/env python3 -from __future__ import print_function -from io import open import os from os.path import basename, dirname, join, splitext import sys SCRIPT_PATH = sys.argv[1] if len(sys.argv) > 1 else 'scripts' +IS_GITHUB_ACTIONS = bool(os.environ.get('GITHUB_ACTIONS')) def expected_cmd(path): """Get the command from the name of a script.""" @@ -20,42 +19,61 @@ def check_ls(fname, line): line = line.strip() comment = '--' if fname.endswith('.lua') else '#' if '[====[' in line or not line.startswith(comment): - print('Error: no leading comment in ' + fname) + print_error('missing leading comment (requred for `ls`)', fname) return 1 return 0 +def print_error(message, filename, line=None): + if not isinstance(line, int): + line = 1 + print('Error: %s:%i: %s' % (filename, line, message)) + if IS_GITHUB_ACTIONS: + print('::error file=%s,line=%i::%s' % (filename, line, message)) + + def check_file(fname): errors, doclines = 0, [] tok1, tok2 = ('=begin', '=end') if fname.endswith('.rb') else \ ('[====[', ']====]') + doc_start_line = None with open(fname, errors='ignore') as f: lines = f.readlines() + if not lines: + print_error('empty file', fname) + return 1 errors += check_ls(fname, lines[0]) - for l in lines: + for i, l in enumerate(lines): if doclines or l.strip().endswith(tok1): + if not doclines: + doc_start_line = i + 1 doclines.append(l.rstrip()) if l.startswith(tok2): break else: if doclines: - print('Error: docs start but not end: ' + fname) + print_error('docs start but do not end', fname, doc_start_line) else: - print('Error: no documentation in: ' + fname) + print_error('no documentation found', fname) return 1 if not doclines: - print('Error: missing or malformed documentation in: ' + fname) + print_error('missing or malformed documentation', fname) return 1 title, underline = [d for d in doclines if d and '=begin' not in d and '[====[' not in d][:2] - if underline != '=' * len(title): - print('Error: title/underline mismatch:', fname, title, underline) + title_line = doc_start_line + doclines.index(title) + expected_underline = '=' * len(title) + if underline != expected_underline: + print_error('title/underline mismatch: expected {!r}, got {!r}'.format( + expected_underline, underline), + fname, title_line + 1) errors += 1 if title != expected_cmd(fname): - print('Warning: expected script title {}, got {}'.format( - expected_cmd(fname), title)) + print_error('expected script title {!r}, got {!r}'.format( + expected_cmd(fname), title), + fname, title_line) errors += 1 return errors From 142a7aa287daba0b1c652d67b94a6d4577d602de Mon Sep 17 00:00:00 2001 From: lethosor Date: Wed, 1 Jul 2020 21:58:54 -0400 Subject: [PATCH 2/2] authors-rst.py: add support for printing errors in GitHub actions format --- travis/authors-rst.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/travis/authors-rst.py b/travis/authors-rst.py index e8db817f6..076f7f226 100755 --- a/travis/authors-rst.py +++ b/travis/authors-rst.py @@ -1,7 +1,7 @@ #!/usr/bin/env python3 """ Overly-complicated script to check formatting/sorting in Authors.rst """ -import re, sys +import os, re, sys def main(): success = [True] @@ -10,6 +10,8 @@ def main(): for k in kwargs: info += ' %s %s:' % (k, kwargs[k]) print('line %i:%s %s' % (line, info, msg)) + if os.environ.get('GITHUB_ACTIONS'): + print('::error file=docs/Authors.rst,line=%i::%s %s' % (line, info.lstrip(), msg)) success[0] = False with open('docs/Authors.rst', 'rb') as f: lines = list(map(lambda line: line.decode('utf8').replace('\n', ''), f.readlines()))