In part 4 of this series of posts we used the Replace multiple strings in files GitHub Action to update files with the replacement values. The files are modified on disk, but what to do with the changes? I could just automatically commit the changes back to version control, but I prefer to take a more cautious approach and give myself a chance to review the changes to confirm if they look reasonable. Creating a pull request is a great way of doing that.

A an action I've used successfully before to do this is Peter Evans' Create Pull Request GitHub Action. Using it is very easy:


- name: Create Pull Request
  uses: peter-evans/[email protected]
  with:
    token: ${{ secrets.PAT_REPO_FULL }}

I pass in a personal access token so that any workflows that should be run on creation of a pull request will be executed.

And with that, I have a full workflow for checking for broken links and repairing them. The only thing to watch out for is that issue I mentioned previously with the invalid JSON produced by the Lychee action. As a temporary measure, I inlined that action in my repo and applied a local code fix. Hopefully once the Lychee action itself is updated then I can go back to using their implementation.


name: Links

on:
  workflow_dispatch:
    
jobs:
  linkChecker:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v3
      - name: Link Checker
        id: lychee
        uses: ./.github/actions/lychee-action #lycheeverse/[email protected]
        env:
          GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}}
        with:
          args: '--verbose ./_posts/**/*.md --exclude-mail --scheme http https'
          format: json
          output: ./lychee/links.json
          fail: false

      - uses: actions/[email protected]
        with:
          path: ./lychee/links.json

      - name: Wayback Machine Query
        uses: flcdrg/wayback-machine-query-action@v2
        id: wayback
        with:
          source-path: ./lychee/links.json
          timestamp-regex: '_posts\/(\d+)\/(?<year>\d+)-(?<month>\d+)-(?<day>\d+)-'

      - uses: actions/[email protected]
        with:
          path: ./wayback/replacements.json
          
      - name: Replacements
        uses: flcdrg/replace-multiple-action@v1
        with:
          find: ${{ steps.wayback.outputs.replacements }}
          prefix: '(^|\\s+|\\()'
          suffix: '($|\\s+|\\))'
          
      - name: Create Pull Request
        # if: ${{ github.ref == 'refs/heads/main' }}
        uses: peter-evans/[email protected]
        with:
          token: ${{ secrets.PAT_REPO_FULL }}

Now that we've addressed the broken links, we're in a better state to revisit the Accessibility Insights Action that started this whole adventure!