mirror of
https://github.com/PHIDIAS0303/ExpCluster.git
synced 2026-11-20 07:23:13 +09:00
Merge branch 'explosivegaming:main' into weblate-expcluster-expscenario
This commit is contained in:
12 files changed
+438
-28
No files matched your search
@@ -0,0 +1,22 @@
|
||||
<!--
|
||||
Describe why and what has been changed by this pull request, feel free to also ask for input on decisions you are unsure about etc.
|
||||
|
||||
The changelog lines below will be copied directly from merged pull requests to the changelog on new releases. Changes are expected to preferentially link to the associated issues. If there are no relevant issues, link to the pull request itself.
|
||||
|
||||
If there are no relevant changelog entries replace the code block below with "None." followed by an optional reason for why it is not needed.
|
||||
-->
|
||||
|
||||
### Changelog
|
||||
```
|
||||
### Features
|
||||
- Added doodad to web interface. #999
|
||||
|
||||
### Changes
|
||||
- ...
|
||||
|
||||
### Fixes
|
||||
- ...
|
||||
|
||||
### Breaking Changes
|
||||
- ...
|
||||
```
|
||||
@@ -0,0 +1,30 @@
|
||||
name: Changelog
|
||||
|
||||
on:
|
||||
pull_request:
|
||||
types: [opened, edited, synchronize, reopened]
|
||||
|
||||
permissions:
|
||||
contents: read
|
||||
|
||||
concurrency:
|
||||
group: ${{ github.workflow }}-${{ github.event.pull_request.number }}
|
||||
cancel-in-progress: true
|
||||
|
||||
jobs:
|
||||
check:
|
||||
# Bot pull requests do not fill in the template.
|
||||
if: github.event.pull_request.user.type != 'Bot'
|
||||
runs-on: ubuntu-latest
|
||||
timeout-minutes: 5
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
with:
|
||||
sparse-checkout: scripts
|
||||
- uses: actions/setup-node@v4
|
||||
with:
|
||||
node-version: 24.x
|
||||
- name: Check changelog in pull request description
|
||||
env:
|
||||
PR_BODY: ${{ github.event.pull_request.body }}
|
||||
run: node scripts/check_pr_changelog.mts
|
||||
@@ -90,5 +90,5 @@ jobs:
|
||||
elif [[ $(jq '[.[].diagnostics[]] | length' check.json) -gt 0 ]] ; then
|
||||
exit 1
|
||||
else
|
||||
echo "✅ All checks succeeded!" | tee "$GITHUB_STEP_SUMMARY"
|
||||
echo "✅ All checks succeeded!" | tee -a "$GITHUB_STEP_SUMMARY"
|
||||
fi
|
||||
@@ -38,6 +38,8 @@ jobs:
|
||||
run: pnpm install --no-frozen-lockfile
|
||||
- name: Run tests
|
||||
run: pnpm --filter "@expcluster/*" run test
|
||||
- name: Run script tests
|
||||
run: node --test test/*.test.js
|
||||
- name: Run reference checks
|
||||
run: |
|
||||
# pnpm install runs prepare in dependency order, which hides missing
|
||||
|
||||
@@ -9,6 +9,7 @@ For developers wanting to add features please follow these guidelines:
|
||||
- Lua is checked with [emmylua](https://github.com/EmmyLuaLs/emmylua-analyzer-rust), configured by `.emmyrc.json`. Install the EmmyLua extension rather than sumnekolua / luals, and use factoriomod-debug to generate the factorio api types. Note that an inline cast must be written `--[[@as T]]`, the spaced form is not parsed.
|
||||
- Changes should be made on your own fork and merged into `main` through a pull request.
|
||||
- Each pull request should be limited to one feature or a few bug fixes and link to the related issue page.
|
||||
- Fill in the Changelog section of the pull request template, or replace its code block with `None.` and a reason. A CI check validates it and the release notes are built from it.
|
||||
- Pull requests are automatically linted, then built and tested against the latest clusterio `master`.
|
||||
- Pull requests are manually reviewed to maintain code and language quality.
|
||||
- New features should have the branch names: `feature/feature-name`
|
||||
|
||||
@@ -53,3 +53,21 @@ ExpDiscordAuth.link(player, code)
|
||||
ExpDiscordAuth.unlink(player)
|
||||
ExpDiscordAuth.get_linked(player) -- Prints the Discord account the player is linked to
|
||||
```
|
||||
|
||||
## Other plugins
|
||||
|
||||
Controller plugins can read links and react to them. Import the plugin class from the controller entrypoint and look it up with the controller.
|
||||
|
||||
```ts
|
||||
import { ControllerPlugin as DiscordAuth } from "@expcluster/discord-auth/dist/node/controller.js";
|
||||
|
||||
const discordAuth = DiscordAuth.get(controller); // undefined if exp_discord_auth is not loaded
|
||||
discordAuth.getLink("Player1"); // { id, name } of the Discord account, or undefined
|
||||
discordAuth.getAccount(discordId); // { id, name, playerNames }, or undefined
|
||||
discordAuth.getAccounts(); // every linked Discord account
|
||||
|
||||
discordAuth.hooks.linked.attach("my_plugin", (playerName, discordUser) => { /* ... */ });
|
||||
discordAuth.hooks.unlinked.attach("my_plugin", (playerName, discordUser) => { /* ... */ });
|
||||
```
|
||||
|
||||
Moving a player to another Discord account calls `unlinked` for the old account and then `linked` for the new one. A Factorio account is linked to at most one Discord account. Add `@expcluster/discord-auth` as a dependency and a `references` entry for its `tsconfig.node.json`.
|
||||
@@ -14,6 +14,8 @@ const codeLength = 6;
|
||||
const codeLetters = "ABCDEFGHJKLMNPQRSTUVWXYZ23456789";
|
||||
|
||||
type DiscordUser = { id: string, name: string };
|
||||
/** A Discord account and the players linked to it, as given to other plugins. */
|
||||
export type DiscordAccount = DiscordUser & { playerNames: string[] };
|
||||
type Session = DiscordUser & { expiresMs: number, code?: string };
|
||||
type LinkCode = { sessionId: string, expiresMs: number };
|
||||
type WebLink = { playerName: string, expiresMs: number };
|
||||
@@ -41,7 +43,22 @@ function randomCode() {
|
||||
return code;
|
||||
}
|
||||
|
||||
export class DiscordAuthHooks extends lib.AsyncHookCollection {
|
||||
/** A player was linked to a Discord account. */
|
||||
readonly linked = this.newHook<[playerName: string, discordUser: DiscordUser]>();
|
||||
/** A player was unlinked from a Discord account, also when the link moves to another account. */
|
||||
readonly unlinked = this.newHook<[playerName: string, discordUser: DiscordUser]>();
|
||||
}
|
||||
|
||||
const loaded = new WeakMap<Controller, ControllerPlugin>();
|
||||
|
||||
export class ControllerPlugin {
|
||||
/** The plugin loaded on a controller, for other plugins to read links from. */
|
||||
static get(controller: Controller) {
|
||||
return loaded.get(controller);
|
||||
}
|
||||
|
||||
hooks: DiscordAuthHooks;
|
||||
/** Logins which have been through Discord, by session id */
|
||||
sessions = new Map<string, Session>();
|
||||
/** Codes waiting to be used in game, by code */
|
||||
@@ -58,6 +75,7 @@ export class ControllerPlugin {
|
||||
public name: string,
|
||||
public accounts: lib.SubscribableDatastore<messages.DiscordAccountRecord>,
|
||||
) {
|
||||
this.hooks = new DiscordAuthHooks(logger);
|
||||
controller.handle(messages.DiscordLinkRequest, this.handleLinkRequest.bind(this));
|
||||
controller.handle(messages.DiscordUnlinkRequest, this.handleUnlinkRequest.bind(this));
|
||||
controller.handle(messages.DiscordLinkGetRequest, this.handleLinkGetRequest.bind(this));
|
||||
@@ -100,7 +118,9 @@ export class ControllerPlugin {
|
||||
messages.DiscordAccountRecord.fromJSON.bind(messages.DiscordAccountRecord),
|
||||
));
|
||||
|
||||
return new ControllerPlugin(controller, context.logger, context.plugin.name, accounts);
|
||||
const plugin = new ControllerPlugin(controller, context.logger, context.plugin.name, accounts);
|
||||
loaded.set(controller, plugin);
|
||||
return plugin;
|
||||
}
|
||||
|
||||
async onSave() {
|
||||
@@ -352,6 +372,23 @@ export class ControllerPlugin {
|
||||
Links
|
||||
*/
|
||||
|
||||
/** The Discord account a player is linked to. */
|
||||
getLink(playerName: string): DiscordUser | undefined {
|
||||
const account = this.findAccount(playerName);
|
||||
return account ? { id: account.id, name: account.name } : undefined;
|
||||
}
|
||||
|
||||
getAccount(discordId: string): DiscordAccount | undefined {
|
||||
const account = this.accounts.get(discordId);
|
||||
return account ? { id: account.id, name: account.name, playerNames: [...account.factorioNames] } : undefined;
|
||||
}
|
||||
|
||||
getAccounts(): DiscordAccount[] {
|
||||
return [...this.accounts.values()].map(
|
||||
account => ({ id: account.id, name: account.name, playerNames: [...account.factorioNames] })
|
||||
);
|
||||
}
|
||||
|
||||
findAccount(playerName: string) {
|
||||
const name = playerName.toLowerCase();
|
||||
for (const account of this.accounts.values()) {
|
||||
@@ -371,13 +408,16 @@ export class ControllerPlugin {
|
||||
|
||||
const account = this.accounts.getMutable(found.id)!;
|
||||
const name = playerName.toLowerCase();
|
||||
const removed = account.factorioNames.find(linked => linked.toLowerCase() === name)!;
|
||||
account.factorioNames = account.factorioNames.filter(linked => linked.toLowerCase() !== name);
|
||||
if (account.factorioNames.length) {
|
||||
this.accounts.set(account);
|
||||
} else {
|
||||
this.accounts.delete(account);
|
||||
}
|
||||
return { id: account.id, name: account.name };
|
||||
const discordUser = { id: account.id, name: account.name };
|
||||
void this.hooks.unlinked.invoke(removed, { ...discordUser });
|
||||
return discordUser;
|
||||
}
|
||||
|
||||
async handleLinkRequest(request: messages.DiscordLinkRequest) {
|
||||
@@ -415,6 +455,7 @@ export class ControllerPlugin {
|
||||
account.name = discordUser.name;
|
||||
account.factorioNames = [...account.factorioNames, playerName];
|
||||
this.accounts.set(account);
|
||||
void this.hooks.linked.invoke(playerName, { id: discordUser.id, name: discordUser.name });
|
||||
}
|
||||
return { id: discordUser.id, name: discordUser.name };
|
||||
}
|
||||
|
||||
@@ -282,6 +282,77 @@ t.test("linking from the user page", t2 => {
|
||||
t2.end();
|
||||
});
|
||||
|
||||
t.test("api for other plugins", t2 => {
|
||||
/** Record every linked and unlinked hook call. */
|
||||
function recordHooks(plugin) {
|
||||
const calls = [];
|
||||
plugin.hooks.linked.attach("test", (playerName, discordUser) => {
|
||||
calls.push(["linked", playerName, discordUser]);
|
||||
});
|
||||
plugin.hooks.unlinked.attach("test", (playerName, discordUser) => {
|
||||
calls.push(["unlinked", playerName, discordUser]);
|
||||
});
|
||||
return calls;
|
||||
}
|
||||
|
||||
t2.test("static get() returns the plugin loaded on a controller", async t3 => {
|
||||
const { plugin, controller } = await startPlugin(t3);
|
||||
t3.equal(ControllerPlugin.get(controller), plugin);
|
||||
const other = new Controller(logger, [], new lib.ControllerConfig("controller", {
|
||||
"controller.database_directory": t3.testdir(),
|
||||
}));
|
||||
t3.equal(ControllerPlugin.get(other), undefined);
|
||||
});
|
||||
|
||||
t2.test(".getLink(), .getAccount() and .getAccounts() read links", async t3 => {
|
||||
const { plugin } = await startPlugin(t3);
|
||||
const session = await discordLogin(plugin, alice);
|
||||
await link(plugin, session, "Player1");
|
||||
await link(plugin, session, "Player2");
|
||||
await link(plugin, await discordLogin(plugin, bob), "Player3");
|
||||
|
||||
t3.same(plugin.getLink("player1"), alice);
|
||||
t3.equal(plugin.getLink("Player4"), undefined);
|
||||
t3.same(plugin.getAccount(alice.id), { ...alice, playerNames: ["Player1", "Player2"] });
|
||||
t3.equal(plugin.getAccount("300"), undefined);
|
||||
t3.same(plugin.getAccounts(), [
|
||||
{ ...alice, playerNames: ["Player1", "Player2"] },
|
||||
{ ...bob, playerNames: ["Player3"] },
|
||||
]);
|
||||
|
||||
plugin.getAccount(alice.id).playerNames.push("Player4");
|
||||
t3.same(plugin.getAccount(alice.id).playerNames, ["Player1", "Player2"], "returns copies");
|
||||
});
|
||||
|
||||
t2.test("hooks fire on link, move and unlink", async t3 => {
|
||||
const { plugin } = await startPlugin(t3);
|
||||
const calls = recordHooks(plugin);
|
||||
const aliceSession = await discordLogin(plugin, alice);
|
||||
await link(plugin, aliceSession, "Player1");
|
||||
await link(plugin, aliceSession, "player1");
|
||||
await link(plugin, await discordLogin(plugin, bob), "Player1");
|
||||
await plugin.handleUnlinkRequest(new messages.DiscordUnlinkRequest("player1"));
|
||||
await plugin.handleUnlinkRequest(new messages.DiscordUnlinkRequest("Player1"));
|
||||
t3.same(calls, [
|
||||
["linked", "Player1", alice],
|
||||
["unlinked", "Player1", alice],
|
||||
["linked", "Player1", bob],
|
||||
["unlinked", "Player1", bob],
|
||||
]);
|
||||
});
|
||||
|
||||
t2.test("hooks fire when linking from the user page", async t3 => {
|
||||
const { plugin } = await startPlugin(t3);
|
||||
const calls = recordHooks(plugin);
|
||||
const ticket = await plugin.handleLinkStartRequest(new messages.DiscordLinkStartRequest("Player1"));
|
||||
const state = new URL((await plugin.authorize(ticket)).redirect).searchParams.get("state");
|
||||
plugin.fetchDiscordUser = async () => alice;
|
||||
await plugin.callback({ code: "c", state }, state);
|
||||
t3.same(calls, [["linked", "Player1", alice]]);
|
||||
});
|
||||
t2.end();
|
||||
});
|
||||
|
||||
t.test("permissions", t2 => {
|
||||
const user = (name, granted = []) => ({
|
||||
name,
|
||||
|
||||
@@ -0,0 +1,71 @@
|
||||
// Checks the changelog section of a pull request body, read from the PR_BODY environment variable.
|
||||
import fs from "node:fs";
|
||||
import { checkPullRequestBody, changelogSections } from "./pr_to_changelog.mts";
|
||||
|
||||
const expectedFormat = [
|
||||
"### Changelog",
|
||||
"```",
|
||||
"### Features",
|
||||
"- Added doodad to web interface. #123",
|
||||
"",
|
||||
"### Fixes",
|
||||
"- Fixed doodad not showing up. #456",
|
||||
"```",
|
||||
"",
|
||||
"Or, when nothing user facing changed, replace the code block with:",
|
||||
"",
|
||||
"### Changelog",
|
||||
"None. Reason no change log is required.",
|
||||
].join("\n");
|
||||
|
||||
/** Returns the problems found in the body, an empty list means it is valid. */
|
||||
export function findChangelogProblems(body: string | null | undefined) {
|
||||
const { error, warnings } = checkPullRequestBody(body);
|
||||
return error ? [error] : warnings;
|
||||
}
|
||||
|
||||
function escapeAnnotation(text: string) {
|
||||
return text.replaceAll("%", "%25").replaceAll("\r", "%0D").replaceAll("\n", "%0A");
|
||||
}
|
||||
|
||||
function main() {
|
||||
const problems = findChangelogProblems(process.env.PR_BODY);
|
||||
const summaryPath = process.env.GITHUB_STEP_SUMMARY;
|
||||
if (!problems.length) {
|
||||
console.log("Changelog section is valid.");
|
||||
if (summaryPath) {
|
||||
fs.appendFileSync(summaryPath, "Changelog section is valid.\n");
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
for (const problem of problems) {
|
||||
console.log(`::error title=Invalid changelog::${escapeAnnotation(problem)}`);
|
||||
}
|
||||
const rules = `Valid sections are ${changelogSections.join(", ")}. ` +
|
||||
"Each entry must reference an issue, or this pull request if there is no issue.";
|
||||
console.log(`\n${rules}`);
|
||||
console.log(`Expected format in the pull request description:\n\n${expectedFormat}`);
|
||||
if (summaryPath) {
|
||||
fs.appendFileSync(summaryPath, [
|
||||
"## Invalid changelog",
|
||||
"",
|
||||
"The changelog section of the pull request description has problems:",
|
||||
"",
|
||||
...problems.map(problem => `- ${problem.replaceAll("<", "<")}`),
|
||||
"",
|
||||
rules,
|
||||
"Edit the description and this check runs again. Expected format:",
|
||||
"",
|
||||
"````markdown",
|
||||
expectedFormat,
|
||||
"````",
|
||||
"",
|
||||
].join("\n"));
|
||||
}
|
||||
process.exitCode = 1;
|
||||
}
|
||||
|
||||
if (import.meta.main) {
|
||||
main();
|
||||
}
|
||||
+17
-2
@@ -11,8 +11,8 @@ export async function githubFetch(
|
||||
init = {
|
||||
...init,
|
||||
headers: {
|
||||
...init.headers as Record<string, string> ?? {},
|
||||
"Accept": "application/vnd.github+json",
|
||||
...init.headers as Record<string, string> ?? {},
|
||||
}
|
||||
};
|
||||
if (process.env.GH_TOKEN) {
|
||||
@@ -24,7 +24,9 @@ export async function githubFetch(
|
||||
console.log(response.headers);
|
||||
throw new Error(`GitHub replied with ${response.status} ${response.statusText}: ${await response.text()}`);
|
||||
}
|
||||
if (!response.headers.get("Content-Type")?.startsWith("application/json")) {
|
||||
const accept = init.headers!["Accept"];
|
||||
const contentType = accept === "application/vnd.github+json" ? "application/json" : accept;
|
||||
if (!response.headers.get("Content-Type")?.startsWith(contentType)) {
|
||||
throw new Error(
|
||||
`GitHub replied with ${response.status} ${response.statusText}: ${response.headers.get("Content-Type")}`
|
||||
);
|
||||
@@ -41,6 +43,15 @@ export async function githubFetchJson<T>(
|
||||
return await response.json();
|
||||
}
|
||||
|
||||
export async function githubFetchText(
|
||||
path: string,
|
||||
query: Record<string, string> = {},
|
||||
init: RequestInit & { headers?: Record<string, string> } = {},
|
||||
): Promise<string> {
|
||||
const response = await githubFetch(path, query, init);
|
||||
return await response.text();
|
||||
}
|
||||
|
||||
export async function githubFetchJsonPaginated<T>(
|
||||
path: string,
|
||||
query: Record<string, string> = {},
|
||||
@@ -117,6 +128,10 @@ export interface Release {
|
||||
updated_at?: string | null,
|
||||
}
|
||||
|
||||
export interface Comparison {
|
||||
total_commits: number,
|
||||
}
|
||||
|
||||
export interface Commit {
|
||||
sha: string,
|
||||
commit: {
|
||||
|
||||
+100
-23
@@ -1,16 +1,26 @@
|
||||
import {
|
||||
type Comparison,
|
||||
type Release,
|
||||
type Issue,
|
||||
githubFetchJson,
|
||||
githubFetchJsonPaginated,
|
||||
githubFetchText,
|
||||
} from "./github_api.mts";
|
||||
|
||||
type Changelog = Record<string, string[]>;
|
||||
|
||||
type Statistics = {
|
||||
commits: number,
|
||||
pullRequests: number,
|
||||
filesChanged: number,
|
||||
lineAdditions: number,
|
||||
lineDeletions: number,
|
||||
};
|
||||
|
||||
const repository = "explosivegaming/ExpCluster";
|
||||
const branch = "main";
|
||||
|
||||
const changelogSections = [
|
||||
export const changelogSections = [
|
||||
"Major Features",
|
||||
"Features",
|
||||
"Fixes",
|
||||
@@ -69,7 +79,7 @@ function extractPullRequestChangelog(
|
||||
{
|
||||
const headerFail = skipToLine(parser, /^###? Changelog *$/);
|
||||
if (headerFail) {
|
||||
return [failure, , "Header not found"];
|
||||
return [failure, , "\"### Changelog\" header not found"];
|
||||
}
|
||||
parser.pos += 1;
|
||||
skipEmptyLines(parser);
|
||||
@@ -87,10 +97,19 @@ function extractPullRequestChangelog(
|
||||
if (blockEndFail) {
|
||||
return [failure, , "End of code block not found after header"];
|
||||
}
|
||||
return [success, parser.lines.slice(blockStartPos, parser.pos)];
|
||||
const lines = parser.lines.slice(blockStartPos, parser.pos);
|
||||
if (!lines.some(line => line.trim() !== "")) {
|
||||
return [failure, , "Code block is empty, replace it with \"None.\" if nothing applies"];
|
||||
}
|
||||
return [success, lines];
|
||||
}
|
||||
|
||||
function parseChangelog(parser: ParserState, pr: Issue, issues: Issue[]): [Changelog, string[]] {
|
||||
/** Pass no issues to skip the checks that need the GitHub API. */
|
||||
function parseChangelog(
|
||||
parser: ParserState,
|
||||
pr?: Issue,
|
||||
issues?: Issue[],
|
||||
): [Changelog, string[]] {
|
||||
let section: string | undefined;
|
||||
let changelog: Changelog = Object.create(null);
|
||||
let warnings: string[] = [];
|
||||
@@ -110,6 +129,10 @@ function parseChangelog(parser: ParserState, pr: Issue, issues: Issue[]): [Chang
|
||||
changelog[section] = changelog[section] ?? [];
|
||||
continue;
|
||||
}
|
||||
if (!section && /^None/i.test(line)) {
|
||||
warnings.push(`"${line}" on line ${parser.pos} must replace the code block, not sit inside it`);
|
||||
continue;
|
||||
}
|
||||
if (!section) {
|
||||
warnings.push(`Unexpected content "${line}" outside of a changelog section on line ${parser.pos}`);
|
||||
continue;
|
||||
@@ -122,6 +145,9 @@ function parseChangelog(parser: ParserState, pr: Issue, issues: Issue[]): [Chang
|
||||
let hasRef = false
|
||||
for (const ref of entry.matchAll(/#(\d+)/g)) {
|
||||
hasRef = true;
|
||||
if (!issues || !pr) {
|
||||
continue;
|
||||
}
|
||||
const id = Number.parseInt(ref[1], 10);
|
||||
const refIssue = issues.find(issue => issue.number === id);
|
||||
if (!refIssue) {
|
||||
@@ -147,26 +173,34 @@ function parseChangelog(parser: ParserState, pr: Issue, issues: Issue[]): [Chang
|
||||
return [changelog, warnings];
|
||||
}
|
||||
|
||||
export function checkPullRequestBody(
|
||||
body: string | null | undefined,
|
||||
pr?: Issue,
|
||||
issues?: Issue[],
|
||||
): { changelog: Changelog, error?: string, warnings: string[] } {
|
||||
if (!body) {
|
||||
return { changelog: Object.create(null), error: "Pull request has no body", warnings: [] };
|
||||
}
|
||||
const [
|
||||
changelogFail,
|
||||
changelogLines,
|
||||
changelogFailReason
|
||||
] = extractPullRequestChangelog(createParser(body.split(/\r?\n/)));
|
||||
if (changelogFail) {
|
||||
return { changelog: Object.create(null), error: changelogFailReason, warnings: [] };
|
||||
}
|
||||
const [changelog, warnings] = parseChangelog(createParser(changelogLines), pr, issues);
|
||||
return { changelog, warnings };
|
||||
}
|
||||
|
||||
function changelogFromPullRequests(pullRequests: Issue[], issues: Issue[]) {
|
||||
const mergedChangelog: Changelog = Object.create(null);
|
||||
for (const pr of pullRequests) {
|
||||
if (!pr.body) {
|
||||
console.error(`Failed to extract changelog in ${pr.html_url}: No body`)
|
||||
console.log(pr.title)
|
||||
const { changelog, error, warnings } = checkPullRequestBody(pr.body, pr, issues);
|
||||
if (error) {
|
||||
console.error(`Failed to extract changelog in ${pr.html_url}: ${error}`)
|
||||
continue;
|
||||
}
|
||||
const [
|
||||
changelogFail,
|
||||
changelogLines,
|
||||
changelogFailReason
|
||||
] = extractPullRequestChangelog(createParser(pr.body!.split(/\r?\n/)));
|
||||
if (changelogFail) {
|
||||
console.error(`Failed to extract changelog in ${pr.html_url}: ${changelogFailReason}`)
|
||||
console.log(pr.title)
|
||||
console.log(pr.body)
|
||||
continue;
|
||||
}
|
||||
const [changelog, warnings] = parseChangelog(createParser(changelogLines), pr, issues);
|
||||
for (const warning of warnings) {
|
||||
console.warn(`Warning in changelog for ${pr.html_url}: ${warning}`)
|
||||
}
|
||||
@@ -180,7 +214,7 @@ function changelogFromPullRequests(pullRequests: Issue[], issues: Issue[]) {
|
||||
async function fetchLastRelease() {
|
||||
// Assumes releases are ordered by their creation or tag time in descending order.
|
||||
const releases = await githubFetchJson<Release[]>(`/repos/${repository}/releases`);
|
||||
const latest = releases[0]
|
||||
const latest = releases.find(r => r.target_commitish === branch);
|
||||
if (!latest) {
|
||||
throw new Error(`Unable to find latest release on ${branch}`);
|
||||
}
|
||||
@@ -194,10 +228,41 @@ async function fetchIssuesUpdatedSince(since: string) {
|
||||
);
|
||||
}
|
||||
|
||||
async function fetchStatistics(lastRelease: Release, pullRequests: Issue[]): Promise<Statistics> {
|
||||
const path = `/repos/${repository}/compare/${lastRelease.tag_name}...${branch}`;
|
||||
const comparison = await githubFetchJson<Comparison>(path, { per_page: "1" });
|
||||
// The JSON response lists at most 300 files, the diff has all of them.
|
||||
const diff = await githubFetchText(path, {}, { headers: { "Accept": "application/vnd.github.diff" } });
|
||||
let filesChanged = 0;
|
||||
let lineAdditions = 0;
|
||||
let lineDeletions = 0;
|
||||
let inHunk = false; // Skips the ---/+++ file headers
|
||||
for (const line of diff.split("\n")) {
|
||||
if (line.startsWith("diff --git ")) {
|
||||
filesChanged += 1;
|
||||
inHunk = false;
|
||||
} else if (line.startsWith("@@")) {
|
||||
inHunk = true;
|
||||
} else if (inHunk && line.startsWith("+")) {
|
||||
lineAdditions += 1;
|
||||
} else if (inHunk && line.startsWith("-")) {
|
||||
lineDeletions += 1;
|
||||
}
|
||||
}
|
||||
return {
|
||||
commits: comparison.total_commits,
|
||||
pullRequests: pullRequests.length,
|
||||
filesChanged,
|
||||
lineAdditions,
|
||||
lineDeletions,
|
||||
};
|
||||
}
|
||||
|
||||
function printMarkdown(
|
||||
changelog: Changelog,
|
||||
issues: Issue[],
|
||||
users: Issue["user"][],
|
||||
statistics: Statistics,
|
||||
refText: (issue: Issue) => string,
|
||||
userText: (user: Issue["user"]) => string,
|
||||
) {
|
||||
@@ -223,8 +288,19 @@ function printMarkdown(
|
||||
}
|
||||
}
|
||||
|
||||
console.log("### Contributors\n");
|
||||
console.log(`Many thanks to the following for contributing to this release:`);
|
||||
console.log(users.map(userText).join("\n"))
|
||||
console.log(users.map(userText).join(", "));
|
||||
|
||||
const format = (value: number) => value.toLocaleString("en-US");
|
||||
console.log("\n### Statistics\n");
|
||||
console.log([
|
||||
`Commits: ${format(statistics.commits)}`,
|
||||
`Pull Requests: ${format(statistics.pullRequests)}`,
|
||||
`Files Changed: ${format(statistics.filesChanged)}`,
|
||||
`Additions: +${format(statistics.lineAdditions)}`,
|
||||
`Deletions: -${format(statistics.lineDeletions)}`,
|
||||
].join(" "));
|
||||
}
|
||||
|
||||
async function main() {
|
||||
@@ -239,17 +315,18 @@ async function main() {
|
||||
|
||||
const changelog = changelogFromPullRequests(pullRequests, issues);
|
||||
const users = [...new Map(pullRequests.map(issue => [issue.user.login, issue.user])).values()]
|
||||
const statistics = await fetchStatistics(lastRelease, pullRequests);
|
||||
|
||||
console.log();
|
||||
console.log("=== Github Release Markdown ===");
|
||||
printMarkdown(changelog, issues, users,
|
||||
printMarkdown(changelog, issues, users, statistics,
|
||||
issue => `${repository}#${issue.number}`,
|
||||
user => `@${user.login}`,
|
||||
);
|
||||
|
||||
console.log();
|
||||
console.log("=== Discord / Changelog Markdown ===");
|
||||
printMarkdown(changelog, issues, users,
|
||||
printMarkdown(changelog, issues, users, statistics,
|
||||
issue => `[#${issue.number}](<${issue.html_url}>)`,
|
||||
user => `[@${user.login}](<${user.html_url}>)`
|
||||
);
|
||||
|
||||
@@ -0,0 +1,62 @@
|
||||
import assert from "node:assert/strict";
|
||||
import { describe, it } from "node:test";
|
||||
|
||||
import { checkPullRequestBody } from "../scripts/pr_to_changelog.mts";
|
||||
import { findChangelogProblems } from "../scripts/check_pr_changelog.mts";
|
||||
|
||||
|
||||
describe("scripts/check_pr_changelog", function() {
|
||||
function body(...lines) {
|
||||
return ["Description.", "", "### Changelog", ...lines].join("\r\n");
|
||||
}
|
||||
|
||||
it("accepts entries in a code block", function() {
|
||||
const text = body(
|
||||
"```",
|
||||
"### Features",
|
||||
"- Added doodad. #12",
|
||||
"",
|
||||
"### Fixes",
|
||||
"- Fixed [#13](https://github.com/explosivegaming/ExpCluster/issues/13)",
|
||||
"```",
|
||||
);
|
||||
assert.deepEqual(findChangelogProblems(text), []);
|
||||
assert.deepEqual({ ...checkPullRequestBody(text).changelog }, {
|
||||
"Features": ["Added doodad. #12"],
|
||||
"Fixes": ["Fixed #13"],
|
||||
});
|
||||
});
|
||||
it("accepts None. in place of the code block", function() {
|
||||
assert.deepEqual(findChangelogProblems(body("", "None. Only changes CI.")), []);
|
||||
});
|
||||
it("rejects a missing body or section", function() {
|
||||
assert.deepEqual(findChangelogProblems(null), ["Pull request has no body"]);
|
||||
assert.deepEqual(findChangelogProblems("Description only."), ["\"### Changelog\" header not found"]);
|
||||
assert.deepEqual(findChangelogProblems(body("Text")), ["Code block not found after header"]);
|
||||
assert.deepEqual(findChangelogProblems(body("```", "### Fixes")), ["End of code block not found after header"]);
|
||||
assert.deepEqual(findChangelogProblems(body("```", "", "```")), [
|
||||
"Code block is empty, replace it with \"None.\" if nothing applies",
|
||||
]);
|
||||
});
|
||||
it("rejects None. inside the code block", function() {
|
||||
assert.deepEqual(findChangelogProblems(body("```", "None.", "```")), [
|
||||
"\"None.\" on line 1 must replace the code block, not sit inside it",
|
||||
]);
|
||||
});
|
||||
it("rejects malformed entries", function() {
|
||||
const text = body("```", "- Orphan #1", "### Bugs", "- Fixed it", "Loose text", "```");
|
||||
assert.deepEqual(findChangelogProblems(text), [
|
||||
"Unexpected content \"- Orphan #1\" outside of a changelog section on line 1",
|
||||
"Changelog section Bugs on line 2 is not one of " +
|
||||
"Major Features, Features, Fixes, Changes, Breaking Changes, Meta",
|
||||
"Changelog entry on line 3 does not reference an issue",
|
||||
"Unexpected content \"Loose text\" on line 4",
|
||||
]);
|
||||
});
|
||||
it("rejects the unedited template", function() {
|
||||
const text = body(
|
||||
"```", "### Features", "- Added doodad to web interface. #999", "", "### Changes", "- ...", "```",
|
||||
);
|
||||
assert.deepEqual(findChangelogProblems(text), ["Changelog entry on line 5 does not reference an issue"]);
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user