Skip to content

Commit

Permalink
Add linting to GitHub workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
Splines committed Feb 17, 2024
1 parent be818b5 commit 9052c69
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 0 deletions.
42 changes: 42 additions & 0 deletions .github/actions/changed_files/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Why is this file in a subdirectory? Because GitHub Actions requires it to be :(
# see: https://github.com/orgs/community/discussions/26245#discussioncomment-5962450
name: "Get changed files"
description: "Checks out the code and returns the filenames of files that have changed in the pull request"

inputs:
file-extensions:
# for example: "\.py$" or something like "\.py$|\.py.myfile$"
description: "Regex expressions for grep to filter for specific files"
required: true

outputs:
changed-files:
description: "A space-separated list of the files that have changed in the pull request"
value: ${{ steps.get-changed-files.outputs.files }}

runs:
using: "composite"
steps:
# This has to be done in the main workflow, not in the action, because
# otherwise this reusable action is not available in the workflow.
# - name: "Checkout code (on a PR branch)"
# uses: actions/checkout@v4
# with:
# fetch-depth: 2 # to also fetch parent of PR

# Adapted from this great comment [1]. Git diff adapted from [2].
# "|| test $? = 1;" is used to ignore the exit code of grep when no files
# are found matching the pattern. For the "three dots" ... syntax, see [3].
#
# Resources:
# number [1] being most important
# [1] https://github.com/actions/checkout/issues/520#issuecomment-1167205721
# [2] https://robertfaldo.medium.com/commands-to-run-rubocop-and-specs-you-changed-in-your-branch-e6d2f2e4110b
# [3] https://community.atlassian.com/t5/Bitbucket-questions/Git-diff-show-different-files-than-PR-Pull-Request/qaq-p/2331786
- name: Get changed files
shell: bash
id: get-changed-files
run: |
files_pretty=$(git diff --name-only --diff-filter=ACMR -r HEAD^1...HEAD | egrep '${{inputs.file-extensions}}' || test $? = 1;)
printf "🎴 Changed files: \n$files_pretty"
echo "files=$(echo ${files_pretty} | xargs)" >> $GITHUB_OUTPUT
50 changes: 50 additions & 0 deletions .github/workflows/linter.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
name: Linting

# for "synchronize", see [1]
on:
pull_request:
types: [opened, reopened, synchronize, ready_for_review]

jobs:
pylint:
name: Pylint
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
fetch-depth: 2 # to also fetch parent of PR (used to get changed files)

- name: Get changed files
id: py-changed
uses: ./.github/actions/changed_files/
with:
file-extensions: \.py$

- name: Setup Python
if: ${{ steps.py-changed.outputs.changed-files != ''}}
uses: actions/setup-python@v5
with:
python-version: '3.11'
cache: 'pipenv'

# see Caching [2]
- name: Install pipenv
if: ${{ steps.py-changed.outputs.changed-files != ''}}
run: curl https://raw.githubusercontent.com/pypa/pipenv/master/get-pipenv.py | python

- name: Install dependencies
if: ${{ steps.py-changed.outputs.changed-files != ''}}
run: pipenv install

# For GitHub output format, see [3]
- name: Run Pylint
if: ${{ steps.py-changed.outputs.changed-files != ''}}
run: |
echo "🚨 Running Pylint version: $(pipenv run python3 -m pylint --version)"
pipenv run python3 -m pylint ${{ steps.py-changed.outputs.changed-files }}
# [1] https://github.com/orgs/community/discussions/26366
# [2] https://github.com/actions/setup-python/blob/main/docs/advanced-usage.md#caching-packages
# [3] https://github.com/pylint-dev/pylint/issues/9443

0 comments on commit 9052c69

Please sign in to comment.