From fa19a803666c76a4b06545f01093920b5f60db1d Mon Sep 17 00:00:00 2001
From: Jean-Michel Picod <jmichel@google.com>
Date: Thu, 27 Feb 2020 20:07:29 +0100
Subject: [PATCH] Try with a custom action

---
 .github/workflows/python.yml        | 11 ++++++----
 github_actions/pylint/Dockerfile    | 10 +++++++++
 github_actions/pylint/action.yml    | 20 ++++++++++++++++++
 github_actions/pylint/entrypoint.sh | 32 +++++++++++++++++++++++++++++
 4 files changed, 69 insertions(+), 4 deletions(-)
 create mode 100644 github_actions/pylint/Dockerfile
 create mode 100644 github_actions/pylint/action.yml
 create mode 100644 github_actions/pylint/entrypoint.sh

diff --git a/.github/workflows/python.yml b/.github/workflows/python.yml
index 1edd79d..d66067f 100644
--- a/.github/workflows/python.yml
+++ b/.github/workflows/python.yml
@@ -23,11 +23,14 @@ jobs:
         run: |
           python -m pip install --upgrade pip setuptools wheel
           pip install tockloader pylint
+      - name: Register matcher
+        run: echo ::add-matcher::./.github/python_matcher.json
       - name: Test code with pylint
-        run: |
-          echo ::add-matcher::./.github/python_matcher.json
-          find . -type f -name '*.py' -exec echo PYLINT:\{\} \; -exec pylint --rcfile=.pylintrc --score=n \{\} \;
-#          find . ! -path "./third_party/*" -type f -name '*.py' -exec echo PYLINT:\{\} \; -exec pylint --rcfile=.pylintrc --score=n \{\} \;
+        uses: ./github_actions/pylint
+        with:
+          config-file: .pylintrc
+          files: '**/*.py'
+          exclude-path: third_party
 
   yapf:
     runs-on: ubuntu-18.04
diff --git a/github_actions/pylint/Dockerfile b/github_actions/pylint/Dockerfile
new file mode 100644
index 0000000..63fbb9a
--- /dev/null
+++ b/github_actions/pylint/Dockerfile
@@ -0,0 +1,10 @@
+FROM python:3.7-alpine
+
+RUN apk add --no-cache bash build-base gcc
+RUN pip install --upgrade pip
+RUN pip install pylint
+RUN python --version ; pip --version ; pylint --version
+
+COPY entrypoint.sh /entrypoint.sh
+RUN chmod +x /entrypoint.sh
+ENTRYPOINT ["/entrypoint.sh"]
diff --git a/github_actions/pylint/action.yml b/github_actions/pylint/action.yml
new file mode 100644
index 0000000..7ca6dda
--- /dev/null
+++ b/github_actions/pylint/action.yml
@@ -0,0 +1,20 @@
+---
+name: 'Pylint'
+description: 'Runs pylint across multiple files/modules'
+author: 'Jean-Michel Picod <jmichel@google.com>'
+inputs:
+  config-file:
+    description: pylintrc configuration file
+    required: false
+  files:
+    description: files, directories, or globs
+    required: true
+  ignore-files:
+    description: files to ignore/exclude
+    required: false
+  exclude-path:
+    description: paths to ignore/exclude
+    required: false
+runs:
+  using: docker
+  image: Dockerfile
diff --git a/github_actions/pylint/entrypoint.sh b/github_actions/pylint/entrypoint.sh
new file mode 100644
index 0000000..f99938b
--- /dev/null
+++ b/github_actions/pylint/entrypoint.sh
@@ -0,0 +1,32 @@
+#!/bin/bash
+
+env
+
+PYLINT_CMD=pylint --score=n${INPUT_CONFIG_FILE:+ --rcfile=${INPUT_CONFIG_FILE}}
+EXCLUDE_PATH=${INPUT_EXCLUDE_PATH:-}
+EXCLUDE_FILES=${INPUT_EXCLUDE_PATH:-}
+
+SUCCESS=0
+for file in ${FILES}
+do
+  fname=$(basename $file)
+  directory=$(dirname $file)
+  if [[ "$directory" =~ "^${EXCLUDE_PATH}" ]]
+  then
+    echo "Ignoring file '$file' (reason: matching exclude-path parameter)"
+    continue
+  fi
+  if [[ "$fname" =~ "${EXCLUDE_FILES}" ]]
+  then
+    echo "Ignoring file '$file' (reason: matching exclude-files parameter)"
+    continue
+  fi
+  # Just to trigger the custom matcher
+  echo PYLINT:$file
+  if ! $PYLINT_CMD $file
+  then
+    SUCCESS=1
+  fi
+done
+
+exit $SUCCESS
-- 
GitLab