Skip to content

Commit

Permalink
Introduce reusable workflow to update pre-commit dependenices
Browse files Browse the repository at this point in the history
  • Loading branch information
rmartin16 committed Jan 29, 2023
1 parent e86bf5e commit 4878154
Show file tree
Hide file tree
Showing 3 changed files with 137 additions and 44 deletions.
77 changes: 77 additions & 0 deletions .github/workflows/automated-changenote.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
name: Automated Change Note

on:
workflow_call:

permissions:
pull-requests: write

env:
COMMIT_MESSAGE: "Add changenote."

jobs:
changenote:
name: Automated Change Note
if: github.repository != 'beeware/.github'
runs-on: ubuntu-latest
timeout-minutes: 5
steps:

- name: Checkout
uses: actions/checkout@v3
with:
fetch-depth: 1
token: ${{ secrets.BRUTUS_PAT_TOKEN }}

- name: Configure git
run: |
git config user.email "brutus@beeware.org"
git config user.name "Brutus (robot)"
- name: Change Note Details
id: changenote
run: |
# If the last commit was a changenote commit, then a changenote commit is not needed on this run
if [[ "${{ github.event.head_commit.message }}" == "${COMMIT_MESSAGE}"* ]]; then
echo "needed=false" >> ${GITHUB_OUTPUT}
else
echo "needed=true" >> ${GITHUB_OUTPUT}
API_URL="${{ github.api_url }}/repos/${{ github.repository }}/commits/${{ github.event.head_commit.id }}/pulls"
PR_NUM=$(curl -s -H "Accept: application/vnd.github+json" "${API_URL}" | jq -r '.[].number')
echo "filename="./changes/${PR_NUM}.misc.rst"" >> ${GITHUB_OUTPUT}
fi
- name: Create Dependabot Change Note
id: dependabot
if: startsWith(github.ref, 'refs/heads/dependabot/') && (steps.changenote.outputs.needed == 'true')
run: |
# Create change note from first line of dependabot commit
NEWS=$(printf "%s" "${{ github.event.head_commit.message }}" | head -n1 | sed -e 's/Bump/Updated/')
printf "%s.\n" "${NEWS}" > "${{ steps.changenote.outputs.filename }}"
git add "${{ steps.changenote.outputs.filename }}"
echo "skip=true" >> ${GITHUB_OUTPUT}
- name: Create pre-commit Autoupdate Change Note
if: github.ref == 'refs/heads/pre-commit-autoupdate' && (steps.changenote.outputs.needed == 'true')
run: |
printf 'Updated hooks for ``pre-commit`` to the latest version.\n' > "${{ steps.changenote.outputs.filename }}"
git add "${{ steps.changenote.outputs.filename }}"
- name: Commit and Push changenote
if: steps.changenote.outputs.needed == 'true'
run: |
if [[ "${{ steps.dependabot.outputs.skip }}" == "true" ]]; then
# "dependabot skip" tells Dependabot to continue rebasing this branch despite foreign commits
COMMIT_MESSAGE="${COMMIT_MESSAGE} [dependabot skip]"
fi
# If there's anything staged, push it; if the changenote already
# exists from a previous run of this workflow, nothing to push.
if [[ $(git status --porcelain) ]]; then
echo "Pushing commit..."
git commit -m "${COMMIT_MESSAGE}"
git push origin
fi
44 changes: 0 additions & 44 deletions .github/workflows/dependabot-changenote.yml

This file was deleted.

60 changes: 60 additions & 0 deletions .github/workflows/pre-commit-update.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
name: Update pre-commit

on:
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

permissions:
pull-requests: write

jobs:
pre-commit-update:
name: Update pre-commit
runs-on: ubuntu-latest
timeout-minutes: 10
steps:

- name: Checkout
uses: actions/checkout@v3
with:
fetch-depth: 1
token: ${{ secrets.BRUTUS_PAT_TOKEN }}

- name: Configure git
run: |
git config user.email "brutus@beeware.org"
git config user.name "Brutus (robot)"
- name: Set up Python
uses: actions/setup-python@v4.5.0
with:
python-version: "3.X"

- name: Install pre-commit
run: python -m pip install ${{ inputs.pre-commit-source }}

- name: Update pre-commit hooks
run: |
pre-commit autoupdate
pre-commit install-hooks
- name: Run pre-commit
# pre-commit returns non-zero when files are changed
continue-on-error: true
run: pre-commit run --all-files

- name: Create Pull Request
uses: peter-evans/create-pull-request@v4.2.3
with:
token: ${{ secrets.BRUTUS_PAT_TOKEN }}
branch: pre-commit-autoupdate
title: Update pre-commit hooks
commit-message: Auto-update pre-commit hooks
body: |
Update versions of tools in pre-commit configs to latest version
and run updated hook over repo.
labels: dependencies

0 comments on commit 4878154

Please sign in to comment.