Adds `docsredirectscheck`, a lint for `docs/redirects.json`, the list of
redirects the documentation website applies for pages that have moved or
been removed. It runs as `make lint/docs-redirects`, part of `make lint`
and `make lint-light`, so a bad redirect is caught in the pull request
that adds it. The file does not exist yet, and the check passes while it
is absent, so this can merge before or after the file does.
## What it checks
It fails on:
- a file that is not a JSON array of well-formed rules (`source`,
`destination`, optional boolean `permanent`, no unknown fields);
- syntax outside the supported subset: a source under `/docs/`, written
as an exact path, a trailing `/:name*`, or a trailing `/:name(.*)`, with
no version segment such as `/@main`;
- an internal destination that is not a page in `docs/manifest.json`, or
a pattern destination that points into a section with no pages or uses a
parameter the source does not define;
- a source that matches a live page, which would hide it;
- duplicate sources, chains (a destination that is another rule's
source), and loops.
It warns, and never fails, when a pull request removes routes from
`docs/manifest.json` while `docs/redirects.json` is unchanged and does
not already cover them. Pages are sometimes removed on purpose. The
comparison is by route rather than by file, so a rename that keeps its
URL (for example `admin/foo/index.md` to `admin/foo.md`) does not warn.
In GitHub Actions the warning is an annotation on the manifest. The base
is the first parent of the PR merge commit, the same approach
`scripts/check_emdash.sh` uses, and the warning is skipped with a note
when it is not available.
Routes are derived from `docs/manifest.json` the way the docs website
derives them, including its plain-suffix stripping of `README.md`,
`index.md`, and `.md`, so the check and the site agree about which pages
exist. `scripts/docsredirectscheck/README.md` documents the rule format
and the limits.
## Testing
- 128 Go tests pass under Go 1.26.5: route derivation, every failing and
passing rule shape, the absent-file pass, exit codes, and the warning
logic through a fake git reader.
- I checked that the tests can fail by breaking the code 16 ways (for
example dropping the `index.md` suffix, disabling the shadow check,
reporting loops twice). All 16 were caught. That run found one gap, a
missing test for "redirects file changed", which is now covered.
- Ran against the repository's real `docs/manifest.json`: passes with no
redirects file, and a file with one of each failure class reports every
problem with the rule index, the source, and how to fix it.
- Ran the warning against real git history: it warns for an older base,
stays silent at `HEAD`, and skips with a note for an unknown revision.
- Lint: `golangci-lint` v2.0.2 built with Go 1.26.5 against the
repository config reports 0 issues. `paralleltestctx`, `intxcheck`,
`typos` 1.46.1, `gofumpt` and `gci` (through `make fmt/go`), and `go
vet` pass.
CI: `lint` (which runs this check), `fmt`, `title`, and the Go and
end-to-end test jobs pass. The `gen` job, and so `required`, fails on
this branch for a reason this PR does not touch: `main` is failing it
too. `docs/manifest/generated/cli.json` has not been regenerated for the
`coder sharing` commands that landed in the latest commit on `main`. It
should clear once `main` is fixed and merged into this branch.
Not run locally: the full `make lint` and `make pre-commit`. The local
hooks are opt-in and were off. The annotation output has been exercised
by unit tests and a local run with `GITHUB_ACTIONS=true`, but not yet on
a real pull request merge commit; this PR does not change the manifest,
so it will not exercise it either. No workflow change is needed, because
the `lint` job already runs `make lint` for any change that is not
offlinedocs-only.
---
🤖 Built with AI assistance.
143 lines
4.3 KiB
Go
143 lines
4.3 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"slices"
|
|
"strings"
|
|
)
|
|
|
|
// Repository-relative paths the advisory check compares across revisions. They
|
|
// are fixed, independent of the -manifest and -redirects flags, because git
|
|
// addresses files by their place in the repository.
|
|
const (
|
|
manifestRepoPath = "docs/manifest.json"
|
|
redirectsRepoPath = "docs/redirects.json"
|
|
)
|
|
|
|
// maxListedRoutes bounds how many removed routes one warning spells out.
|
|
const maxListedRoutes = 10
|
|
|
|
// removedRoutes returns the routes live in base that are no longer live in
|
|
// head, sorted. It compares routes, not files, so a rename that keeps the
|
|
// public URL (for example admin/foo/index.md to admin/foo.md) is not a
|
|
// removal.
|
|
func removedRoutes(base, head map[string]bool) []string {
|
|
var removed []string
|
|
for route := range base {
|
|
if !head[route] {
|
|
removed = append(removed, route)
|
|
}
|
|
}
|
|
slices.Sort(removed)
|
|
return removed
|
|
}
|
|
|
|
// uncoveredRoutes returns the removed routes that no rule's source matches,
|
|
// preserving order.
|
|
func uncoveredRoutes(removed []string, rules []rule) []string {
|
|
var uncovered []string
|
|
for _, route := range removed {
|
|
covered := false
|
|
for _, r := range rules {
|
|
if r.matches(route) {
|
|
covered = true
|
|
break
|
|
}
|
|
}
|
|
if !covered {
|
|
uncovered = append(uncovered, route)
|
|
}
|
|
}
|
|
return uncovered
|
|
}
|
|
|
|
func advisoryMessage(uncovered []string) string {
|
|
listed := uncovered
|
|
more := ""
|
|
if len(listed) > maxListedRoutes {
|
|
more = fmt.Sprintf(" and %d more", len(listed)-maxListedRoutes)
|
|
listed = listed[:maxListedRoutes]
|
|
}
|
|
noun := "routes"
|
|
if len(uncovered) == 1 {
|
|
noun = "route"
|
|
}
|
|
return fmt.Sprintf(
|
|
"This change removes %d docs %s from %s without a matching redirect in %s: %s%s. "+
|
|
"If readers or other sites may still link to a removed page, add a redirect. "+
|
|
"If the removal is intentional, this warning can be ignored.",
|
|
len(uncovered), noun, manifestRepoPath, redirectsRepoPath, strings.Join(listed, ", "), more,
|
|
)
|
|
}
|
|
|
|
// annotation formats a GitHub Actions workflow command. The message must stay
|
|
// on one line, so the characters the runner treats as delimiters are escaped.
|
|
func annotation(level, file, title, message string) string {
|
|
escapeData := strings.NewReplacer("%", "%25", "\r", "%0D", "\n", "%0A")
|
|
escapeProperty := strings.NewReplacer("%", "%25", "\r", "%0D", "\n", "%0A", ":", "%3A", ",", "%2C")
|
|
return fmt.Sprintf("::%s file=%s,title=%s::%s", level, escapeProperty.Replace(file), escapeProperty.Replace(title), escapeData.Replace(message))
|
|
}
|
|
|
|
// runAdvisory warns, without ever failing the run, when the change under
|
|
// review removes docs routes and leaves docs/redirects.json alone. Pages are
|
|
// sometimes removed on purpose, which is why this is advice and not a rule.
|
|
// Every step that cannot be completed, for example in a shallow checkout,
|
|
// skips the check with a note rather than guessing.
|
|
func runAdvisory(o options, headLive map[string]bool, rules []rule) {
|
|
skip := func(why string) {
|
|
_, _ = fmt.Fprintf(o.stdout, "removed-route advisory skipped: %s\n", why)
|
|
}
|
|
|
|
base := o.base
|
|
if base == "" && o.getenv("GITHUB_BASE_REF") != "" {
|
|
// On a pull request, actions/checkout checks out the merge commit, whose
|
|
// first parent is exactly the base the change merges into.
|
|
base = "HEAD^1"
|
|
}
|
|
if base == "" {
|
|
skip("not a pull request run (pass -base <rev> to enable)")
|
|
return
|
|
}
|
|
if !o.git.HasRev(base) {
|
|
skip(fmt.Sprintf("base revision %q is not available (shallow checkout?)", base))
|
|
return
|
|
}
|
|
|
|
baseData, err := o.git.Show(base, manifestRepoPath)
|
|
if err != nil {
|
|
skip(fmt.Sprintf("cannot read %s at %s: %v", manifestRepoPath, base, err))
|
|
return
|
|
}
|
|
baseManifest, err := parseManifest(baseData)
|
|
if err != nil {
|
|
skip(fmt.Sprintf("%s at %s is not a valid manifest: %v", manifestRepoPath, base, err))
|
|
return
|
|
}
|
|
|
|
removed := removedRoutes(liveRoutes(baseManifest), headLive)
|
|
if len(removed) == 0 {
|
|
return
|
|
}
|
|
|
|
changed, err := o.git.Changed(base, redirectsRepoPath)
|
|
if err != nil {
|
|
skip(fmt.Sprintf("cannot compare %s with %s: %v", redirectsRepoPath, base, err))
|
|
return
|
|
}
|
|
if changed {
|
|
return
|
|
}
|
|
|
|
uncovered := uncoveredRoutes(removed, rules)
|
|
if len(uncovered) == 0 {
|
|
return
|
|
}
|
|
|
|
msg := advisoryMessage(uncovered)
|
|
if o.getenv("GITHUB_ACTIONS") == "true" {
|
|
_, _ = fmt.Fprintln(o.stdout, annotation("warning", manifestRepoPath, "Docs routes removed without a redirect", msg))
|
|
return
|
|
}
|
|
_, _ = fmt.Fprintf(o.stdout, "warning: %s\n", msg)
|
|
}
|