Skip to content

Commit

Permalink
Add recipe for checking links in pull requests
Browse files Browse the repository at this point in the history
  • Loading branch information
mre committed Oct 25, 2024
1 parent d2c6ace commit b2e6a2d
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 0 deletions.
4 changes: 4 additions & 0 deletions astro.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ export default defineConfig({
{
label: "GitHub Action Recipes",
items: [
{
label: "Check Links in Pull Requests",
link: "/github_action_recipes/pull-requests",
},
{
label: "Replace with Archived Links",
link: "/github_action_recipes/archived-links",
Expand Down
106 changes: 106 additions & 0 deletions src/content/docs/github_action_recipes/pull-requests.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
---
title: Check Links in Pull Requests
---

This recipe shows how to check for broken links in pull requests. It only checks
newly added links by comparing against the base branch, reducing noise from
existing broken links.

## Usage

Add this workflow to `.github/workflows/check-links.yml` in your repository.

## Workflow

```yaml
name: Check Links In Pull Requests

on:
pull_request:
branches:
- main
# Optionally limit the check to certain file types
# paths:
# - '**/*.md'
# - '**/*.html'

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

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 base branch
run: git checkout ${{github.event.pull_request.base.ref}}

- name: Dump all links from ${{github.event.pull_request.base.ref}}
uses: lycheeverse/lychee-action@v2
with:
args: |
--dump
--include-fragments
.
output: ./existing-links.txt
continue-on-error: true # Don't fail if base branch check has issues

- 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
run: git stash pop || true

- name: Update ignore file
run: |
if [ -f "existing-links.txt" ]; then
cat existing-links.txt >> .lycheeignore
fi
- name: Check links
uses: lycheeverse/lychee-action@v2
with:
args: |
--no-progress
--include-fragments
.
- name: Provide helpful failure message
if: failure()
run: |
echo "::error::Link check failed! Please review the broken links reported above."
echo ""
echo "If certain links are valid but fail due to:"
echo "- CAPTCHA challenges"
echo "- IP blocking"
echo "- Authentication requirements"
echo "- Rate limiting"
echo ""
echo "Consider adding them to .lycheeignore to bypass future checks."
echo "Format: Add one URL pattern per line"
exit 1
```
## Explanation
The workflow is triggered on pull request events against the `main` branch.
It clones the repository and checks out the main branch to dump all links.
It then stashes untracked files and checks out the feature branch.
The stashed changes are applied and the links from the main branch are appended to the `.lycheeignore` file.
Finally, the links in the feature branch are checked and suggestions are provided if the check fails.

## References

- [Real-world example from the `pytorch/tutorials` repo](https://github.com/pytorch/tutorials/pull/3085)
- [Discussion on GitHub](https://github.com/lycheeverse/lychee-action/issues/238)

0 comments on commit b2e6a2d

Please sign in to comment.