Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Introduce reusable workflow to update pre-commit hooks #6

Merged
merged 2 commits into from
Feb 1, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 82 additions & 0 deletions .github/workflows/automation-changenote.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
name: Automation Change Note

#######
# Adds a changenote to PRs from automation such as Dependabot.
#######

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}

# Generate a filename for the changenote based on the PR for this commit
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
id: pre-commit
if: github.ref == 'refs/heads/autoupdates/pre-commit' && (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

# Only attempt to push if files changed; otherwise, the empty push fails the entire job.
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.

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

#######
# Updates pre-commit's hooks, runs pre-commit, and commits all changes into a PR.
#######

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: 15
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 and fails the job
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 }}
title: Auto-update `pre-commit` hooks
branch: autoupdates/pre-commit
commit-message: Auto-updated `pre-commit` hooks.
committer: Brutus (robot) <brutus@beeware.org>
author: Brutus (robot) <brutus@beeware.org>
body: Updated `pre-commit` hooks to their latest versions and ran updated hooks against the repo.
labels: dependencies