From 67cb4eb24db7998e67c7ec478c2ac166c35e4a24 Mon Sep 17 00:00:00 2001 From: Andrew Imeson Date: Tue, 22 Sep 2020 23:23:24 -0400 Subject: [PATCH] Auto-change output format if GitHub Actions detected --- docs/integration.rst | 34 ++++++++++++++++++++++++++++++++++ tests/test_cli.py | 18 ++++++++++++++++++ yamllint/cli.py | 4 +++- 3 files changed, 55 insertions(+), 1 deletion(-) diff --git a/docs/integration.rst b/docs/integration.rst index 3457556..b094edc 100644 --- a/docs/integration.rst +++ b/docs/integration.rst @@ -17,3 +17,37 @@ Here is an example, to add in your .pre-commit-config.yaml hooks: - id: yamllint args: [-c=/path/to/.yamllint] + +Integration with GitHub Actions +------------------------------- + +yamllint auto-detects when it's running inside of `GitHub +Actions` and automatically uses the suited +output format to decorate code with linting errors automatically. You can also +force the GitHub Actions output with ``yamllint --format github``. + +An example workflow using GitHub Actions: + +.. code:: yaml + + --- + name: yamllint test + + on: push + + jobs: + test: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + + - name: Set up Python + uses: actions/setup-python@v2 + with: + python-version: 3.8 + + - name: Install yamllint + run: pip install yamllint + + - name: Lint YAML files + run: yamllint . diff --git a/tests/test_cli.py b/tests/test_cli.py index ca3056c..020b371 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -563,6 +563,24 @@ class CommandLineTestCase(unittest.TestCase): self.assertEqual( (ctx.returncode, ctx.stdout, ctx.stderr), (1, expected_out, '')) + def test_github_actions_detection(self): + path = os.path.join(self.wd, 'a.yaml') + self.addCleanup(os.environ.__delitem__, 'GITHUB_ACTIONS') + self.addCleanup(os.environ.__delitem__, 'GITHUB_WORKFLOW') + + with RunContext(self) as ctx: + os.environ['GITHUB_ACTIONS'] = 'something' + os.environ['GITHUB_WORKFLOW'] = 'something' + cli.run((path, )) + 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 diff --git a/yamllint/cli.py b/yamllint/cli.py index 5701fa3..cb87350 100644 --- a/yamllint/cli.py +++ b/yamllint/cli.py @@ -109,7 +109,9 @@ def show_problems(problems, file, args_format, no_warn): continue if args_format == 'parsable': print(Format.parsable(problem, file)) - elif args_format == 'github': + elif args_format == 'github' or (args_format == 'auto' and + 'GITHUB_ACTIONS' in os.environ and + 'GITHUB_WORKFLOW' in os.environ): print(Format.github(problem, file)) elif args_format == 'colored' or \ (args_format == 'auto' and supports_color()):