Running cog automatically against GitHub pull requests

I really like Cog (previously) as a tool for automating aspects of my Python project documentation - things like the SQL schemas shown on the LLM logging page.

When using cog in this way it's important to remember to run cog -r to update those generated files before pushing a commit.

I've previously been enforcing this using GitHub Actions - as part of my tests I run cog --check *.md so that my test suite fails if the generated files are out of date.

This morning I switched to a friendlier version. I now run a GitHub Actions workflow against any created or modified pull requests and, if cog needs to be run, the workflow runs it and then helpfully commits the files back to that repo.

I got Claude to write the initial workflow, and then iterated on it to get it working.

This goes in .github/workflows/cog.yml:

name: Run Cog

on:
  pull_request:
    types: [opened, synchronize]

permissions:
  contents: write
  pull-requests: write

jobs:
  run-cog:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
        with:
          ref: ${{ github.head_ref }}
      - name: Set up Python 3.11
        uses: actions/setup-python@v5
        with:
          python-version: '3.11'
      - name: Install dependencies
        run: pip install -e '.[test]'
      - name: Run cog
        run: |
          cog -r docs/**/*.md docs/*.md
      - name: Check for changes
        id: check-changes
        run: |
          if [ -n "$(git diff)" ]; then
            echo "changes=true" >> $GITHUB_OUTPUT
          else
            echo "changes=false" >> $GITHUB_OUTPUT
          fi
      - name: Commit and push if changed
        if: steps.check-changes.outputs.changes == 'true'
        run: |
          git config --local user.email "github-actions[bot]@users.noreply.github.com"
          git config --local user.name "github-actions[bot]"
          git add -A
          git commit -m "Ran cog"
          git push

The [opened, synchronize] trigger means that the workflow will run whenever a pull request is either created or updated in some way.

Using that user.email and user.name causes the bot to get a nice icon of its own:

List of commits on GitHub - the last one reads Ran cog and shows a GitHub avatar icon

Squash-merging the PR causes the bot to be credited as a co-author.

Created 2024-11-06T07:25:59-08:00 · Edit