feat(docs): publish generated news and per-version release notes
Derive a public news page and one release-notes page per component version from CHANGELOG.archied.md and CHANGELOG.archie.md, the notes the release process already freezes. - tools/newsgen writes docs/news/releases.json, the canonical artifact a host renders from, plus the MkDocs pages. Check mode fails when the committed files lag a changelog edit, and runs in task check; tools/release.sh --tag regenerates and stages them in the release commit, skipping files already current. - mkdocs.yml keeps the version pages out of the derived navigation without excluding them from the site.
This commit is contained in:
1 parent
0198724f14
commit
eaa53df4d8
103 files changed
+3552
-2
No files matched your search
@@ -0,0 +1,231 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"regexp"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// component is one independently versioned binary and the changelog that
|
||||
// carries its release notes. key is the tag prefix and the output directory;
|
||||
// name is how the component is written for a reader.
|
||||
type component struct {
|
||||
key string
|
||||
name string
|
||||
changelog string
|
||||
}
|
||||
|
||||
// components is the fixed publishing order. Archied leads because it is the
|
||||
// user-facing entry point; the order only breaks ties between releases that
|
||||
// share a date.
|
||||
var components = []component{
|
||||
{key: "archied", name: "archied", changelog: "CHANGELOG.archied.md"},
|
||||
{key: "archie", name: "archie-agent", changelog: "CHANGELOG.archie.md"},
|
||||
}
|
||||
|
||||
// release is one changelog section: the facts a host needs to render a version.
|
||||
type release struct {
|
||||
Component string `json:"component"`
|
||||
Name string `json:"name"`
|
||||
Version string `json:"version"`
|
||||
Date string `json:"date"`
|
||||
Body string `json:"body"`
|
||||
}
|
||||
|
||||
// releaseHeading matches a changelog release heading. The date is optional in
|
||||
// the pattern so `## [Unreleased]` is recognised and skipped rather than read as
|
||||
// a release with an invented date.
|
||||
var releaseHeading = regexp.MustCompile(`^## \[([^\]]+)\](?:\s*-\s*(\d{4}-\d{2}-\d{2}))?$`)
|
||||
|
||||
// inlineLink matches the target of a Markdown link or image.
|
||||
var inlineLink = regexp.MustCompile(`\]\(([^)]+)\)`)
|
||||
|
||||
// linkScheme matches an absolute link target: a URL scheme, a site-root path or
|
||||
// an in-page anchor. Everything else is relative to the changelog's directory.
|
||||
var linkScheme = regexp.MustCompile(`^[a-zA-Z][a-zA-Z0-9+.-]*:`)
|
||||
|
||||
// parseChangelog reads the release sections out of one component's changelog.
|
||||
func parseChangelog(c component, source string) ([]release, error) {
|
||||
var releases []release
|
||||
for _, section := range splitSections(source) {
|
||||
if !strings.HasPrefix(section.heading, "## [") {
|
||||
continue
|
||||
}
|
||||
match := releaseHeading.FindStringSubmatch(section.heading)
|
||||
if match == nil {
|
||||
return nil, fmt.Errorf("%s: %q is not a release heading; write `## [<version>] - <date>`", c.changelog, section.heading)
|
||||
}
|
||||
version, date := match[1], match[2]
|
||||
if version == "Unreleased" {
|
||||
continue
|
||||
}
|
||||
if date == "" {
|
||||
return nil, fmt.Errorf("%s: release %s has no date; write `## [<version>] - <date>`", c.changelog, version)
|
||||
}
|
||||
if section.body == "" {
|
||||
return nil, fmt.Errorf("%s: release %s has no notes", c.changelog, version)
|
||||
}
|
||||
if target, ok := relativeLink(section.body); ok {
|
||||
return nil, fmt.Errorf("%s: release %s has a relative link %q; release-notes links must be absolute", c.changelog, version, target)
|
||||
}
|
||||
releases = append(releases, release{
|
||||
Component: c.key,
|
||||
Name: c.name,
|
||||
Version: version,
|
||||
Date: date,
|
||||
Body: section.body,
|
||||
})
|
||||
}
|
||||
return releases, nil
|
||||
}
|
||||
|
||||
// section is a level-two block: its heading line and the body that follows it.
|
||||
type section struct {
|
||||
heading string
|
||||
body string
|
||||
}
|
||||
|
||||
// splitSections divides a changelog at every level-two heading. Level-three
|
||||
// headings stay inside their section's body, and a `##` line inside a fenced
|
||||
// code block is prose about markdown rather than a heading.
|
||||
func splitSections(source string) []section {
|
||||
var sections []section
|
||||
current := section{}
|
||||
started := false
|
||||
inFence := false
|
||||
for _, raw := range strings.Split(source, "\n") {
|
||||
line := strings.TrimRight(raw, " \t")
|
||||
if isFence(line) {
|
||||
inFence = !inFence
|
||||
}
|
||||
if !inFence && strings.HasPrefix(line, "## ") {
|
||||
if started {
|
||||
current.body = strings.Trim(current.body, "\n")
|
||||
sections = append(sections, current)
|
||||
}
|
||||
current = section{heading: line}
|
||||
started = true
|
||||
continue
|
||||
}
|
||||
if started {
|
||||
current.body += line + "\n"
|
||||
}
|
||||
}
|
||||
if started {
|
||||
current.body = strings.Trim(current.body, "\n")
|
||||
sections = append(sections, current)
|
||||
}
|
||||
return sections
|
||||
}
|
||||
|
||||
// isFence reports whether a line opens or closes a fenced code block.
|
||||
func isFence(line string) bool {
|
||||
trimmed := strings.TrimSpace(line)
|
||||
return strings.HasPrefix(trimmed, "```") || strings.HasPrefix(trimmed, "~~~")
|
||||
}
|
||||
|
||||
// relativeLink returns the first link target in the body that is not absolute.
|
||||
func relativeLink(body string) (string, bool) {
|
||||
for _, match := range inlineLink.FindAllStringSubmatch(body, -1) {
|
||||
target := strings.TrimSpace(match[1])
|
||||
if i := strings.IndexAny(target, " \t"); i >= 0 {
|
||||
target = strings.TrimSpace(target[:i]) // drop an optional link title
|
||||
}
|
||||
target = strings.Trim(target, "<>")
|
||||
if target == "" || strings.HasPrefix(target, "#") || strings.HasPrefix(target, "/") {
|
||||
continue
|
||||
}
|
||||
if linkScheme.MatchString(target) {
|
||||
continue
|
||||
}
|
||||
return target, true
|
||||
}
|
||||
return "", false
|
||||
}
|
||||
|
||||
// sortReleases orders releases newest first, then by component, then by
|
||||
// descending version. Every host renders this order, so the canonical artifact
|
||||
// is also the ordering contract.
|
||||
func sortReleases(releases []release) {
|
||||
sort.SliceStable(releases, func(i, j int) bool {
|
||||
a, b := releases[i], releases[j]
|
||||
if a.Date != b.Date {
|
||||
return a.Date > b.Date
|
||||
}
|
||||
if ai, bi := componentIndex(a.Component), componentIndex(b.Component); ai != bi {
|
||||
return ai < bi
|
||||
}
|
||||
return compareVersions(a.Version, b.Version) > 0
|
||||
})
|
||||
}
|
||||
|
||||
func componentIndex(key string) int {
|
||||
for i, c := range components {
|
||||
if c.key == key {
|
||||
return i
|
||||
}
|
||||
}
|
||||
return len(components)
|
||||
}
|
||||
|
||||
// compareVersions orders dotted versions by their numeric parts, so 1.9.10 is
|
||||
// newer than 1.9.9. A version carrying a prerelease is older than the same
|
||||
// version without one.
|
||||
func compareVersions(a, b string) int {
|
||||
aCore, aPre := splitPrerelease(a)
|
||||
bCore, bPre := splitPrerelease(b)
|
||||
if c := compareDotted(aCore, bCore); c != 0 {
|
||||
return c
|
||||
}
|
||||
switch {
|
||||
case aPre == bPre:
|
||||
return 0
|
||||
case aPre == "":
|
||||
return 1
|
||||
case bPre == "":
|
||||
return -1
|
||||
case aPre < bPre:
|
||||
return -1
|
||||
default:
|
||||
return 1
|
||||
}
|
||||
}
|
||||
|
||||
func splitPrerelease(version string) (core, prerelease string) {
|
||||
if i := strings.IndexAny(version, "-+"); i >= 0 {
|
||||
return version[:i], version[i+1:]
|
||||
}
|
||||
return version, ""
|
||||
}
|
||||
|
||||
func compareDotted(a, b string) int {
|
||||
as, bs := strings.Split(a, "."), strings.Split(b, ".")
|
||||
for i := 0; i < len(as) || i < len(bs); i++ {
|
||||
var av, bv string
|
||||
if i < len(as) {
|
||||
av = as[i]
|
||||
}
|
||||
if i < len(bs) {
|
||||
bv = bs[i]
|
||||
}
|
||||
an, aerr := strconv.Atoi(av)
|
||||
bn, berr := strconv.Atoi(bv)
|
||||
switch {
|
||||
case aerr == nil && berr == nil:
|
||||
if an != bn {
|
||||
if an < bn {
|
||||
return -1
|
||||
}
|
||||
return 1
|
||||
}
|
||||
case av != bv:
|
||||
if av < bv {
|
||||
return -1
|
||||
}
|
||||
return 1
|
||||
}
|
||||
}
|
||||
return 0
|
||||
}
|
||||
@@ -0,0 +1,170 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
const sampleChangelog = `# archied changelog
|
||||
|
||||
## [1.31.0] - 2026-09-22
|
||||
|
||||
### A heading
|
||||
|
||||
- one bullet
|
||||
|
||||
## [1.30.0] - 2026-09-20
|
||||
|
||||
Just prose.
|
||||
|
||||
## [Unreleased]
|
||||
|
||||
Future work.
|
||||
|
||||
## Licence
|
||||
|
||||
Not a release section.
|
||||
`
|
||||
|
||||
func TestParseChangelogReadsReleaseSections(t *testing.T) {
|
||||
got, err := parseChangelog(components[0], sampleChangelog)
|
||||
if err != nil {
|
||||
t.Fatalf("parseChangelog error = %v", err)
|
||||
}
|
||||
if len(got) != 2 {
|
||||
t.Fatalf("got %d releases, want 2: %+v", len(got), got)
|
||||
}
|
||||
first := got[0]
|
||||
if first.Component != "archied" || first.Name != "archied" {
|
||||
t.Fatalf("first release component/name = %q/%q, want archied/archied", first.Component, first.Name)
|
||||
}
|
||||
if first.Version != "1.31.0" || first.Date != "2026-09-22" {
|
||||
t.Fatalf("first release version/date = %q/%q", first.Version, first.Date)
|
||||
}
|
||||
if first.Body != "### A heading\n\n- one bullet" {
|
||||
t.Fatalf("first release body = %q", first.Body)
|
||||
}
|
||||
if got[1].Version != "1.30.0" || got[1].Body != "Just prose." {
|
||||
t.Fatalf("second release = %+v", got[1])
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseChangelogSkipsUnreleased(t *testing.T) {
|
||||
got, err := parseChangelog(components[0], sampleChangelog)
|
||||
if err != nil {
|
||||
t.Fatalf("parseChangelog error = %v", err)
|
||||
}
|
||||
for _, r := range got {
|
||||
if r.Version == "Unreleased" {
|
||||
t.Fatal("Unreleased was published as a release")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseChangelogRejectsMalformedInput(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
source string
|
||||
wantSub string
|
||||
}{
|
||||
{
|
||||
name: "release heading without a date",
|
||||
source: "## [1.2.0]\n\nNotes.\n",
|
||||
wantSub: "no date",
|
||||
},
|
||||
{
|
||||
name: "release heading with an invalid date",
|
||||
source: "## [1.2.0] - 2026-9-2\n\nNotes.\n",
|
||||
wantSub: "not a release heading",
|
||||
},
|
||||
{
|
||||
name: "release without notes",
|
||||
source: "## [1.2.0] - 2026-01-01\n",
|
||||
wantSub: "no notes",
|
||||
},
|
||||
{
|
||||
name: "relative markdown link",
|
||||
source: "## [1.2.0] - 2026-01-01\n\nSee [the guide](guides/first-playbook.md).\n",
|
||||
wantSub: "relative link",
|
||||
},
|
||||
{
|
||||
name: "relative image link",
|
||||
source: "## [1.2.0] - 2026-01-01\n\n\n",
|
||||
wantSub: "relative link",
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
_, err := parseChangelog(components[0], tt.source)
|
||||
if err == nil {
|
||||
t.Fatal("expected an error")
|
||||
}
|
||||
if !strings.Contains(err.Error(), tt.wantSub) {
|
||||
t.Fatalf("error = %q, want it to mention %q", err, tt.wantSub)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseChangelogAllowsAbsoluteLinks(t *testing.T) {
|
||||
source := `## [1.2.0] - 2026-01-01
|
||||
|
||||
- [vulnerability](https://pkg.go.dev/vuln/GO-2026-6443), [anchor](#section),
|
||||
[root](/absolute), [angle](<https://example.com>), [mail](mailto:a@b.c).
|
||||
`
|
||||
if _, err := parseChangelog(components[0], source); err != nil {
|
||||
t.Fatalf("parseChangelog error = %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseChangelogKeepsFencedHeadingsInTheBody(t *testing.T) {
|
||||
source := "## [1.2.0] - 2026-01-01\n\n```sh\n## not a heading\n```\n\n- after the fence\n"
|
||||
got, err := parseChangelog(components[0], source)
|
||||
if err != nil {
|
||||
t.Fatalf("parseChangelog error = %v", err)
|
||||
}
|
||||
if len(got) != 1 {
|
||||
t.Fatalf("got %d releases, want 1", len(got))
|
||||
}
|
||||
want := "```sh\n## not a heading\n```\n\n- after the fence"
|
||||
if got[0].Body != want {
|
||||
t.Fatalf("body = %q, want %q", got[0].Body, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCompareVersions(t *testing.T) {
|
||||
tests := []struct {
|
||||
a, b string
|
||||
want int
|
||||
}{
|
||||
{"1.9.10", "1.9.9", 1},
|
||||
{"1.10.0", "1.9.0", 1},
|
||||
{"2.0.0", "1.99.99", 1},
|
||||
{"1.0.0", "2.0.0", -1},
|
||||
{"1.9.0", "1.9.0", 0},
|
||||
{"1.2.3", "1.2.3-rc1", 1},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
if got := compareVersions(tt.a, tt.b); got != tt.want {
|
||||
t.Fatalf("compareVersions(%q, %q) = %d, want %d", tt.a, tt.b, got, tt.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestSortReleasesNewestFirstThenComponentThenVersion(t *testing.T) {
|
||||
releases := []release{
|
||||
{Component: "archie", Name: "archie-agent", Version: "1.31.0", Date: "2026-09-22"},
|
||||
{Component: "archied", Name: "archied", Version: "1.9.9", Date: "2026-09-22"},
|
||||
{Component: "archied", Name: "archied", Version: "1.30.0", Date: "2026-09-20"},
|
||||
{Component: "archied", Name: "archied", Version: "1.9.10", Date: "2026-09-22"},
|
||||
{Component: "archied", Name: "archied", Version: "1.31.0", Date: "2026-09-22"},
|
||||
}
|
||||
sortReleases(releases)
|
||||
want := []string{"archied/1.31.0", "archied/1.9.10", "archied/1.9.9", "archie/1.31.0", "archied/1.30.0"}
|
||||
for i, w := range want {
|
||||
got := releases[i].Component + "/" + releases[i].Version
|
||||
if got != w {
|
||||
t.Fatalf("position %d = %s, want %s (all: %+v)", i, got, w, releases)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,203 @@
|
||||
// Command newsgen derives every published release fact from the component
|
||||
// changelogs.
|
||||
//
|
||||
// CHANGELOG.archied.md and CHANGELOG.archie.md are 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/data/generated/releases.json the canonical, host-neutral fact set
|
||||
// docs/news/index.md the news page
|
||||
// docs/news/<component>/<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"
|
||||
"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 every component changelog and returns the releases newest first.
|
||||
func load(repoRoot string) ([]release, error) {
|
||||
releases := []release{}
|
||||
for _, c := range components {
|
||||
source, err := os.ReadFile(filepath.Join(repoRoot, c.changelog))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("read %s: %w", c.changelog, err)
|
||||
}
|
||||
parsed, err := parseChangelog(c, string(source))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
releases = append(releases, parsed...)
|
||||
}
|
||||
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 changelogs")
|
||||
}
|
||||
}
|
||||
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 changelogs\n", len(releases))
|
||||
return nil
|
||||
}
|
||||
|
||||
// unownedPages lists files inside a generated component directory that this
|
||||
// tool does not produce. The news directory's own root is not a generated
|
||||
// directory, so a hand-written announcement there is left alone.
|
||||
func unownedPages(repoRoot string, files map[string][]byte) []string {
|
||||
var unowned []string
|
||||
for _, c := range components {
|
||||
dir := filepath.Join(repoRoot, newsDir, c.key)
|
||||
err := filepath.WalkDir(dir, 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 {
|
||||
unowned = append(unowned, slash)
|
||||
}
|
||||
return nil
|
||||
})
|
||||
if err != nil && !errors.Is(err, fs.ErrNotExist) {
|
||||
unowned = append(unowned, fmt.Sprintf("%s is unreadable: %v", c.key, err))
|
||||
}
|
||||
}
|
||||
return unowned
|
||||
}
|
||||
@@ -0,0 +1,153 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
const archiedFixture = `# archied changelog
|
||||
|
||||
## [1.31.0] - 2026-09-22
|
||||
|
||||
- gateway bullet
|
||||
`
|
||||
|
||||
const archieFixture = `# archie-agent changelog
|
||||
|
||||
## [1.31.0] - 2026-09-22
|
||||
|
||||
- runtime bullet
|
||||
`
|
||||
|
||||
func writeChangelogs(t *testing.T, root, archied, archie string) {
|
||||
t.Helper()
|
||||
for name, body := range map[string]string{
|
||||
"CHANGELOG.archied.md": archied,
|
||||
"CHANGELOG.archie.md": archie,
|
||||
} {
|
||||
if err := os.WriteFile(filepath.Join(root, name), []byte(body), 0o644); err != nil {
|
||||
t.Fatalf("write %s: %v", name, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func readFile(t *testing.T, root, name string) []byte {
|
||||
t.Helper()
|
||||
body, err := os.ReadFile(filepath.Join(root, name))
|
||||
if err != nil {
|
||||
t.Fatalf("read %s: %v", name, err)
|
||||
}
|
||||
return body
|
||||
}
|
||||
|
||||
func TestWriteThenCheckIsClean(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
writeChangelogs(t, root, archiedFixture, archieFixture)
|
||||
|
||||
if err := write(root); err != nil {
|
||||
t.Fatalf("write error = %v", err)
|
||||
}
|
||||
if err := check(root); err != nil {
|
||||
t.Fatalf("check after write error = %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestWriteIsIdempotent(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
writeChangelogs(t, root, archiedFixture, archieFixture)
|
||||
|
||||
if err := write(root); err != nil {
|
||||
t.Fatalf("first write error = %v", err)
|
||||
}
|
||||
first := readFile(t, root, releasesJSONPath)
|
||||
if err := write(root); err != nil {
|
||||
t.Fatalf("second write error = %v", err)
|
||||
}
|
||||
if second := readFile(t, root, releasesJSONPath); string(second) != string(first) {
|
||||
t.Fatalf("second write changed %s", releasesJSONPath)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCheckReportsAStaleChangelog(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
writeChangelogs(t, root, archiedFixture, archieFixture)
|
||||
if err := write(root); err != nil {
|
||||
t.Fatalf("write error = %v", err)
|
||||
}
|
||||
|
||||
writeChangelogs(t, root, archiedFixture+"\n## [1.32.0] - 2026-09-23\n\n- newer bullet\n", archieFixture)
|
||||
|
||||
err := check(root)
|
||||
if err == nil {
|
||||
t.Fatal("check accepted a committed tree that lags the changelog")
|
||||
}
|
||||
for _, want := range []string{"task news", "docs/news/archied/1.32.0.md"} {
|
||||
if !strings.Contains(err.Error(), want) {
|
||||
t.Fatalf("error = %q, want it to mention %q", err, want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestCheckReportsFilesItDoesNotOwn(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
writeChangelogs(t, root, archiedFixture, archieFixture)
|
||||
if err := write(root); err != nil {
|
||||
t.Fatalf("write error = %v", err)
|
||||
}
|
||||
|
||||
handwritten := filepath.Join(root, "docs", "news", "archied", "handwritten.md")
|
||||
if err := os.WriteFile(handwritten, []byte("mine\n"), 0o644); err != nil {
|
||||
t.Fatalf("write handwritten page: %v", err)
|
||||
}
|
||||
|
||||
err := check(root)
|
||||
if err == nil {
|
||||
t.Fatal("check ignored a file in a generated directory")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "handwritten.md") || !strings.Contains(err.Error(), "not generated") {
|
||||
t.Fatalf("error = %q, want it to name the unmanaged page", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestWriteLeavesOtherNewsFilesAlone(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
writeChangelogs(t, root, archiedFixture, archieFixture)
|
||||
if err := write(root); err != nil {
|
||||
t.Fatalf("write error = %v", err)
|
||||
}
|
||||
|
||||
announcement := filepath.Join(root, "docs", "news", "announcement.md")
|
||||
if err := os.WriteFile(announcement, []byte("hand-written\n"), 0o644); err != nil {
|
||||
t.Fatalf("write announcement: %v", err)
|
||||
}
|
||||
if err := write(root); err != nil {
|
||||
t.Fatalf("second write error = %v", err)
|
||||
}
|
||||
if _, err := os.Stat(announcement); err != nil {
|
||||
t.Fatalf("write removed a hand-written news page: %v", err)
|
||||
}
|
||||
if err := check(root); err != nil {
|
||||
t.Fatalf("check rejected a hand-written news page it does not own: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRealChangelogsParse(t *testing.T) {
|
||||
releases, err := load(filepath.Join("..", ".."))
|
||||
if err != nil {
|
||||
t.Fatalf("load real changelogs: %v", err)
|
||||
}
|
||||
seen := map[string]int{}
|
||||
for _, r := range releases {
|
||||
seen[r.Component]++
|
||||
if r.Version == "" || r.Date == "" || r.Body == "" {
|
||||
t.Fatalf("incomplete release: %+v", r)
|
||||
}
|
||||
}
|
||||
for _, c := range components {
|
||||
if seen[c.key] == 0 {
|
||||
t.Fatalf("%s produced no releases", c.changelog)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// releasesJSONPath is the canonical, host-neutral artifact every renderer
|
||||
// reads. It lives with the news it describes, in the one directory this tool
|
||||
// owns end-to-end, so it cannot collide with another generator's output.
|
||||
const releasesJSONPath = "docs/news/releases.json"
|
||||
|
||||
// newsDir is where the MkDocs adapter writes the news index and its pages.
|
||||
const newsDir = "docs/news"
|
||||
|
||||
// render returns every generated file keyed by repo-relative path. It is pure:
|
||||
// the same releases always produce the same bytes, so the write path can skip a
|
||||
// file that is already current.
|
||||
func render(releases []release) (map[string][]byte, error) {
|
||||
if releases == nil {
|
||||
releases = []release{}
|
||||
}
|
||||
canonical, err := encodeCanonical(releases)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
files := map[string][]byte{
|
||||
releasesJSONPath: canonical,
|
||||
newsDir + "/index.md": renderIndex(releases),
|
||||
}
|
||||
for _, r := range releases {
|
||||
files[fmt.Sprintf("%s/%s/%s.md", newsDir, r.Component, r.Version)] = renderRelease(r)
|
||||
}
|
||||
return files, nil
|
||||
}
|
||||
|
||||
// encodeCanonical writes the JSON every host reads. HTML escaping is off so
|
||||
// the artifact stays readable to a reviewer and to a non-Go consumer: the
|
||||
// changelogs write `->` and `<memory>`, not `\u003e` and `\u003cmemory\u003e`.
|
||||
func encodeCanonical(releases []release) ([]byte, error) {
|
||||
var buf bytes.Buffer
|
||||
encoder := json.NewEncoder(&buf)
|
||||
encoder.SetEscapeHTML(false)
|
||||
encoder.SetIndent("", " ")
|
||||
if err := encoder.Encode(releases); err != nil {
|
||||
return nil, fmt.Errorf("encode %s: %w", releasesJSONPath, err)
|
||||
}
|
||||
return buf.Bytes(), nil
|
||||
}
|
||||
|
||||
// renderIndex is the news page: one dated group per release day, newest first.
|
||||
func renderIndex(releases []release) []byte {
|
||||
var b strings.Builder
|
||||
b.WriteString("# News\n\n")
|
||||
b.WriteString("Release notes for archied and archie-agent, newest first.\n")
|
||||
lastDate := ""
|
||||
for _, r := range releases {
|
||||
if r.Date != lastDate {
|
||||
fmt.Fprintf(&b, "\n## %s\n\n", r.Date)
|
||||
lastDate = r.Date
|
||||
}
|
||||
fmt.Fprintf(&b, "- [%s %s](%s/%s.md)\n", r.Name, r.Version, r.Component, r.Version)
|
||||
}
|
||||
return []byte(b.String())
|
||||
}
|
||||
|
||||
// renderRelease is one version's page: the facts, and the changelog body
|
||||
// verbatim.
|
||||
func renderRelease(r release) []byte {
|
||||
var b strings.Builder
|
||||
fmt.Fprintf(&b, "# %s %s\n\n", r.Name, r.Version)
|
||||
fmt.Fprintf(&b, "Released %s. [All news](../index.md)\n\n", r.Date)
|
||||
b.WriteString(r.Body)
|
||||
b.WriteString("\n")
|
||||
return []byte(b.String())
|
||||
}
|
||||
@@ -0,0 +1,145 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func sampleReleases() []release {
|
||||
return []release{
|
||||
{Component: "archied", Name: "archied", Version: "1.31.0", Date: "2026-09-22", Body: "### Gateway\n\n- a bullet"},
|
||||
{Component: "archie", Name: "archie-agent", Version: "1.31.0", Date: "2026-09-22", Body: "- a runtime bullet"},
|
||||
{Component: "archied", Name: "archied", Version: "1.30.0", Date: "2026-09-20", Body: "Prose."},
|
||||
}
|
||||
}
|
||||
|
||||
func TestRenderEmitsTheCanonicalJSON(t *testing.T) {
|
||||
files, err := render(sampleReleases())
|
||||
if err != nil {
|
||||
t.Fatalf("render error = %v", err)
|
||||
}
|
||||
body, ok := files[releasesJSONPath]
|
||||
if !ok {
|
||||
t.Fatalf("render produced no %s (files: %v)", releasesJSONPath, keys(files))
|
||||
}
|
||||
var decoded []release
|
||||
if err := json.Unmarshal(body, &decoded); err != nil {
|
||||
t.Fatalf("canonical JSON does not decode into []release: %v", err)
|
||||
}
|
||||
if !reflect.DeepEqual(decoded, sampleReleases()) {
|
||||
t.Fatalf("round trip = %+v, want %+v", decoded, sampleReleases())
|
||||
}
|
||||
}
|
||||
|
||||
func TestRenderJSONKeepsHTMLCharactersReadable(t *testing.T) {
|
||||
releases := []release{{
|
||||
Component: "archied", Name: "archied", Version: "1.0.0", Date: "2026-01-01",
|
||||
Body: "a -> b and <memory>",
|
||||
}}
|
||||
files, err := render(releases)
|
||||
if err != nil {
|
||||
t.Fatalf("render error = %v", err)
|
||||
}
|
||||
got := string(files[releasesJSONPath])
|
||||
for _, want := range []string{"a -> b", "<memory>"} {
|
||||
if !strings.Contains(got, want) {
|
||||
t.Fatalf("canonical JSON escaped %q:\n%s", want, got)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestRenderWritesOnePagePerComponentVersion(t *testing.T) {
|
||||
files, err := render(sampleReleases())
|
||||
if err != nil {
|
||||
t.Fatalf("render error = %v", err)
|
||||
}
|
||||
for _, path := range []string{
|
||||
"docs/news/index.md",
|
||||
"docs/news/archied/1.31.0.md",
|
||||
"docs/news/archie/1.31.0.md",
|
||||
"docs/news/archied/1.30.0.md",
|
||||
} {
|
||||
if _, ok := files[path]; !ok {
|
||||
t.Fatalf("render did not produce %s (files: %v)", path, keys(files))
|
||||
}
|
||||
}
|
||||
if len(files) != 5 {
|
||||
t.Fatalf("render produced %d files, want 5: %v", len(files), keys(files))
|
||||
}
|
||||
}
|
||||
|
||||
func TestRenderIndexGroupsByDate(t *testing.T) {
|
||||
files, err := render(sampleReleases())
|
||||
if err != nil {
|
||||
t.Fatalf("render error = %v", err)
|
||||
}
|
||||
want := `# News
|
||||
|
||||
Release notes for archied and archie-agent, newest first.
|
||||
|
||||
## 2026-09-22
|
||||
|
||||
- [archied 1.31.0](archied/1.31.0.md)
|
||||
- [archie-agent 1.31.0](archie/1.31.0.md)
|
||||
|
||||
## 2026-09-20
|
||||
|
||||
- [archied 1.30.0](archied/1.30.0.md)
|
||||
`
|
||||
if got := string(files["docs/news/index.md"]); got != want {
|
||||
t.Fatalf("index markdown =\n%s\nwant\n%s", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRenderReleasePageKeepsTheBodyVerbatim(t *testing.T) {
|
||||
files, err := render(sampleReleases())
|
||||
if err != nil {
|
||||
t.Fatalf("render error = %v", err)
|
||||
}
|
||||
want := `# archie-agent 1.31.0
|
||||
|
||||
Released 2026-09-22. [All news](../index.md)
|
||||
|
||||
- a runtime bullet
|
||||
`
|
||||
if got := string(files["docs/news/archie/1.31.0.md"]); got != want {
|
||||
t.Fatalf("release page =\n%s\nwant\n%s", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRenderIsDeterministic(t *testing.T) {
|
||||
first, err := render(sampleReleases())
|
||||
if err != nil {
|
||||
t.Fatalf("first render error = %v", err)
|
||||
}
|
||||
second, err := render(sampleReleases())
|
||||
if err != nil {
|
||||
t.Fatalf("second render error = %v", err)
|
||||
}
|
||||
if !reflect.DeepEqual(first, second) {
|
||||
t.Fatal("two renders of the same releases differ")
|
||||
}
|
||||
}
|
||||
|
||||
func TestRenderHandlesNoReleases(t *testing.T) {
|
||||
files, err := render(nil)
|
||||
if err != nil {
|
||||
t.Fatalf("render error = %v", err)
|
||||
}
|
||||
if got := string(files[releasesJSONPath]); got != "[]\n" {
|
||||
t.Fatalf("empty canonical JSON = %q, want %q", got, "[]\n")
|
||||
}
|
||||
if got := string(files["docs/news/index.md"]); got != "# News\n\nRelease notes for archied and archie-agent, newest first.\n" {
|
||||
t.Fatalf("empty index = %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
func keys(m map[string][]byte) []string {
|
||||
out := make([]string, 0, len(m))
|
||||
for k := range m {
|
||||
out = append(out, k)
|
||||
}
|
||||
return out
|
||||
}
|
||||
+7
-1
@@ -181,6 +181,12 @@ fi
|
||||
[ "$RUNTIME_VERSION" = "skip" ] || grep -q "^## \\[$RUNTIME_VERSION\\]" CHANGELOG.archie.md ||
|
||||
die "CHANGELOG.archie.md has no [$RUNTIME_VERSION] section; run --prepare first"
|
||||
|
||||
# The published release notes are derived from the changelogs, so regenerate
|
||||
# them after the maintainer's edit and ship them in the same commit as the
|
||||
# sections they describe. newsgen skips a file whose bytes already match, so a
|
||||
# re-run leaves the tree clean.
|
||||
go -C tools run -mod=readonly ./newsgen --repo-root ..
|
||||
|
||||
# Plain `[ cond ] && cmd` would abort the script under set -e whenever cond
|
||||
# is false, i.e. whenever a component is skipped.
|
||||
msg="chore(release):"
|
||||
@@ -191,7 +197,7 @@ if [ "$RUNTIME_VERSION" != "skip" ]; then
|
||||
msg="$msg archie v$RUNTIME_VERSION"
|
||||
fi
|
||||
|
||||
git add CHANGELOG.archied.md CHANGELOG.archie.md
|
||||
git add CHANGELOG.archied.md CHANGELOG.archie.md docs/news
|
||||
git commit -m "$msg"
|
||||
|
||||
if [ "$GATEWAY_VERSION" != "skip" ]; then
|
||||
|
||||
Reference in new issue
Block a user