Files
archie/tools/newsgen/main.go
T
sam 83c06b6aff feat(release): one release stream, one version, one page per version
CHANGELOG.archied.md and CHANGELOG.archie.md were two version streams that
drifted: a release that moved both components had two numbers, two pages and
two tags, and a reader had to visit two files to learn what shipped.

The merged history holds 65 versions from 64 archied and 34 archie sections --
33 that both components released, 31 only archied, one only archie -- so 98
published pages become 65. A version's date is the newest of its sections'
dates, and a component whose own date differs carries it on its label, because
the history has two dates and the merge must not invent one. `## [Unreleased]`
is not a release: it gets no page, and it stays at the top of the file.

A level-two heading is where the merge could silently drop a section, so the
merge is verified by content rather than by the generator: section counts per
source, the merged version set against the exact union, and every source `###`
title present both as a substring and, after stripping the component prefix, as
an identical heading. Counting `##` headings rather than releases overcounts by
one here, which is how the empty `[Unreleased]` block was caught.

- newsgen reads one file and writes `docs/news/<version>.md`, the index, the
  canonical `releases.json`, and `docs/news/redirects.json`. The redirect table
  is what keeps the retired `/news/<component>/<version>/` URLs alive; it is
  generated beside the pages so it cannot drift from them.
- release.sh takes one `--version`, keeps commit and tag (the Woodpecker
  pipeline that will own them does not exist yet), and exits 3 when the commit
  set justifies no release, so a pipeline can finish green without tagging.
- The consumers of the two files follow: the deploy workflow's tag gate and
  release-notes extraction, the zip contents, the update check's changelog
  links, the packaged-path note, the docs skill, and newsgen's tests.

The per-component detail survives as labelled sections inside each release, so
a reader still learns whether archied or the agent changed; what goes away is
the separate numbers and the separate pages.
2026-09-22 17:18:06 +10:00

211 lines
6.2 KiB
Go

// Command newsgen derives every published release fact from CHANGELOG.md.
//
// CHANGELOG.md is the release process's frozen notes: tools/release.sh
// --prepare writes a section, the maintainer edits it, and --tag refuses to tag
// a version without its section. Tags are deliberately not a source, because CI
// and deployment check out shallow and a site build has no tags to read.
//
// Output is generated and committed:
//
// docs/news/releases.json the canonical, host-neutral fact set
// docs/news/redirects.json the retired per-component URLs, mapped to pages
// docs/news/index.md the news page
// docs/news/<version>.md one page per release
//
// `newsgen check` reproduces the output in memory and fails when the committed
// files differ, which keeps a changelog edit from landing without its pages.
// `newsgen` rewrites only files it owns and leaves every other file in
// docs/news alone.
package main
import (
"bytes"
"errors"
"flag"
"fmt"
"io"
"io/fs"
"log"
"os"
"path/filepath"
"regexp"
"sort"
"strings"
)
type mode int
const (
modeWrite mode = iota
modeCheck
)
// options is a parsed command line.
type options struct {
mode mode
repoRoot string
}
func main() {
parsed, err := parseOptions(os.Args[1:])
if err != nil {
log.Fatalf("newsgen: %v", err)
}
if parsed.mode == modeCheck {
err = check(parsed.repoRoot)
} else {
err = write(parsed.repoRoot)
}
if err != nil {
log.Fatalf("newsgen: %v", err)
}
}
// parseOptions resolves the optional `check` subcommand and the flags that
// follow it. A leftover positional argument is an error rather than something
// to ignore: `newsgen --repo-root .. check` would otherwise fall through to the
// write path and rewrite the files that check promises not to touch.
func parseOptions(args []string) (options, error) {
selected := modeWrite
if len(args) > 0 && args[0] == "check" {
selected = modeCheck
args = args[1:]
}
flags := flag.NewFlagSet("newsgen", flag.ContinueOnError)
flags.SetOutput(io.Discard)
repoRoot := flags.String("repo-root", "..", "path to the repository root")
if err := flags.Parse(args); err != nil {
return options{}, err
}
if extra := flags.Args(); len(extra) > 0 {
return options{}, fmt.Errorf("unexpected argument %q", extra[0])
}
return options{mode: selected, repoRoot: *repoRoot}, nil
}
// load parses the changelog and returns the releases newest first.
func load(repoRoot string) ([]release, error) {
source, err := os.ReadFile(filepath.Join(repoRoot, changelogPath))
if err != nil {
return nil, fmt.Errorf("read %s: %w", changelogPath, err)
}
releases, err := parseChangelog(string(source))
if err != nil {
return nil, err
}
sortReleases(releases)
return releases, nil
}
// write regenerates the owned files, skipping any whose bytes already match so
// a re-run cannot dirty a release commit.
func write(repoRoot string) error {
releases, err := load(repoRoot)
if err != nil {
return err
}
files, err := render(releases)
if err != nil {
return err
}
updated := 0
for path, body := range files {
abs := filepath.Join(repoRoot, filepath.FromSlash(path))
current, err := os.ReadFile(abs)
switch {
case err == nil && bytes.Equal(current, body):
continue
case err != nil && !errors.Is(err, fs.ErrNotExist):
return fmt.Errorf("read %s: %w", path, err)
}
if err := os.MkdirAll(filepath.Dir(abs), 0o755); err != nil {
return fmt.Errorf("create %s: %w", filepath.Dir(path), err)
}
if err := os.WriteFile(abs, body, 0o644); err != nil {
return fmt.Errorf("write %s: %w", path, err)
}
updated++
}
fmt.Printf("newsgen: %d releases, %d files updated\n", len(releases), updated)
return nil
}
// check reports every difference between the committed files and a fresh
// derivation, without touching the working tree.
func check(repoRoot string) error {
releases, err := load(repoRoot)
if err != nil {
return err
}
files, err := render(releases)
if err != nil {
return err
}
var problems []string
for path, expected := range files {
current, err := os.ReadFile(filepath.Join(repoRoot, filepath.FromSlash(path)))
switch {
case errors.Is(err, fs.ErrNotExist):
problems = append(problems, path+" is missing")
continue
case err != nil:
return fmt.Errorf("read %s: %w", path, err)
}
if !bytes.Equal(current, expected) {
problems = append(problems, path+" does not match the changelog")
}
}
for _, path := range unownedPages(repoRoot, files) {
problems = append(problems, path+" is not generated; move it out of the generated directory")
}
if len(problems) > 0 {
sort.Strings(problems)
return fmt.Errorf("release notes are stale:\n %s\n fix with `task news`, then commit the result",
strings.Join(problems, "\n "))
}
fmt.Printf("newsgen: %d release notes match the changelog\n", len(releases))
return nil
}
// releasePageName matches the basename of a generated version page, so a page
// left behind for a release the changelog no longer carries is reported.
var releasePageName = regexp.MustCompile(`^[0-9]+\.[0-9]+`)
// unownedPages lists files under docs/news that this tool does not produce. A
// page from the retired per-component layout (a file in a subdirectory) and a
// stale version page are both unowned; anything else is a hand-written news
// item, and the news directory's root is where those live.
func unownedPages(repoRoot string, files map[string][]byte) []string {
var unowned []string
root := filepath.Join(repoRoot, newsDir)
err := filepath.WalkDir(root, func(path string, entry fs.DirEntry, err error) error {
if err != nil {
return err
}
if entry.IsDir() {
return nil
}
relative, err := filepath.Rel(repoRoot, path)
if err != nil {
return err
}
slash := filepath.ToSlash(relative)
if _, generated := files[slash]; generated {
return nil
}
if strings.Contains(slash[len(newsDir)+1:], "/") || looksLikeReleasePage(filepath.Base(path)) {
unowned = append(unowned, slash)
}
return nil
})
if err != nil && !errors.Is(err, fs.ErrNotExist) {
unowned = append(unowned, fmt.Sprintf("%s is unreadable: %v", newsDir, err))
}
return unowned
}
func looksLikeReleasePage(name string) bool {
base, ok := strings.CutSuffix(name, ".md")
return ok && releasePageName.MatchString(base)
}