-
Notifications
You must be signed in to change notification settings - Fork 4
139 lines (120 loc) · 4.5 KB
/
pre-commit-update.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
name: Update pre-commit
#######
# Updates pre-commit's hooks, runs pre-commit, and commits all changes into a PR.
#######
on:
schedule:
- cron: "0 20 * * SUN" # Sunday @ 2000 UTC
workflow_dispatch:
workflow_call:
inputs:
pre-commit-source:
description: "The arguments for `pip install` to install pre-commit; use ./path/to/package[dev] for the repo's pinned version."
default: ".[dev]"
type: string
create-changenote:
description: "Defaults 'true' to create a misc changenote in the './changes' directory."
default: true
type: boolean
secrets:
BRUTUS_PAT_TOKEN:
required: true
permissions:
pull-requests: write
env:
BRANCH_PREFIX: "autoupdates/pre-commit"
CHANGENOTE_DIR: "./changes"
FORCE_COLOR: "1"
defaults:
run:
shell: bash
jobs:
pre-commit-hooks:
name: Pre-commit hooks
runs-on: ubuntu-latest
timeout-minutes: 5
outputs:
hook-repos: ${{ steps.hooks.outputs.repos }}
steps:
- name: Checkout Repo
uses: actions/[email protected]
- name: Get Pre-commit Hooks
id: hooks
run: |
REPOS=$(yq .repos[].repo < .pre-commit-config.yaml | jq -R -s -c 'split("\n")[:-1]')
echo "repos=${REPOS}" >> ${GITHUB_OUTPUT}
pre-commit-update:
name: Update Pre-commit hook
needs: pre-commit-hooks
runs-on: ubuntu-latest
timeout-minutes: 15
strategy:
fail-fast: false
matrix:
hook-repo: ${{ fromJson(needs.pre-commit-hooks.outputs.hook-repos) }}
steps:
- name: Checkout
uses: actions/[email protected]
with:
fetch-depth: 1
token: ${{ secrets.BRUTUS_PAT_TOKEN }}
- name: Configure git
run: |
git config user.email "[email protected]"
git config user.name "Brutus (robot)"
- name: Set up Python
uses: actions/[email protected]
with:
python-version: 3.X
cache: pip
cache-dependency-path: |
**/setup.cfg
**/pyproject.toml
.pre-commit-config.yaml
- name: Install pre-commit
run: python -m pip install ${{ inputs.pre-commit-source || 'pre-commit' }}
- name: Update pre-commit hooks
id: update
run: |
pre-commit autoupdate --repo ${{ matrix.hook-repo }} | tee update.log
echo "vers-bump-str=$(grep -ohE '([^ ]+ -> [^ ]+)' update.log | sed 's/->/to/')" >> ${GITHUB_OUTPUT}
rm -f update.log
pre-commit install-hooks
- name: Run pre-commit
# pre-commit returns non-zero when files are changed and fails the job
continue-on-error: true
run: pre-commit run --all-files
- name: PR Needed?
id: pr
run: |
if [[ $(git status --porcelain) ]]; then
echo "needed=true" >> ${GITHUB_OUTPUT}
else
echo "needed=false" >> ${GITHUB_OUTPUT}
fi
REPO="${{ matrix.hook-repo }}"
echo "hook-name=${REPO##*/}" >> ${GITHUB_OUTPUT}
- name: Create Pull Request
id: created-pr
if: steps.pr.outputs.needed == 'true'
uses: peter-evans/[email protected]
with:
token: ${{ secrets.BRUTUS_PAT_TOKEN }}
title: "Bump ${{ steps.pr.outputs.hook-name }} from ${{ steps.update.outputs.vers-bump-str }}"
branch: "${{ env.BRANCH_PREFIX }}/${{ steps.pr.outputs.hook-name }}"
commit-message: "Bump ${{ steps.pr.outputs.hook-name }} from ${{ steps.update.outputs.vers-bump-str }}"
committer: "Brutus (robot) <[email protected]>"
author: "Brutus (robot) <[email protected]>"
body: "Bumps `pre-commit` hook for `${{ steps.pr.outputs.hook-name }}` from ${{ steps.update.outputs.vers-bump-str }} and ran the update against the repo."
labels: "dependencies"
- name: Add changenote
if: (inputs.create-changenote == true) && (steps.created-pr.outputs.pull-request-number != '')
run: |
BRANCH_NAME="${{ env.BRANCH_PREFIX }}/${{ steps.pr.outputs.hook-name }}"
git fetch origin
git checkout "${BRANCH_NAME}"
FILENAME="${{ env.CHANGENOTE_DIR }}/${{ steps.created-pr.outputs.pull-request-number }}.misc.rst"
printf 'The ``pre-commit`` hook for ``${{ steps.pr.outputs.hook-name }}`` was updated to its latest version.\n' > "${FILENAME}"
git add "${FILENAME}"
git commit -m "Add changenote."
git push --set-upstream origin "${BRANCH_NAME}"