Skip to content

armgit / kaneo

Public Imported from GitHub
0

⚡ Project management that doesn't become the project. Boards, Gantt charts, and GitHub integration. Self-host it or use Kaneo Cloud.

ci: build release notes from pull requests and credit contributors

Andrej367132d0ea71
diff --git a/AGENTS.md b/AGENTS.md
index 98455101d..47cee60d4 100644
--- a/AGENTS.md
+++ b/AGENTS.md
@@ -103,7 +103,13 @@ This is why the commit convention matters:
- `refactor:` / `docs:` → shown in the changelog, no bump on their own
- `test:` / `build:` / `ci:` / `chore:` / `style:` → no release, hidden from the notes
-The commit subject is the changelog entry that ships to users. A squash-merge subject like `fix: apply CodeRabbit auto-fixes` or `fix: remove text-sm` becomes a public release note; write what changed for the reader instead.
+Notes are generated by `scripts/release/notes.mjs`, which maps every commit back to the pull request that landed it and collapses the range to one entry per pull request, so review churn like `fix: apply CodeRabbit auto-fixes` no longer reaches the notes. The entry text is the pull request title when its type matches the commit's, and the commit subject otherwise; both are public, so write them for the reader. Commits pushed straight to `main` have no pull request and fall back to a commit link. Pull request authors are credited by handle, bots excluded.
+
+Preview any range before releasing:
+
+```bash
+node scripts/release/notes.mjs v2.21.0 HEAD
+```
Version-carrying files are listed in `scripts/release/apply-version.mjs`. Add new ones there rather than in a workflow step, because the image build and the release commit both run that script.
diff --git a/release.config.js b/release.config.js
index 223a9ed3a..b5a429f05 100644
--- a/release.config.js
+++ b/release.config.js
@@ -12,27 +12,7 @@ export default {
{ preset: "conventionalcommits" },
],
- [
- "@semantic-release/release-notes-generator",
- {
- preset: "conventionalcommits",
- presetConfig: {
- types: [
- { type: "feat", section: "Features" },
- { type: "fix", section: "Bug Fixes" },
- { type: "perf", section: "Performance Improvements" },
- { type: "revert", section: "Reverts" },
- { type: "refactor", section: "Code Refactoring" },
- { type: "docs", section: "Documentation" },
- { type: "test", section: "Tests", hidden: true },
- { type: "build", section: "Build System", hidden: true },
- { type: "ci", section: "Continuous Integration", hidden: true },
- { type: "chore", section: "Miscellaneous", hidden: true },
- { type: "style", section: "Styles", hidden: true },
- ],
- },
- },
- ],
+ "./scripts/release/notes.mjs",
[
"@semantic-release/exec",
diff --git a/scripts/release/notes.mjs b/scripts/release/notes.mjs
new file mode 100644
index 000000000..4f00d6c9c
--- /dev/null
+++ b/scripts/release/notes.mjs
@@ -0,0 +1,219 @@
+#!/usr/bin/env node
+// semantic-release generateNotes plugin.
+//
+// Kaneo merges most pull requests with merge commits, so a release range contains
+// every branch-internal commit rather than one commit per PR. Each commit is mapped
+// back to its pull request through the commit-association endpoint, which resolves
+// merge-commit branches as well as squashes, and the range is then collapsed to one
+// entry per pull request.
+//
+// Preview a range locally: node scripts/release/notes.mjs v2.20.0 v2.21.0
+import { execFileSync } from "node:child_process";
+
+const SECTIONS = [
+ ["feat", "Features"],
+ ["fix", "Bug Fixes"],
+ ["perf", "Performance Improvements"],
+ ["revert", "Reverts"],
+ ["refactor", "Code Refactoring"],
+ ["docs", "Documentation"],
+];
+
+const RANK = new Map(SECTIONS.map(([type], index) => [type, index]));
+const SUBJECT = /^(\w+)(?:\(([^)]*)\))?(!)?:\s*(.+)$/;
+const BREAKING = /^BREAKING[ -]CHANGE:/m;
+
+const git = (...args) =>
+ execFileSync("git", args, { encoding: "utf8", maxBuffer: 64 * 1024 * 1024 });
+
+function parse(commit) {
+ const match = SUBJECT.exec(commit.subject);
+ if (!match) return null;
+ const [, type, scope, bang, description] = match;
+ return {
+ ...commit,
+ type: type.toLowerCase(),
+ scope: scope || null,
+ description,
+ breaking: Boolean(bang) || BREAKING.test(commit.body),
+ };
+}
+
+function readCommits(from, to) {
+ const range = from ? `${from}..${to}` : to;
+ const raw = git("log", "--no-merges", "--format=%H%x00%s%x00%b%x1e", range);
+ return raw
+ .split("\x1e")
+ .map((entry) => entry.trim())
+ .filter(Boolean)
+ .map((entry) => {
+ const [hash, subject, body = ""] = entry.split("\x00");
+ return { hash, subject, body };
+ });
+}
+
+async function resolvePullRequests(commits, { repo, token, logger }) {
+ const found = new Map();
+ if (!token) {
+ logger.log("notes: no GitHub token, falling back to commit links");
+ return found;
+ }
+
+ const queue = [...commits];
+ const workers = Array.from({ length: 8 }, async () => {
+ for (let commit = queue.shift(); commit; commit = queue.shift()) {
+ try {
+ const response = await fetch(
+ `https://api.github.com/repos/${repo}/commits/${commit.hash}/pulls`,
+ {
+ headers: {
+ accept: "application/vnd.github+json",
+ authorization: `Bearer ${token}`,
+ "user-agent": "kaneo-release-notes",
+ },
+ },
+ );
+ if (!response.ok) continue;
+ // Oldest association wins; a commit cherry-picked later still credits the PR that introduced it.
+ const [pull] = await response.json();
+ if (pull) found.set(commit.hash, pull);
+ } catch (error) {
+ // Never fail the release over note decoration; the commit link is a fine fallback.
+ logger.log(
+ `notes: ${commit.hash.slice(0, 7)} lookup failed (${error.message})`,
+ );
+ }
+ }
+ });
+
+ await Promise.all(workers);
+ return found;
+}
+
+function collapse(commits, pulls) {
+ const entries = new Map();
+
+ for (const commit of commits) {
+ const pull = pulls.get(commit.hash);
+ const key = pull ? `pr:${pull.number}` : `commit:${commit.hash}`;
+ const existing = entries.get(key);
+
+ if (!existing) {
+ entries.set(key, { commit, pull });
+ continue;
+ }
+ // One line per pull request: the most significant change in it speaks for the whole.
+ const rank = (candidate) =>
+ RANK.get(candidate.type) ?? Number.MAX_SAFE_INTEGER;
+ if (
+ (existing.commit.breaking ? 0 : 1) > (commit.breaking ? 0 : 1) ||
+ rank(commit) < rank(existing.commit)
+ ) {
+ entries.set(key, { commit, pull });
+ }
+ }
+
+ return [...entries.values()];
+}
+
+function isBot(login) {
+ return !login || login.endsWith("[bot]") || login === "Copilot";
+}
+
+function render(entries, { repo }) {
+ const commitUrl = (hash) => `https://github.com/${repo}/commit/${hash}`;
+ const grouped = new Map(SECTIONS.map(([, heading]) => [heading, []]));
+ const breaking = [];
+ const credits = new Set();
+
+ for (const { commit, pull } of entries) {
+ if (pull && !isBot(pull.user?.login)) credits.add(pull.user.login);
+
+ const heading = grouped.has(sectionFor(commit)) ? sectionFor(commit) : null;
+ if (!heading && !commit.breaking) continue;
+
+ // The pull request title is the better summary when it agrees with the commit
+ // about what kind of change this is; when it disagrees it is usually describing
+ // the whole branch rather than the part that landed in this range.
+ const titled = pull
+ ? parse({ subject: pull.title, body: "", hash: "" })
+ : null;
+ const headline = titled && titled.type === commit.type ? titled : commit;
+
+ const scope = headline.scope ? `**${headline.scope}:** ` : "";
+ const reference = pull
+ ? `#${pull.number}`
+ : `[${commit.hash.slice(0, 7)}](${commitUrl(commit.hash)})`;
+ const line = `- ${scope}${headline.description}: ${reference}`;
+
+ if (commit.breaking) breaking.push(line);
+ else grouped.get(heading).push(line);
+ }
+
+ let notes = "";
+ if (breaking.length)
+ notes += `### BREAKING CHANGES\n\n${breaking.join("\n")}\n\n`;
+ for (const [, heading] of SECTIONS) {
+ const lines = grouped.get(heading);
+ if (lines.length) notes += `### ${heading}\n\n${lines.join("\n")}\n\n`;
+ }
+ if (credits.size) {
+ notes += `### Credits\n\nHuge thanks to ${formatList([...credits].map((login) => `@${login}`))} for helping!\n`;
+ }
+ return notes.trimEnd();
+}
+
+function sectionFor(commit) {
+ const index = RANK.get(commit.type);
+ return index === undefined ? null : SECTIONS[index][1];
+}
+
+function formatList(names) {
+ if (names.length === 1) return names[0];
+ if (names.length === 2) return `${names[0]} and ${names[1]}`;
+ return `${names.slice(0, -1).join(", ")}, and ${names.at(-1)}`;
+}
+
+async function build({ from, to, repo, token, logger }) {
+ const commits = readCommits(from, to).map(parse).filter(Boolean);
+ const pulls = await resolvePullRequests(commits, { repo, token, logger });
+ return render(collapse(commits, pulls), { repo });
+}
+
+export async function generateNotes(_pluginConfig, context) {
+ const { lastRelease, nextRelease, env, logger } = context;
+ return build({
+ from: lastRelease?.gitTag || lastRelease?.gitHead,
+ to: nextRelease.gitHead || "HEAD",
+ repo: env.GITHUB_REPOSITORY || "usekaneo/kaneo",
+ token: env.GITHUB_TOKEN || env.GH_TOKEN,
+ logger,
+ });
+}
+
+const invokedDirectly =
+ process.argv[1] && import.meta.url.endsWith(process.argv[1].split("/").pop());
+if (invokedDirectly) {
+ const [from, to = "HEAD"] = process.argv.slice(2);
+ const token =
+ process.env.GITHUB_TOKEN ||
+ process.env.GH_TOKEN ||
+ (() => {
+ try {
+ return execFileSync("gh", ["auth", "token"], {
+ encoding: "utf8",
+ }).trim();
+ } catch {
+ return "";
+ }
+ })();
+ console.log(
+ await build({
+ from,
+ to,
+ repo: "usekaneo/kaneo",
+ token,
+ logger: { log: () => {} },
+ }),
+ );
+}