mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-18 23:33:12 +09:00
Check the changelog section of pull request descriptions in CI
The check runs the same parser as scripts/pr_to_changelog.mts, so a description that passes CI is one the release script accepts. Checks that need the GitHub API (issue references existing, pointing at this pull request) still only run at release time. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
42e2e0284b
commit
3d1ca99027
4 files changed
+203
-16
No files matched your search
@@ -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. Only changes CI configuration.",
|
||||
].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();
|
||||
}
|
||||
+43
-16
@@ -20,7 +20,7 @@ type Statistics = {
|
||||
const repository = "clusterio/clusterio";
|
||||
const branch = "master";
|
||||
|
||||
const changelogSections = [
|
||||
export const changelogSections = [
|
||||
"Major Features",
|
||||
"Features",
|
||||
"Fixes",
|
||||
@@ -79,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);
|
||||
@@ -97,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[] = [];
|
||||
@@ -120,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;
|
||||
@@ -132,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) {
|
||||
@@ -157,23 +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}: Pull request has no body`);
|
||||
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}`)
|
||||
continue;
|
||||
}
|
||||
const [changelog, warnings] = parseChangelog(createParser(changelogLines), pr, issues);
|
||||
for (const warning of warnings) {
|
||||
console.warn(`Warning in changelog for ${pr.html_url}: ${warning}`)
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user