Skip to content

feat: Add tests and GitHub actions workflows #3

feat: Add tests and GitHub actions workflows

feat: Add tests and GitHub actions workflows #3

Workflow file for this run

name: "[Tools] pre-commit"
on:
pull_request:
# Default is [opened, reopened, synchronize]. Since we want to run pre-commit on "ready to review",
# we add that one. Since lists are overwritten, not merged, we add all four.
# See https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#pull_request
# for documentation of the default behaviour
types:
- opened
- reopened
- synchronize
- ready_for_review
merge_group:
# Cancel a previous job if the same workflow is triggers on the same branch
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true
jobs:
pre-commit:
runs-on:
labels: self-hosted-main
# Disable pre-commit during draft PR status, only run when "Ready for Review"
if: ${{ !github.event.pull_request.draft }}
steps:
# Checkout the repo into the worker
- uses: actions/checkout@c85c95e3d7251135ab7dc9ce3241c5835cc595a9 # v3
with:
token: ${{ secrets.GH_PERSONAL_ACCESS_TOKEN }}
# fetch-depth 0 is needed for PRs with merge commits
fetch-depth: 0
# Cache the pre-commit environments based on changes to the .pre-commit-config.yaml file
- uses: actions/cache@88522ab9f39a2ea568f7027eddc7d8d8bc9d59c8 # v3
with:
path: ~/.cache/pre-commit
key: pre-commit-3|${{ env.pythonLocation }}|${{ hashFiles('.pre-commit-config.yaml') }}
# FIXME: This should be taken from the image automatically, but for some reason that does not happen.
# https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#adding-a-system-path
- run: echo "/home/runner/.local/conda/bin:/home/runner/go/bin" >> $GITHUB_PATH
- name: Get changed files
id: changed-files
uses: tj-actions/changed-files@de0eba32790fb9bf87471b32855a30fc8f9d5fc6 # v37.4.0
# Adapted from https://github.com/pre-commit/action/blob/efd3bcfec120bd343786e46318186153b7bc8c68/action.yml#L19
- run: pre-commit run --show-diff-on-failure --color=always --files ${{ steps.changed-files.outputs.all_changed_files }}
# When pre-commit makes changes, this step will error. With continue-on-error, we
# continue execution to commit the changed files
continue-on-error: true
if: "${{ steps.changed-files.outputs.all_changed_files != '' }}"
- name: Checkout the branch we're running on to enable a commit to it
run: git checkout ${{ github.head_ref }}
- name: Commit linted files
if: "${{ steps.changed-files.outputs.all_changed_files != '' }}"
uses: EndBug/add-and-commit@1bad3abcf0d6ec49a5857d124b0bfb52dc7bb081 # v9.1.3
with:
message: "chore(pre-commit): linting"
author_name: Anaconda Pre-Commit Bot
author_email: anaconda-pre-commit-bot@anaconda.com
# Run a second time - if there's still changes, manual intervention is needed
# Adapted from https://github.com/pre-commit/action/blob/efd3bcfec120bd343786e46318186153b7bc8c68/action.yml#L19
- run: pre-commit run --show-diff-on-failure --color=always --files ${{ steps.changed-files.outputs.all_changed_files }}
if: "${{ steps.changed-files.outputs.all_changed_files != '' }}"