Skip to content

Commit

Permalink
initial commit with basic bash wrapper script
Browse files Browse the repository at this point in the history
  • Loading branch information
gilesw committed May 1, 2024
0 parents commit fcd29d8
Show file tree
Hide file tree
Showing 4 changed files with 171 additions and 0 deletions.
28 changes: 28 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -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

4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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

43 changes: 43 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
@@ -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 }}
96 changes: 96 additions & 0 deletions pre-commit-bash.sh
Original file line number Diff line number Diff line change
@@ -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 "[email protected]"
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"

0 comments on commit fcd29d8

Please sign in to comment.