diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml new file mode 100644 index 0000000..bf81f15 --- /dev/null +++ b/.github/workflows/test.yml @@ -0,0 +1,28 @@ +name: test + +on: + push: + +jobs: + test: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + + - name: check some build related things + run: | + git --version + git config user.name "GitHub Actions Bot" + git config user.email "<>" + git status + git diff + ls -la . + git tag -l + +########################################################################################### + - name: test pre-commit + id: pre-commit + run: ./pre-commit-bash.sh + env: + token: wibble + diff --git a/README.md b/README.md new file mode 100644 index 0000000..393d892 --- /dev/null +++ b/README.md @@ -0,0 +1,4 @@ +# Overview + +Small action to setup and run pre-commit and give us control to feed in only the files that have changed + diff --git a/action.yml b/action.yml new file mode 100644 index 0000000..baf5193 --- /dev/null +++ b/action.yml @@ -0,0 +1,43 @@ +name: pre-commit-bash + +inputs: + token: + description: 'token to perform git commits' + required: true + +runs: + using: "composite" + steps: + - name: adjust the path so scripts in this repo can be run + run: echo "${{ github.action_path }}" >> $GITHUB_PATH + shell: bash + + - name: setup python which pre-commit uses + uses: actions/setup-python@v5 + with: + python-version: "3.10" + + - name: set PY + run: echo "PY=$(python -VV | sha256sum | cut -d' ' -f1)" >> "$GITHUB_ENV" + shell: bash + + - name: cache pip installations and pre-commit installations + uses: actions/cache@v4 + with: + path: | + ~/.cache/pip + ~/.cache/pre-commit + key: pre-commit|${{ env.PY }}|${{ hashFiles('.pre-commit-config.yaml') }} + + - name: get changed files + id: changed-files + uses: tj-actions/changed-files@5e85e31a0187e8df23b438284aa04f21b55f1510 + + - name: run precommit wrapper + run: pre-commit-bash.sh + shell: bash + env: + # FIXME: our gh script seems to need GITHUB_TOKEN but runner wants GH_TOKEN + GH_TOKEN: ${{ inputs.token }} + GITHUB_TOKEN: ${{ inputs.token }} + CHANGED_FILES: ${{ steps.changed-files.outputs.all_changed_files }} diff --git a/pre-commit-bash.sh b/pre-commit-bash.sh new file mode 100755 index 0000000..bac7012 --- /dev/null +++ b/pre-commit-bash.sh @@ -0,0 +1,96 @@ +#!/usr/bin/env bash + +# +# defaults +# + +set -o errexit -o errtrace -o nounset -o functrace -o pipefail +shopt -s inherit_errexit 2>/dev/null || true + +trap 'echo "exit_code $? line $LINENO linecallfunc $BASH_COMMAND"' ERR + +changed_files=${CHANGED_FILES:-unset} +all_files=${ALL_FILES:-unset} + +NAME=pre-commit-bash.sh + +# +# functions +# + +usage(){ +I_USAGE=" + + Usage: ${NAME} [OPTIONS] + + Description: + + Github actions inputs: + + To handle running this script via github actions where it can be called from a dispatch, or a commit trigger + The config is fed through environment variables + + Requirements: + + This script needs to be run in a git checkout directory. For github actions this is done using the actions/checkout step before + + Environment variable options: + \$CHANGED_FILES - pass in a list of files that have changed + \$ALL_FILES - pass in true to cause all files to be scanned by pre-commit + +" + echo "$I_USAGE" + exit +} + +# +# args +# + +while : +do + case ${1-default} in + --*help|-h ) usage ; exit 0 ;; + --man ) usage ; exit 0 ;; + --) shift ; break ;; + -*) echo "WARN: Unknown option (ignored): $1" >&2 ; shift ;; + *) break ;; + esac +done + +# +# main +# + +if [[ "$changed_files" == 'unset' ]];then + files_arg="" +else + files_arg="--files $changed_files" +fi + +if [[ "$all_files" == 'unset' ]];then + all_files_arg="" +else + all_files_arg="--all-files" +fi + +pip install pre-commit==3.7.0 +pip freeze --local + +if pre-commit run ${all_files_arg:+ $all_files_arg} ${files_arg:+ $files_arg} --show-diff-on-failure --color=always;then + pre_commit_exit_code="$?" +else + pre_commit_exit_code="$?" + if [[ "$pre_commit_exit_code" -eq 1 ]];then + echo "files changed" + git config --global user.email "actions@github.com" + git config --global user.name "github actions" + git status + git add -A + timestamp=$(date -u) + git commit -m "pre-commit fixes: ${timestamp}" + git push + exit 0 + fi +fi +echo "$pre_commit_exit_code"