A local-first personal finance app
import * as childProcess from 'node:child_process';
import * as fs from 'node:fs/promises';
import { join } from 'node:path';
import { inspect, promisify } from 'node:util';
import { formatNotes, parseReleaseNotes } from '../src/release-notes/util.mjs';
const exec = promisify(childProcess.exec);
if (!process.env.GITHUB_REPOSITORY) {
throw new Error('GITHUB_REPOSITORY env var is not set');
}
const [owner, repo] = process.env.GITHUB_REPOSITORY.split('/');
const releaseBranch = process.env.RELEASE_BRANCH;
const notesBranch = process.env.NOTES_BRANCH;
const version = process.env.VERSION;
if (!releaseBranch || !notesBranch || !version) {
throw new Error(
'RELEASE_BRANCH, NOTES_BRANCH, and VERSION env vars are required',
);
}
const baseBranch = 'master';
const apiResult = await fetch('https://api.github.com/graphql', {
method: 'POST',
headers: {
Authorization: `bearer ${process.env.GITHUB_TOKEN}`,
'Content-Type': 'application/json',
},
body: JSON.stringify({
query: /* GraphQL */ `
query GetPRMetadata(
$name: String!
$owner: String!
$headRefName: String!
) {
repository(name: $name, owner: $owner) {
pullRequests(headRefName: $headRefName, first: 1) {
edges {
node {
number
headRefName
body
}
}
}
}
}
`,
variables: {
name: repo,
owner,
headRefName: notesBranch,
},
}),
}).then(res => res.json());
await collapsedLog('API Response', apiResult);
const prData = apiResult.data.repository.pullRequests.edges[0]?.node;
if (!prData) {
console.error(`No PR found for branch ${notesBranch}`);
process.exit(1);
}
const releaseDateMatch = (prData.body || '').match(
/<!-- release-date:(\d{4}-\d{2}-\d{2}) -->/,
);
if (!releaseDateMatch) {
console.error(
`PR for ${notesBranch} body missing <!-- release-date:YYYY-MM-DD --> marker`,
);
process.exit(1);
}
const releaseDate = releaseDateMatch[1];
const author = process.env.GITHUB_ACTOR;
if (!author) {
console.error('::error::GITHUB_ACTOR env var is not set');
process.exit(1);
}
const slug = version.replace(/\./g, '-');
const commitMessage = `Generate release notes for v${version}`;
const botName = 'github-actions[bot]';
const botEmail = '41898282+github-actions[bot]@users.noreply.github.com';
await exec(`git config user.name '${botName}'`);
await exec(`git config user.email '${botEmail}'`);
const AUTOGEN_MARKER = '<!-- release-notes:auto-generated -->';
await group('Prepare branch', async () => {
// recover deleted release note files from previous generation commits
await exec(`git fetch origin ${baseBranch}`, { stdio: 'inherit' });
const { stdout: mergeBase } = await exec(
`git merge-base HEAD origin/${baseBranch}`,
);
const base = mergeBase.trim();
const { stdout: genLog } = await exec(
`git log --grep='${commitMessage}' --format=%H ${base}..HEAD`,
);
const genCommits = genLog.split('\n').filter(Boolean);
console.log(
`Reversing upcoming-release-notes deletions from ${genCommits.length} prior generation commit(s)`,
);
const tmpDir = process.env.RUNNER_TEMP || '/tmp';
for (const sha of genCommits) {
const patchPath = join(tmpDir, `revert-${sha}.patch`);
try {
await exec(
`git diff --diff-filter=D ${sha}~1..${sha} -- upcoming-release-notes > ${patchPath}`,
);
const { size } = await fs.stat(patchPath);
if (size > 0) {
await exec(`git apply -R --3way ${patchPath}`, { stdio: 'inherit' });
}
} finally {
await fs.unlink(patchPath).catch(() => undefined);
}
}
await exec(`git fetch origin ${releaseBranch}`, { stdio: 'inherit' });
await exec(`git checkout origin/${releaseBranch} -- upcoming-release-notes`, {
stdio: 'inherit',
});
});
// Only notes present on the release branch belong in this release. The notes
// branch is created from master, so on a hotfix the directory also holds
// master's pending notes for changes that are not part of the release; those
// must be neither compiled nor deleted (deleting them here would propagate to
// master when this PR merges, silently dropping them from the next release).
const { stdout: releaseNotesList } = await exec(
`git ls-tree -r --name-only origin/${releaseBranch} -- upcoming-release-notes`,
);
const releaseNoteFiles = releaseNotesList
.split('\n')
.filter(Boolean)
.map(path => path.replace(/^upcoming-release-notes\//, ''));
const { notesByCategory, files } = await parseReleaseNotes(
'upcoming-release-notes',
owner,
repo,
`origin/${baseBranch}`,
releaseNoteFiles,
);
const categorizedNotes = formatNotes(notesByCategory);
await collapsedLog('Release Notes', categorizedNotes);
if (files.length === 0) {
console.log('No release notes found, nothing to generate');
process.exit(0);
}
const highlights = '- TODO: Add release highlights';
const blogPath = join(
'packages/docs/blog',
`${releaseDate}-release-${slug}.md`,
);
const releasesPath = 'packages/docs/docs/releases.md';
await group('Generate blog post', async () => {
const template = `---
title: Release ${version}
description: New release of Actual.
date: ${releaseDate}T10:00
slug: release-${version}
tags: [announcement, release]
in_app_notification: true
hide_table_of_contents: false
authors: ${author}
---
${highlights}
<!--truncate-->
**Docker Tag: ${version}**
${AUTOGEN_MARKER}
${categorizedNotes}
`;
let blogContent;
try {
const existing = await fs.readFile(blogPath, 'utf-8');
const idx = existing.indexOf(AUTOGEN_MARKER);
if (idx === -1) {
console.log(
`WARNING: ${blogPath} missing ${AUTOGEN_MARKER}, rewriting from template`,
);
blogContent = template;
} else {
blogContent =
existing.slice(0, idx + AUTOGEN_MARKER.length) +
'\n' +
categorizedNotes +
'\n';
}
} catch (e) {
if (e.code !== 'ENOENT') throw e;
blogContent = template;
}
await fs.writeFile(blogPath, blogContent);
console.log(`Wrote ${blogPath}`);
});
await group('Update releases.md', async () => {
const existing = await fs.readFile(releasesPath, 'utf-8');
const sectionRe = new RegExp(
`(^|\\n)## ${escapeRegExp(version)}\\n[\\s\\S]*?(?=\\n## |$)`,
);
const match = existing.match(sectionRe);
let updated;
if (match) {
const section = match[0];
const idx = section.indexOf(AUTOGEN_MARKER);
if (idx === -1) {
console.log(
`WARNING: section for ${version} in ${releasesPath} missing ${AUTOGEN_MARKER}, leaving as-is`,
);
updated = existing;
} else {
const newSection =
section.slice(0, idx + AUTOGEN_MARKER.length) + '\n' + categorizedNotes;
updated = existing.replace(section, newSection);
}
} else {
const newSection = `## ${version}
Release date: ${releaseDate}
${highlights}
**Docker Tag: ${version}**
${AUTOGEN_MARKER}
${categorizedNotes}`;
updated = existing.replace(
'# Release Notes\n',
`# Release Notes\n\n${newSection}\n`,
);
}
await fs.writeFile(releasesPath, updated);
console.log(`Updated ${releasesPath}`);
});
await group('Remove used release notes', async () => {
await Promise.all(
files.map(f => fs.unlink(join('upcoming-release-notes', f))),
);
});
await group('Format generated files', async () => {
await exec(`yarn exec oxfmt ${blogPath} ${releasesPath}`, {
stdio: 'inherit',
});
});
// The in-app notifications feed is generated from the blog and committed. This
// job pushes with GITHUB_TOKEN, which doesn't trigger the PR workflows that
// normally regenerate it (autofix.ci), so it has to be part of this commit.
const newsFeedPath = 'packages/desktop-client/src/data/news.json';
await group('Generate news feed', async () => {
await exec(
'node --import=extensionless/register --experimental-strip-types packages/ci-actions/bin/generate-news-feed.ts',
{ stdio: 'inherit' },
);
});
await group('Commit and push', async () => {
await exec(
`git add upcoming-release-notes packages/docs/blog packages/docs/docs/releases.md ${newsFeedPath}`,
{ stdio: 'inherit' },
);
try {
await exec('git diff --cached --quiet');
console.log('No changes to commit');
return;
} catch {
// there are staged changes
}
await exec(`git commit -m '${commitMessage}'`);
await exec('git push origin', { stdio: 'inherit' });
});
function escapeRegExp(str) {
return str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
}
async function collapsedLog(name, value) {
await group(name, () => {
if (typeof value === 'string') {
console.log(value);
} else {
console.log(inspect(value, { depth: null }));
}
});
}
async function group(name, cb) {
console.log(`::group::${name}`);
await cb();
console.log('::endgroup::');
}