diff --git a/.github/scripts/readme-contributors.mjs b/.github/scripts/readme-contributors.mjs
new file mode 100644
index 00000000..75993d80
--- /dev/null
+++ b/.github/scripts/readme-contributors.mjs
@@ -0,0 +1,62 @@
+// Keeps the contributors' avatars in README.md in step with GitHub's contributors list.
+// .github/workflows/readme-contributors.yml runs the default branch's copy of this module.
+import { readFileSync, writeFileSync } from "node:fs";
+
+export const START = "";
+export const END = "";
+
+/** Automation that GitHub lists as a user rather than a bot. */
+export const EXCLUDED = new Set(["semantic-release-bot"]);
+
+/** A second account of someone already listed, counted under their first. Keys are lowercase. */
+export const ALIASES = new Map([["alexandrebucci", "albucci"]]);
+
+const LOGIN = /^[A-Za-z0-9](?:[A-Za-z0-9-]*[A-Za-z0-9])?$/;
+
+/** Logins to show, most contributions first; ties keep GitHub's order. */
+export function pickContributors(contributors) {
+ const people = new Map();
+ for (const contributor of contributors ?? []) {
+ const login = contributor?.login;
+ if (contributor?.type !== "User" || typeof login !== "string" || !LOGIN.test(login)) continue;
+ const lower = login.toLowerCase();
+ if (EXCLUDED.has(lower)) continue;
+ const key = ALIASES.get(lower) ?? lower;
+ const person = people.get(key) ?? { login: ALIASES.has(lower) ? ALIASES.get(lower) : login, contributions: 0 };
+ if (lower === key) person.login = login;
+ person.contributions += Number(contributor.contributions) || 0;
+ people.set(key, person);
+ }
+ return [...people.values()].sort((a, b) => b.contributions - a.contributions).map((person) => person.login);
+}
+
+export function renderAvatars(logins) {
+ const links = logins.map(
+ (login) =>
+ `
`,
+ );
+ return ["
", ...links, "
"].join("\n"); +} + +/** The README with the avatars between the markers replaced. */ +export function replaceAvatars(readme, logins) { + const start = readme.indexOf(START); + const end = readme.indexOf(END); + if (start === -1 || end < start) throw new Error(`README.md needs ${START} and ${END} around the avatars.`); + // An empty list means GitHub's answer went wrong, not that everyone left. + if (logins.length === 0) throw new Error("GitHub listed no contributors; leaving README.md as it is."); + return `${readme.slice(0, start + START.length)}\n${renderAvatars(logins)}\n${readme.slice(end)}`; +} + +/** Rewrites README.md from GitHub's list and returns whether it changed. */ +export async function updateReadme({ github, context, core, path = "README.md" }) { + const { owner, repo } = context.repo; + const contributors = await github.paginate(github.rest.repos.listContributors, { owner, repo, per_page: 100 }); + const logins = pickContributors(contributors); + const before = readFileSync(path, "utf8"); + const after = replaceAvatars(before, logins); + const changed = after !== before; + if (changed) writeFileSync(path, after); + core.info(`${logins.length} contributors: ${logins.join(", ")}. README.md ${changed ? "updated" : "unchanged"}.`); + return changed; +} diff --git a/.github/scripts/readme-contributors.test.mjs b/.github/scripts/readme-contributors.test.mjs new file mode 100644 index 00000000..e3240dac --- /dev/null +++ b/.github/scripts/readme-contributors.test.mjs @@ -0,0 +1,81 @@ +import { test } from "node:test"; +import assert from "node:assert/strict"; +import { mkdtempSync, readFileSync, writeFileSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { END, START, pickContributors, renderAvatars, replaceAvatars, updateReadme } from "./readme-contributors.mjs"; + +const user = (login, contributions) => ({ login, type: "User", contributions }); + +test("orders people by contributions and keeps GitHub's order for ties", () => { + const logins = pickContributors([user("a", 5), user("b", 1), user("c", 9), user("d", 1)]); + assert.deepEqual(logins, ["c", "a", "b", "d"]); +}); + +test("leaves out bots, release automation and malformed logins", () => { + const logins = pickContributors([ + user("M4n0x", 10), + { login: "dependabot[bot]", type: "Bot", contributions: 4 }, + user("semantic-release-bot", 20), + user('x">