Files
coder/scripts/docsredirectscheck/inventory.go
T
Ethan 267f1d8c9e fix: stop lint/docs-redirects depending on the live docs site (#32739)
`lint` has failed on every push to `main` and `release/2.40` since the
docs site was republished on the 24th, e.g.
https://github.com/coder/coder/actions/runs/45560160492/job/139021802160

coder/docs-engine#273 has the docs site generate one page per REST API
endpoint from `coderd/apidoc/swagger.json`, at
`reference/api/<section>/<endpoint>`, and list them in `llms.txt`.
`lint/docs-redirects` downloads that list, and treats any page on it
that isn't in `docs/manifest.json` as a page we removed, so it reported
all 389 endpoint pages:

```
docs route "reference/api/agents/authenticate-agent-on-aws-instance" no longer resolves (from the live page list https://coder.com/beta-docs/@main/llms.txt). Add a rule to docs/redirects.json, for example: {"source": "reference/api/agents/authenticate-agent-on-aws-instance", "destination": "/"}
```

Nothing is broken on the site, those pages are live, and I don't think
the docs engine is doing anything wrong either. The actual problem is
that `make lint` depends on whatever coder.com is serving at the time,
so a docs site release can turn `main` red without a commit here. It
also fails where nobody can act on it: a PR only downloads the list when
it changes `docs/manifest.json` or `docs/redirects.json`, whilst every
push to `main` downloads it, and that run is after the merge. So PRs
kept passing lint and merging onto a red `main`.

To fix, `lint/docs-redirects` now runs with `-offline`, and only
compares the manifest and redirects against the base branch. A PR that
moves or removes a page without a redirect still fails, as that
comparison never needed the download.

I think this is the right fix, rather than just teaching the check about
endpoint pages, because the download wasn't catching anything. The site
is built from the manifest, so a page can only be on the live list and
missing from the manifest if a PR removed it, in which case the base
comparison already fails that PR, or if the docs engine made the page
itself. Today 391 live pages aren't in the manifest, and all of them are
the second kind: the 389 endpoint pages, plus 2 pages the site serves
with hyphens for dots, which the check already special-cased. Endpoint
pages would be the second special case of that kind, and the next docs
engine change that adds pages would need a third.

What we lose is a second failure on `main` when a page removal is merged
past a failing lint, and that failure clears by itself once the site is
republished without the page. On a push there's no base branch to
compare against, so only the rules themselves are validated there.

Alternatives I considered:

- Only skipping the endpoint pages, which is what this PR did at first.
It fixes today's failure and leaves the cause in place, as above.
- Deriving the endpoint pages from `swagger.json` in the check, so
they're compared like any other page. That means copying the docs
engine's URL rules into this repo and keeping the two in step, in order
to catch renamed or removed endpoints. In the last 90 days on `main`
that was one removal and no renames.
- Deleting the live comparison outright. I've kept it, as it's still
useful to run by hand without `-offline`, e.g. after a change to how the
docs engine builds its routes. So that it's usable, the first commit
stays: routes of the endpoint page form are left out of that comparison
whilst their section page is still in the manifest, and it prints how
many were skipped. `go run ./scripts/docsredirectscheck -branch main`
goes from 389 failures to passing.
- Comparing against the previous commit on push runs, so that `main`
keeps a removed-route check. That's the comparison the PR already ran,
and a failure there comes after the merge.

The URL of an endpoint page changes whenever its `@Summary` or tag does,
and nothing here will notice. Catching that would mean requiring a
redirect on those API changes, which I've left out of this PR. This also
needs backporting to `release/2.40`.
2026-11-25 07:53:58 +00:00

369 lines
11 KiB
Go

package main
import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"path"
"regexp"
"slices"
"strings"
"time"
"golang.org/x/xerrors"
)
const (
// liveListURLFormat is the production page list of one docs version. The
// segment is "@main" for main and "vX.Y" for release/X.Y.
liveListURLFormat = "https://coder.com/beta-docs/%s/llms.txt"
// userAgent identifies the check to the site. Its CDN answers 403 to the
// default Go user agent in some cases.
userAgent = "coder-docsredirectscheck (+https://github.com/coder/coder)"
fetchAttempts = 4
fetchTimeout = 30 * time.Second
fetchMaxBytes = 32 << 20
maxRedirectHops = 3
)
// fetcher reads a URL, so tests never touch the network.
type fetcher interface {
// Get returns the HTTP status and body of url.
Get(ctx context.Context, url string) (status int, body []byte, err error)
}
type httpFetcher struct {
client *http.Client
}
func newHTTPFetcher() httpFetcher {
return httpFetcher{client: &http.Client{Timeout: fetchTimeout}}
}
func (f httpFetcher) Get(ctx context.Context, url string) (int, []byte, error) {
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
if err != nil {
return 0, nil, err
}
req.Header.Set("User-Agent", userAgent)
resp, err := f.client.Do(req)
if err != nil {
return 0, nil, err
}
defer resp.Body.Close()
body, err := io.ReadAll(io.LimitReader(resp.Body, fetchMaxBytes))
if err != nil {
return resp.StatusCode, nil, err
}
return resp.StatusCode, body, nil
}
// releaseBranchRe matches the release branches that have a docs version of their
// own.
var releaseBranchRe = regexp.MustCompile(`^release/(\d+\.\d+)$`)
// versionSegment returns the docs version segment the live page list of a
// target branch lives under, or false when the branch has no published docs.
func versionSegment(branch string) (string, bool) {
if branch == "main" {
return "@main", true
}
if m := releaseBranchRe.FindStringSubmatch(branch); m != nil {
return "v" + m[1], true
}
return "", false
}
// linkRe finds the target of a Markdown link.
var linkRe = regexp.MustCompile(`\]\(([^)\s]+)\)`)
// otherVersionRe matches the first path segment of a link into some version of
// the docs, which an unversioned link never has.
var otherVersionRe = regexp.MustCompile(`^(@[^/]+|v\d+(\.\d+)*)$`)
// liveListSiteBases are the base paths the docs site serves its versions
// under, in order of preference. The list URL may redirect from /beta-docs to
// /docs once the docs move there, and its links then use /docs.
var liveListSiteBases = []string{"/beta-docs", "/docs"}
// parseLiveList returns the routes of the links in an llms.txt body that point
// into the given version. The docs root is the empty route.
//
// The version that is served at a site's unversioned base path links its own
// pages without a version segment, so when no link carries the segment, the
// unversioned links count instead.
func parseLiveList(body []byte, segment string) []string {
var hrefs []string
for _, m := range linkRe.FindAllSubmatch(body, -1) {
href := strings.TrimPrefix(string(m[1]), "https://coder.com")
if i := strings.IndexAny(href, "?#"); i >= 0 {
href = href[:i]
}
hrefs = append(hrefs, href)
}
for _, base := range liveListSiteBases {
if routes := routesUnder(hrefs, base+"/"+segment); len(routes) > 0 {
return routes
}
}
for _, base := range liveListSiteBases {
unversioned := slices.DeleteFunc(routesUnder(hrefs, base), func(route string) bool {
first, _, _ := strings.Cut(route, "/")
return otherVersionRe.MatchString(first)
})
if len(unversioned) > 0 {
return unversioned
}
}
return nil
}
// routesUnder returns the routes of the hrefs at or below base, without
// duplicates and in order of appearance.
func routesUnder(hrefs []string, base string) []string {
var routes []string
seen := map[string]bool{}
for _, href := range hrefs {
if href != base && !strings.HasPrefix(href, base+"/") {
continue
}
route := convertPathToRoute(strings.TrimPrefix(href, base))
if route != "" && checkRoute(route) != "" {
continue
}
if !seen[route] {
seen[route] = true
routes = append(routes, route)
}
}
return routes
}
// errNotPublished means the site has no page list for a version, which is the
// case for a new release branch before its docs are published.
var errNotPublished = xerrors.New("docs version is not published")
// sleepContext waits for d, or returns early with the context's error when ctx
// is done first.
func sleepContext(ctx context.Context, d time.Duration) error {
t := time.NewTimer(d)
defer t.Stop()
select {
case <-ctx.Done():
return ctx.Err()
case <-t.C:
return nil
}
}
// fetchLiveRoutes reads the live page list of a version, retrying transient
// failures a bounded number of times. An empty list is an error. A 404 is not
// retried and returns an error that wraps errNotPublished.
func fetchLiveRoutes(ctx context.Context, f fetcher, url, segment string, wait func(context.Context, time.Duration) error) ([]string, error) {
var lastErr error
for attempt := 0; attempt < fetchAttempts; attempt++ {
if attempt > 0 {
if err := wait(ctx, time.Duration(1<<(attempt-1))*time.Second); err != nil {
return nil, xerrors.Errorf("cannot read %s: %w", url, err)
}
}
status, body, err := f.Get(ctx, url)
switch {
case err != nil:
lastErr = err
case status == http.StatusNotFound:
return nil, xerrors.Errorf("%s answered HTTP status 404: %w", url, errNotPublished)
case status != http.StatusOK:
lastErr = xerrors.Errorf("HTTP status %d", status)
default:
routes := parseLiveList(body, segment)
if len(routes) == 0 {
return nil, xerrors.Errorf("%s lists no pages of docs version %s", url, segment)
}
return routes, nil
}
}
return nil, xerrors.Errorf("cannot read %s after %d attempts: %w", url, fetchAttempts, lastErr)
}
// inventory is the set of routes that must keep working, each with where it
// came from.
type inventory map[string][]string
func (inv inventory) add(route, origin string) {
if route == "" {
// The docs root always exists.
return
}
if !slices.Contains(inv[route], origin) {
inv[route] = append(inv[route], origin)
}
}
// apiReferenceRoute is the section that holds the REST API reference.
const apiReferenceRoute = "reference/api"
// isAPIEndpointRoute reports whether route is the page of one REST API
// endpoint, "reference/api/<section>/<endpoint>", below a section page that is
// live. The docs site generates these pages from coderd/apidoc/swagger.json,
// and the manifest lists only their section pages, so the manifest cannot say
// which of them exist.
func isAPIEndpointRoute(route string, live routeSet) bool {
section := path.Dir(route)
return path.Dir(section) == apiReferenceRoute && live.has(section)
}
// baseRuleSources returns the route each rule of a base branch's
// docs/redirects.json answers for: exact sources as written, and the prefix of
// a wildcard. It is lenient on purpose, because the base file may predate the
// current format. It reads the old absolute form, and skips what it cannot
// read.
func baseRuleSources(data []byte) []string {
var entries []struct {
Source string `json:"source"`
}
if err := json.Unmarshal(data, &entries); err != nil {
return nil
}
var routes []string
for _, e := range entries {
route := strings.TrimSuffix(convertLegacy(e.Source), "/*")
if checkRoute(route) == "" {
routes = append(routes, route)
}
}
return routes
}
// resolver decides whether a route still works in the head branch.
type resolver struct {
live routeSet
// hyphenated holds the live routes with "." replaced by "-".
hyphenated map[string]bool
rules []rule
// added holds the routes that are live in head and were not live in the
// base branch, which are the likely new homes of a renamed page. It is
// empty when there is no base manifest to compare with.
added []string
}
// newResolver builds a resolver for the head branch. baseLive is the base
// branch's live routes, or nil when they are not known.
func newResolver(live, baseLive routeSet, rules []rule) resolver {
r := resolver{live: live, hyphenated: live.hyphenated(), rules: rules}
if baseLive != nil {
for route := range live {
if route != "" && !baseLive.has(route) {
r.added = append(r.added, route)
}
}
slices.Sort(r.added)
}
return r
}
// isLive reports whether a route is a page of the head branch. beta-docs
// serves dotted segments with hyphens ("esr-2.24-2.29-upgrade" is served as
// "esr-2-24-2-29-upgrade"), so a route that equals a head route with "." replaced
// by "-" counts as present until the docs engine is fixed.
func (r resolver) isLive(route string) bool {
return r.live.has(route) || r.hyphenated[route]
}
// resolves reports whether a request for route ends on a page: it is a live
// route, or the head rules send it, within maxRedirectHops, to a live route,
// the docs root, or an allowed external URL. Every redirect counts as a hop,
// whatever its destination.
func (r resolver) resolves(route string) bool {
route = normalizeRoute(route)
for hops := 0; ; hops++ {
if r.isLive(route) {
return true
}
i := slices.IndexFunc(r.rules, func(rl rule) bool { return rl.matches(route) })
if i < 0 || !r.rules[i].dst.valid || hops == maxRedirectHops {
return false
}
kind, next := r.rules[i].target(route)
if kind != destRoute {
return true
}
route = next
}
}
// suggest picks a destination for a rule that restores route. In order of
// preference it is the one live page with the same last segment, the one route
// added by this change in the same parent section (a rename such as
// "admin/integrations/istio" to "admin/integrations/istio-mesh"), or the docs
// root when no page is an obvious home.
func (r resolver) suggest(route string) string {
last := func(s string) string {
s = strings.ReplaceAll(s, ".", "-")
return s[strings.LastIndex(s, "/")+1:]
}
var found []string
for candidate := range r.live {
if candidate != "" && last(candidate) == last(route) {
found = append(found, candidate)
}
}
if len(found) == 1 {
return found[0]
}
parent := func(s string) string {
i := strings.LastIndex(s, "/")
if i < 0 {
return ""
}
return s[:i]
}
found = found[:0]
for _, candidate := range r.added {
if parent(candidate) == parent(route) {
found = append(found, candidate)
}
}
if len(found) == 1 {
return found[0]
}
return "/"
}
// missingRoute is an inventory route that no longer works.
type missingRoute struct {
route string
origins []string
suggestion string
}
func (m missingRoute) rule() string {
return ruleJSON(m.route, m.suggestion, nil)
}
func (m missingRoute) message() string {
return fmt.Sprintf(
"docs route %q no longer resolves (from %s). Add a rule to docs/redirects.json, for example: %s",
m.route, strings.Join(m.origins, ", "), m.rule(),
)
}
// missingRoutes returns the inventory routes the head branch no longer serves
// or redirects, sorted by route.
func missingRoutes(inv inventory, r resolver) []missingRoute {
var missing []missingRoute
for route, origins := range inv {
if r.resolves(route) {
continue
}
missing = append(missing, missingRoute{route: route, origins: origins, suggestion: r.suggest(route)})
}
slices.SortFunc(missing, func(a, b missingRoute) int { return strings.Compare(a.route, b.route) })
return missing
}