Files
floci/.github/release-tooling/contributors.test.mjs
T
Hector Ventura 6fc8eb1eec chore(ci): mention the contributors of each release in its notes (#9841)
A local semantic-release plugin, after the release notes generator, looks
up the pull request each squash-merged commit names, 100 per GraphQL
request, and appends a Contributors section: every human author by
@handle, alphabetically, with the ones whose first merged pull request
is in the release called out above the list. It reaches CHANGELOG.md,
the release commit and the GitHub Release alike. A GitHub API failure
leaves the section out instead of failing the release. CI runs its Node
tests with the other release script tests.
2026-12-01 19:08:27 -04:00

118 lines
4.5 KiB
JavaScript

// node --test .github/release-tooling/contributors.test.mjs
import assert from 'node:assert/strict';
import { test } from 'node:test';
import {
formatSection, generateNotes, isExcluded, pullRequestNumbers,
} from './contributors.mjs';
const pullRequests = {
101: { login: 'zed', __typename: 'User', mergedAt: '2026-11-20T10:00:00Z' },
102: { login: 'Alice', __typename: 'User', mergedAt: '2026-11-21T10:00:00Z' },
103: { login: 'dependabot', __typename: 'Bot', mergedAt: '2026-11-22T10:00:00Z' },
104: { login: 'zed', __typename: 'User', mergedAt: '2026-11-19T10:00:00Z' },
105: null,
};
const mergedBefore = { zed: 0, Alice: 3 };
/** A GitHub GraphQL endpoint answering the pull request and search queries from the tables above. */
function fakeFetch(calls) {
return async (url, init) => {
const { query } = JSON.parse(init.body);
calls.push(query);
const data = {};
if (query.includes('repository(')) {
data.repository = {};
for (const [, number] of query.matchAll(/pullRequest\(number: (\d+)\)/g)) {
const pr = pullRequests[number];
data.repository[`p${number}`] = {
number: Number(number),
mergedAt: pr?.mergedAt ?? null,
author: pr ? { login: pr.login, __typename: pr.__typename } : null,
};
}
} else {
for (const [, alias, login] of query.matchAll(/(s\d+): search\(query: "[^"]*author:([^ ]+) [^"]*"/g)) {
data[alias] = { issueCount: mergedBefore[login] };
}
}
return { ok: true, json: async () => ({ data }) };
};
}
function context(commits) {
const logs = [];
return {
commits,
lastRelease: { gitTag: '2.6.0' },
options: { repositoryUrl: 'https://github.com/floci-io/floci.git' },
env: { GH_TOKEN: 'token' },
logger: { log: (m) => logs.push(m), error: (m) => logs.push(`ERROR ${m}`) },
logs,
};
}
test('reads the pull request number from a squash subject and a merge commit', () => {
assert.deepEqual(pullRequestNumbers([
{ subject: 'fix(s3): keep the tags (#102)' },
{ message: 'Merge pull request #7 from someone/branch\n\nbody' },
{ subject: 'chore(release): 2.6.0' },
{ subject: 'feat: mentions (#3) in the middle (#101)' },
{ subject: 'fix(s3): keep the tags (#102)' },
]), [7, 101, 102]);
});
test('leaves out bots and deleted accounts', () => {
assert.equal(isExcluded({ login: 'dependabot', __typename: 'Bot' }), true);
assert.equal(isExcluded({ login: 'renovate[bot]', __typename: 'User' }), true);
assert.equal(isExcluded(null), true);
assert.equal(isExcluded({ login: 'alice', __typename: 'User' }), false);
});
test('lists contributors alphabetically and names a newcomer by their earliest pull request', async () => {
const calls = [];
const notes = await generateNotes({ fetch: fakeFetch(calls), lastReleaseDate: '2026-11-15T00:00:00Z' }, context([
{ subject: 'fix: one (#101)' }, { subject: 'fix: two (#102)' }, { subject: 'chore(deps): bump (#103)' },
{ subject: 'fix: three (#104)' }, { subject: 'fix: ghost (#105)' },
]));
assert.equal(notes, [
'### Contributors',
'',
'**New contributors**',
'* @zed made their first contribution in #104',
'',
'Thanks to everyone who contributed to this release:',
'@Alice, @zed',
].join('\n'));
assert.equal(calls.length, 2);
assert.match(calls[1], /merged:<2026-11-15T00:00:00Z/);
});
test('names no newcomer for a first release', async () => {
const ctx = context([{ subject: 'fix: one (#101)' }]);
ctx.lastRelease = {};
const notes = await generateNotes({ fetch: fakeFetch([]) }, ctx);
assert.equal(notes, '### Contributors\n\nThanks to everyone who contributed to this release:\n@zed');
});
test('a GitHub failure leaves the section out instead of failing the release', async () => {
const ctx = context([{ subject: 'fix: one (#101)' }]);
const failing = async () => ({ ok: false, status: 502, json: async () => ({}) });
assert.equal(await generateNotes({ fetch: failing, lastReleaseDate: '2026-11-15T00:00:00Z' }, ctx), '');
assert.match(ctx.logs.at(-1), /ERROR Contributors: section left out, GitHub GraphQL answered 502/);
});
test('no pull requests or no token gives no section', async () => {
assert.equal(await generateNotes({ fetch: fakeFetch([]) }, context([{ subject: 'chore: tidy' }])), '');
const noToken = context([{ subject: 'fix: one (#101)' }]);
noToken.env = {};
assert.equal(await generateNotes({ fetch: fakeFetch([]) }, noToken), '');
});
test('formats nothing when every author was left out', () => {
assert.equal(formatSection([], new Set()), '');
});