Skip to content

Commit

Permalink
ci(darker): improve comparison refs for focused errors (#5311)
Browse files Browse the repository at this point in the history
Change which commits we compare in darker.yml,
not to include changes introduced by other branches

More details:
- #5311
- #5282 (comment)
  • Loading branch information
p2edwards authored Dec 18, 2024
1 parent 424311f commit b6a2829
Showing 1 changed file with 29 additions and 8 deletions.
37 changes: 29 additions & 8 deletions .github/workflows/darker.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
uses: actions/checkout@v4
with:
fetch-depth: 0
# In addition to checking out the 'merge commit', we also want to
# fetch enough commits to find the most recent commit in the base
# branch (typically 'main')
fetch-depth: 100

- name: Set up Python
uses: actions/setup-python@v5
Expand All @@ -18,12 +21,30 @@ jobs:
- name: Install pip dependencies
run: python -m pip install darker[isort] flake8 flake8-quotes isort --quiet

# use `--ignore=F821` to avoid raising false positive error in typing
# annotations with string, e.g. def my_method(my_model: 'ModelName')

# darker still exit with code 1 even with no errors on changes
- name: Run Darker with base commit
- name: Run Darker, comparing '${{github.ref_name}}' with the latest in '${{github.event.pull_request.base.ref}}'
run: |
output=$(darker --check --isort -L "flake8 --max-line-length=88 --extend-ignore=F821" kpi kobo hub -r ${{ github.event.pull_request.base.sha }})
# Run darker only on file changes introduced in this PR.
# Allows incremental adoption of linter rules on a per-file basis.
# Prevents this scenario:
# - PR A is opened, modifying file A; CI passes.
# - PR B is opened & merged, with linter errors in file B.
# - PR A is updated again, modifying file A. CI fails from file B.
# Get the latest commit in the base branch (usually 'main') at time of
# CI run, to compare with this PR's merge branch.
# GitHub doesn't provide a nice name for this SHA, but we can find it:
# https://www.kenmuse.com/blog/the-many-shas-of-a-github-pull-request/#extracting-the-base-sha
MERGE_PARENTS=($(git rev-list --parents -1 ${{ github.sha }}))
LATEST_IN_BASE_BRANCH=${MERGE_PARENTS[1]}
# Run darker. (https://github.com/akaihola/darker)
# -L runs the linter
# `--ignore=F821` avoids raising false positive error in typing
# annotations with string, e.g. def my_method(my_model: 'ModelName')
# -r REV specifies a commit to compare with the worktree
output=$(darker --check --isort -L "flake8 --max-line-length=88 --extend-ignore=F821" kpi kobo hub -r $LATEST_IN_BASE_BRANCH)
# darker still exits with code 1 even with no errors on changes
# So, make this fail CI only if there is output from darker.
[[ -n "$output" ]] && echo "$output" && exit 1 || exit 0
shell: /usr/bin/bash {0}

0 comments on commit b6a2829

Please sign in to comment.