Skip to content

Check links relevant to the changes in a PR only #238

@f-hollow

Description

@f-hollow

Naturally, it is desirable to minimize bothering your contributor with issues introduced by somebody else in your repo. So I need a way to check and report invalid links (1) added / updated in that contributor's PR only, or (2) existing links affected by the introduced changes.

It appeared to be harder than I expected. You cannot run lychee on the changes only, because the links internal to your repo (checked with my beloved --include-fragments option) can and will produce false positives.

Browsing around this repo, I came across mre's comment about separately checking existing links and new links. This idea nudged me in the right direction! The related issues #17 and #134 offer no immediately usable solution yet.

Below is the workflow that I created and tested so far. It works and does what I need.

Possible improvements:

  • I have to use git stash, otherwise GitHub CI complains about the untracked file links-main.txt (not sure why it works like this):
    $ git checkout my_feature_branch
    error: The following untracked working tree files would be overwritten by checkout:
      links-main.txt
    Please move or remove them before you switch branches.
    Aborting
    Action: I wonder if there is a way to further simplify the steps or reduce their number (like not using git stash)?
  • If a contributor removes a file, the existing internal links referring to this file wouldn't be reported as invalid, because .lycheeignore still contains them. My repo has a cron job checking all links in main once a week, which should take care of such issues for the time being.
    Action: In this workflow, it might be good to recheck existing internal links affected by removed files.
  • I am sure there are other ways to improve this workflow.
    Action: Any other ways to further optimize this workflow or make it more robust?

Your feedback or further improvements to this workflow would be very much appreciated!

The workflow that checks links relevant to the changes in a PR only

name: Check links in diffs

on:
  pull_request:
    branches: [main]

jobs:
  check-links:
    runs-on: ubuntu-latest
    steps:
      - name: Clone repository
        uses: actions/checkout@v4
        with:
          fetch-depth: 0
          ref: ${{github.event.pull_request.head.ref}}
          repository: ${{github.event.pull_request.head.repo.full_name}}

      - name: Check out main branch
        run: git checkout main

      - name: Dump all links from main
        id: dump_links_from_main
        uses: lycheeverse/lychee-action@v1
        with:
          args: |
            --dump
            --include-fragments
            .
          output: ./links-main.txt

      - name: Stash untracked files
        run: git stash push --include-untracked

      - name: Check out feature branch
        run: git checkout ${{ github.head_ref }}

      - name: Apply stashed changes
        # Apply stashed changes, ignore errors if stash is empty
        run: git stash pop || true

      - name: Append links-main.txt to .lycheeignore
        run: cat links-main.txt >> .lycheeignore

      - name: Check links
        uses: lycheeverse/lychee-action@v1
        with:
          args: |
            --no-progress
            --include-fragments
            .
          # Fail action on broken links
          fail: true

      - name: Suggestions
        if: failure()
        run: |
          echo -e "\nPlease review the links reported in the Check links step above."
          echo -e "If a link is valid but fails due to a CAPTCHA challenge, IP blocking, login requirements, etc.,
          consider adding such links to .lycheeignore file to bypass future checks.\n"
          exit 1

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions