Skip to content

Commit c17d30e

Browse files
check-links: flag absolute self-links with a suggested relative link, probe external links on added lines
Absolute links to this site (https://sourcegraph.com/docs/..., the legacy docs.sourcegraph.com host, http://, //, www.) are findings even when the target exists: they leave preview deployments and local dev, and hide moved pages behind redirects. The finding proposes the relative link, following src/data/redirects.ts when the page moved. Version-pinned links (/@5.1/...) stay external. --diff replaces --changed-files and scopes two new features to added lines: --check-external requests each external link (HEAD, then GET on an error status, following redirects) and reports 404/410 only; --review writes a GitHub review with one suggested change per line, which the workflow posts, skipping suggestions already on the PR. Amp-Thread-ID: https://ampcode.com/threads/T-01a08a01-44c1-775b-84d0-d67ff9501905 Co-authored-by: Amp <amp@ampcode.com>
1 parent 41790f3 commit c17d30e

3 files changed

Lines changed: 266 additions & 33 deletions

File tree

‎.github/workflows/check-links.yml‎

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
name: Check links
22

33
# Reports internal links and #anchors that this PR breaks, compared with the
4-
# merge base. Pre-existing broken links on the base branch are ignored.
4+
# merge base, absolute links to this site, and external links on added lines
5+
# that 404. Pre-existing broken links on the base branch are ignored.
56

67
on:
78
pull_request:
@@ -40,7 +41,7 @@ jobs:
4041
run: |
4142
merge_base=$(git merge-base "$BASE_SHA" HEAD)
4243
git worktree add "$RUNNER_TEMP/base" "$merge_base"
43-
git diff --name-only "$merge_base" HEAD > "$RUNNER_TEMP/changed-files.txt"
44+
git diff -U0 "$merge_base" HEAD > "$RUNNER_TEMP/changes.diff"
4445
4546
- name: Record broken links already present on the base branch
4647
# Exit 1 means findings, which is expected here
@@ -55,9 +56,10 @@ jobs:
5556
# File links in the report open the file on the PR branch
5657
LINK_BASE: ${{ github.event.pull_request.head.repo.html_url }}/blob/${{ github.event.pull_request.head.ref }}
5758
run: |
58-
if node dev/check-links.mjs --check-anchors --format markdown \
59+
if node dev/check-links.mjs --check-anchors --check-external --format markdown \
5960
--baseline "$RUNNER_TEMP/base-links.json" \
60-
--changed-files "$RUNNER_TEMP/changed-files.txt" \
61+
--diff "$RUNNER_TEMP/changes.diff" \
62+
--review "$RUNNER_TEMP/review.json" \
6163
--link-base "$LINK_BASE" > "$RUNNER_TEMP/report.md"; then
6264
echo "broken=false" >> "$GITHUB_OUTPUT"
6365
else
@@ -94,6 +96,26 @@ jobs:
9496
gh pr comment "$PR_NUMBER" --body-file "$RUNNER_TEMP/comment.md"
9597
fi
9698
99+
- name: Suggest fixes as review comments
100+
# One suggested change per added line with a fix. Suggestions already on
101+
# the PR (same file, line, and text) are not posted again.
102+
if: steps.check.outputs.broken == 'true' && github.event.pull_request.head.repo.full_name == github.repository
103+
env:
104+
GH_TOKEN: ${{ github.token }}
105+
PR_NUMBER: ${{ github.event.pull_request.number }}
106+
run: |
107+
gh api "repos/$GITHUB_REPOSITORY/pulls/$PR_NUMBER/comments" --paginate \
108+
--jq '.[] | {path, line, body}' | jq -s . > "$RUNNER_TEMP/posted.json"
109+
jq --slurpfile posted "$RUNNER_TEMP/posted.json" \
110+
'.comments |= map(select(. as $comment | $posted[0] | index({path: $comment.path, line: $comment.line, body: $comment.body}) | not))' \
111+
"$RUNNER_TEMP/review.json" > "$RUNNER_TEMP/review-new.json"
112+
113+
if [ "$(jq '.comments | length' "$RUNNER_TEMP/review-new.json")" -gt 0 ]; then
114+
gh api --method POST "repos/$GITHUB_REPOSITORY/pulls/$PR_NUMBER/reviews" \
115+
--input "$RUNNER_TEMP/review-new.json" > /dev/null \
116+
|| echo "::warning::Could not post the suggested fixes; they are in the report above"
117+
fi
118+
97119
- name: Fail when this PR introduces broken links
98120
if: steps.check.outputs.broken == 'true'
99121
run: exit 1

‎AGENTS.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
- **Build**: `npm run build`
77
- **Dev**: `npm run dev`
88
- **Lint**: `npm run lint`
9-
- **Check links**: `npm run check-links -- --check-anchors` (CI comments on PRs that break links; see `dev/check-links.mjs`). When moving a page or renaming a heading, update every link to it; a redirect in `src/data/redirects.ts` does not satisfy the check
9+
- **Check links**: `npm run check-links -- --check-anchors` (CI comments on PRs that break links; see `dev/check-links.mjs`). When moving a page or renaming a heading, update every link to it; a redirect in `src/data/redirects.ts` does not satisfy the check. Link to this site with relative paths (`/admin/config/site-config`), never `https://sourcegraph.com/docs/…` or `https://docs.sourcegraph.com/…`. To also probe the external links you added: `npm run check-links -- --check-anchors --check-external --diff <(git diff -U0 origin/main)`
1010
- **Prove changed links resolve on a deploy**: `node dev/verify-links-live.mjs --site <vercel-preview-url>` prints a Markdown table for the PR description
1111

1212
## AI Chat Integration

0 commit comments

Comments
 (0)