From e05d0c65cba6d496ef21363b680db8c83882012f Mon Sep 17 00:00:00 2001 From: Patric Stout Date: Wed, 30 Oct 2019 16:22:43 +0100 Subject: [PATCH] Add: first version of a flake8 checker with annotation (#1) This uses a ProblemMatcher together with GitHub Actions 'add-matcher'. To allow warnings/errors to show up correctly, a 'sed' is used to handled that correctly. --- .github/workflows/testing.yml | 14 ++++++++++++++ Dockerfile | 13 +++++++++++++ LICENSE | 21 +++++++++++++++++++++ README.md | 10 ++++++++++ action.yml | 14 ++++++++++++++ entrypoint.sh | 23 +++++++++++++++++++++++ example/example.py | 5 +++++ flake8-matcher.json | 17 +++++++++++++++++ requirements.txt | 1 + 9 files changed, 118 insertions(+) create mode 100644 .github/workflows/testing.yml create mode 100644 Dockerfile create mode 100644 LICENSE create mode 100644 README.md create mode 100644 action.yml create mode 100755 entrypoint.sh create mode 100644 example/example.py create mode 100644 flake8-matcher.json create mode 100644 requirements.txt diff --git a/.github/workflows/testing.yml b/.github/workflows/testing.yml new file mode 100644 index 0000000..3a888d6 --- /dev/null +++ b/.github/workflows/testing.yml @@ -0,0 +1,14 @@ +name: Testing + +on: [pull_request] + +jobs: + run_action: + name: Test run action + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v1 + - uses: ./ + with: + path: example + only_warn: 1 diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..240ed43 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,13 @@ +FROM python:3.8-alpine + +COPY LICENSE \ + README.md \ + entrypoint.sh \ + flake8-matcher.json \ + requirements.txt \ + /code/ + +RUN pip install -r /code/requirements.txt + +ENTRYPOINT ["/code/entrypoint.sh"] +CMD [] diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..c83254c --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +The MIT License + +Copyright (c) 2019, Patric "TrueBrain" Stout + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..3c39b21 --- /dev/null +++ b/README.md @@ -0,0 +1,10 @@ +# Flake8 with GitHub Actions -- including annotations for Pull Requests + +This GitHub Actions runs flake8 over your code. +Any warnings or errors will be annoted in the Pull Request. + +## Example usages + +``` +uses: TrueBrain/actions-flake8@master +``` diff --git a/action.yml b/action.yml new file mode 100644 index 0000000..fe26733 --- /dev/null +++ b/action.yml @@ -0,0 +1,14 @@ +name: 'Flake8' +description: 'Flake8 with annotations for Pull Request' +inputs: + path: + description: 'Path to run flake8 in' + requred: false + default: '' + only_warn: + description: 'If set, only warn, never error' + requred: false + default: '' +runs: + using: 'docker' + image: 'Dockerfile' diff --git a/entrypoint.sh b/entrypoint.sh new file mode 100755 index 0000000..1ad9f6b --- /dev/null +++ b/entrypoint.sh @@ -0,0 +1,23 @@ +#!/bin/sh + +# Copy the matcher to the host system; otherwise "add-matcher" can't find it. +cp /code/flake8-matcher.json /github/workflow/flake8-matcher.json +echo "::add-matcher::${RUNNER_TEMP}/_github_workflow/flake8-matcher.json" + +# Run flake8. We add 'error/warning' as GitHub Actions ProblemMatcher needs +# the text 'error/warning' to know how to classify it. +# The weird piping is needed because we want to get the exitcode from flake8, +# but there is no other good universal way of doing so. +# e.g. PIPESTATUS and pipestatus only work in bash/zsh respectively. +exec 5>&1 +res=`{ { flake8 ${INPUT_PATH}; echo $? 1>&4; } | sed -r 's/: ([^W][0-9][0-9][0-9])/: error: \1/;s/: (W[0-9][0-9][0-9])/: warning: \1/' 1>&5; } 4>&1` + +# Remove the matcher, so no other jobs hit it. +echo "::remove-matcher owner=flake8::" + +# If we are in warn-only mode, return always as if we pass +if [ -n "${INPUT_ONLY_WARN}" ]; then + exit 0 +else + exit $res +fi diff --git a/example/example.py b/example/example.py new file mode 100644 index 0000000..6e36ab9 --- /dev/null +++ b/example/example.py @@ -0,0 +1,5 @@ + +def test(): + pass +def test() : + pass diff --git a/flake8-matcher.json b/flake8-matcher.json new file mode 100644 index 0000000..9c998e9 --- /dev/null +++ b/flake8-matcher.json @@ -0,0 +1,17 @@ +{ + "problemMatcher": [ + { + "owner": "flake8", + "pattern": [ + { + "regexp": "^([^:]*):(\\d+):(\\d+): (error|warning): (\\w\\d\\d\\d) (.*)$", + "file": 1, + "line": 2, + "column": 3, + "severity": 4, + "message": 6 + } + ] + } + ] +} diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..5c71e7d --- /dev/null +++ b/requirements.txt @@ -0,0 +1 @@ +flake8==3.7.9