Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .github/workflows/pr-checks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,10 @@ jobs:
working-directory: pr-checks
run: npx tsx --test

- name: Run `pr-checks/changenotes.mts` to ensure that all unreleased change notes are valid
if: ${{ !cancelled() && steps.install-deps.outcome == 'success' }}
run: npx tsx pr-checks/changenotes.mts validate

- name: Verify all Actions use the same Node version
id: head-version
run: |
Expand Down
2 changes: 1 addition & 1 deletion eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ export default [
},
},
{
files: ["**/*.ts", "**/*.js"],
files: ["**/*.ts", "**/*.js", "**/*.mts"],
Comment thread
mario-campos marked this conversation as resolved.

rules: {
"@typescript-eslint/no-explicit-any": "off",
Expand Down
11 changes: 11 additions & 0 deletions pr-checks/changelog/validate.mts
Original file line number Diff line number Diff line change
Expand Up @@ -119,3 +119,14 @@ export function isValidChangenoteFile(filename: string): boolean {

return isValid;
}

/**
* Validates the change-note files of the given list of file paths, ignoring ".gitkeep".
* @param filepaths A list of filepaths to validate
* @returns True if all the paths are valid, false otherwise.
*/
export function isValidAllChangenoteFiles(filepaths: string[]): boolean {
return filepaths
.filter((f) => f !== ".gitkeep")
.reduce((r, filePath) => r && isValidChangenoteFile(filePath), true);
}
45 changes: 44 additions & 1 deletion pr-checks/changelog/validate.test.mts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import assert from "node:assert/strict";
import * as fs from "node:fs";
import * as path from "node:path";
import { describe, it } from "node:test";

import { withTmpFile } from "../../src/util";
import { withTmpDir, withTmpFile } from "../../src/util";

import {
hasValidChangenoteCategory,
isValidAllChangenoteFiles,
isValidChangenoteContent,
isValidChangenoteFile,
isValidChangenoteFilename,
Expand Down Expand Up @@ -150,6 +153,10 @@ await describe("isValidChangenoteFile", async () => {
);
});

await it("rejects a non-existent path", async () => {
assert.equal(isValidChangenoteFile("non-existent-file.md"), false);
});

await it("rejects invalid filename", async () => {
await withTmpFile(
"fix-bug.md",
Expand Down Expand Up @@ -180,3 +187,39 @@ await describe("isValidChangenoteFile", async () => {
);
});
});

await describe("isValidAllChangenoteFiles", async () => {
await it("accepts list of file paths of valid change-notes", async () => {
await withTmpDir(async (tmpDir) => {
const fileName1 = path.join(tmpDir, "2026-01-01-fix-bug.md");
const fileName2 = path.join(tmpDir, "2026-01-02-add-feature.md");
fs.writeFileSync(fileName1, "---\ncategory: fix\n---\n- Fixed a bug\n");
fs.writeFileSync(
fileName2,
"---\ncategory: feature\n---\n- Added a feature\n",
);
assert.equal(isValidAllChangenoteFiles([fileName1, fileName2]), true);
});
});

await it("accepts the empty list", async () => {
assert.equal(isValidAllChangenoteFiles([]), true);
});

await it("accepts list of .gitkeep", async () => {
assert.equal(isValidAllChangenoteFiles([".gitkeep"]), true);
});

await it("rejects list containing a file path to an invalid change-note", async () => {
await withTmpDir(async (tmpDir) => {
const fileName1 = path.join(tmpDir, "2026-01-01-fix-bug.md");
const fileName2 = path.join(tmpDir, "2026-01-02-wrong-category.md");
fs.writeFileSync(fileName1, "---\ncategory: fix\n---\n- Fixed a bug\n");
fs.writeFileSync(
fileName2,
"---\ncategory: foobar\n---\n- Added a feature\n",
);
assert.equal(isValidAllChangenoteFiles([fileName1, fileName2]), false);
});
});
});
31 changes: 17 additions & 14 deletions pr-checks/changenotes.mts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
#!/usr/bin/env npx tsx

import * as fs from "node:fs";
import { pathToFileURL } from "node:url";
import { parseArgs } from "node:util";

import { isValidChangenoteFile } from "./changelog/validate.mjs";
import { isValidAllChangenoteFiles } from "./changelog/validate.mjs";
import { CHANGENOTES_DIR } from "./config";

const entryPoint = process.argv[1];
if (entryPoint && import.meta.url === pathToFileURL(entryPoint).href) {
Expand All @@ -20,34 +22,35 @@ function main(): number {
allowPositionals: true,
strict: true,
});
const [command, ...paths] = positionals;
const [command] = positionals;
switch (command) {
case undefined:
case "help":
return usage();
case "validate":
return validate(paths);
return validate();
default:
console.error(`Unknown command: ${command}`);
return 1;
}
}

function usage(): number {
console.log(`Usage: changenotes.mts validate <path> [<path> ...]`);
console.log(`Usage: changenotes.mts validate`);
return 0;
}

function validate(paths: string[]): number {
let valid = true;
if (paths.length === 0) {
console.error("error: no paths provided (see 'help' command for usage)");
return 1;
}
for (const path of paths) {
if (!isValidChangenoteFile(path)) {
valid = false;
function validate(): number {
try {
if (isValidAllChangenoteFiles(fs.readdirSync(CHANGENOTES_DIR))) {
console.log(`All changenote files in '${CHANGENOTES_DIR}' are valid.`);
return 0;
}
} catch (error) {
console.error(
`Failed to read change-notes directory (${CHANGENOTES_DIR})`,
error,
);
}
return valid ? 0 : 1;
return 1;
}
3 changes: 3 additions & 0 deletions pr-checks/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ export const PACKAGE_JSON = path.join(REPO_ROOT, "package.json");
/** The path of the changelog. */
export const CHANGELOG_FILE = path.join(REPO_ROOT, "CHANGELOG.md");

/** The path to the unreleased change-notes directory. */
export const CHANGENOTES_DIR = path.join(REPO_ROOT, "unreleased-change-notes");

/** The path to the esbuild metadata file. */
export const BUNDLE_METADATA_FILE = path.join(REPO_ROOT, "meta.json");

Expand Down
Empty file.
Loading