Skip to content

Commit 88b738c

Browse files
committed
Extend changetool to support directories
1 parent 1cf8f51 commit 88b738c

3 files changed

Lines changed: 80 additions & 9 deletions

File tree

‎pr-checks/changelog/validate.mts‎

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ export function hasValidChangenoteCategory(
8484
* @param filename The name of the change-note file to validate.
8585
* @returns True if the file is a valid change-note, false otherwise.
8686
*/
87-
export function isValidChangenoteFile(filename: string): boolean {
87+
function isValidChangenoteFile(filename: string): boolean {
8888
let isValid: boolean = true;
8989

9090
let fileData: string | undefined;
@@ -119,3 +119,28 @@ export function isValidChangenoteFile(filename: string): boolean {
119119

120120
return isValid;
121121
}
122+
123+
/**
124+
* Validates that the given path is a valid change-note file or directory of change-note files.
125+
* @param filepath The path to the change-note file or directory to validate.
126+
* @returns True if the path is valid, false otherwise.
127+
*/
128+
export function isValidChangenoteFileOrDir(filepath: string): boolean {
129+
try {
130+
const stats = fs.statSync(filepath);
131+
if (stats.isFile()) {
132+
return isValidChangenoteFile(filepath);
133+
}
134+
if (stats.isDirectory()) {
135+
return fs
136+
.readdirSync(filepath)
137+
.filter((f) => f !== ".gitkeep")
138+
.every((f) => isValidChangenoteFile(path.join(filepath, f)));
139+
}
140+
console.error(`${filepath}: not a file or directory`);
141+
return false;
142+
} catch (error) {
143+
console.error(`${filepath}: failed to read file or directory`, error);
144+
return false;
145+
}
146+
}

‎pr-checks/changelog/validate.test.mts‎

Lines changed: 52 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { withTmpFile } from "../../src/util";
66
import {
77
hasValidChangenoteCategory,
88
isValidChangenoteContent,
9-
isValidChangenoteFile,
9+
isValidChangenoteFileOrDir,
1010
isValidChangenoteFilename,
1111
VALID_CHANGE_NOTE_CATEGORIES,
1212
} from "./validate.mjs";
@@ -139,23 +139,55 @@ await describe("hasValidChangenoteCategory", async () => {
139139
});
140140
});
141141

142-
await describe("isValidChangenoteFile", async () => {
142+
await describe("isValidChangenoteFileOrDir", async () => {
143143
await it("accepts a valid change-note file", async () => {
144144
await withTmpFile(
145145
"2026-01-01-fix-bug.md",
146146
"---\ncategory: fix\n---\n- Fixed a bug\n",
147147
(filePath) => {
148-
assert.equal(isValidChangenoteFile(filePath), true);
148+
assert.equal(isValidChangenoteFileOrDir(filePath), true);
149149
},
150150
);
151151
});
152152

153+
await it("accepts a directory of valid change-note files", async () => {
154+
await withTmpDir(async (tmpDir) => {
155+
fs.writeFileSync(
156+
path.join(tmpDir, "2026-01-01-fix-bug.md"),
157+
"---\ncategory: fix\n---\n- Fixed a bug\n",
158+
);
159+
fs.writeFileSync(
160+
path.join(tmpDir, "2026-01-02-add-feature.md"),
161+
"---\ncategory: feature\n---\n- Added a feature\n",
162+
);
163+
assert.equal(isValidChangenoteFileOrDir(tmpDir), true);
164+
});
165+
});
166+
167+
await it("accepts an empty directory", async () => {
168+
await withTmpDir(async (tmpDir) => {
169+
assert.equal(isValidChangenoteFileOrDir(tmpDir), true);
170+
});
171+
});
172+
173+
await it("accepts a directory of .gitkeep only", async () => {
174+
await withTmpFile(".gitkeep", "", async (filePath) => {
175+
const dirPath = path.dirname(filePath);
176+
assert.equal(isValidChangenoteFileOrDir(dirPath), true);
177+
});
178+
});
179+
180+
await it("rejects a non-existent path", async () => {
181+
assert.equal(isValidChangenoteFileOrDir("non-existent-file.md"), false);
182+
assert.equal(isValidChangenoteFileOrDir("this-dir-does-not-exist"), false);
183+
});
184+
153185
await it("rejects invalid filename", async () => {
154186
await withTmpFile(
155187
"fix-bug.md",
156188
"---\ncategory: fix\n---\n- Fixed a bug\n",
157189
(filePath) => {
158-
assert.equal(isValidChangenoteFile(filePath), false);
190+
assert.equal(isValidChangenoteFileOrDir(filePath), false);
159191
},
160192
);
161193
});
@@ -165,7 +197,7 @@ await describe("isValidChangenoteFile", async () => {
165197
"2026-01-01-fix-bug.md",
166198
"- Fixed a bug\n",
167199
(filePath) => {
168-
assert.equal(isValidChangenoteFile(filePath), false);
200+
assert.equal(isValidChangenoteFileOrDir(filePath), false);
169201
},
170202
);
171203
});
@@ -175,8 +207,22 @@ await describe("isValidChangenoteFile", async () => {
175207
"2026-01-01-fix-bug.md",
176208
"---\ncategory: fix\n---\n* Fixed a bug\n",
177209
(filePath) => {
178-
assert.equal(isValidChangenoteFile(filePath), false);
210+
assert.equal(isValidChangenoteFileOrDir(filePath), false);
179211
},
180212
);
181213
});
214+
215+
await it("rejects directory with one invalid change-note file", async () => {
216+
await withTmpDir(async (tmpDir) => {
217+
fs.writeFileSync(
218+
path.join(tmpDir, "2026-01-01-fix-bug.md"),
219+
"---\ncategory: fix\n---\n- Fixed a bug\n",
220+
);
221+
fs.writeFileSync(
222+
path.join(tmpDir, "2026-01-02-wrong-category.md"),
223+
"---\ncategory: foobar\n---\n- Added a feature\n",
224+
);
225+
assert.equal(isValidChangenoteFileOrDir(tmpDir), false);
226+
});
227+
});
182228
});

‎pr-checks/changenotes.mts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import { pathToFileURL } from "node:url";
44
import { parseArgs } from "node:util";
55

6-
import { isValidChangenoteFile } from "./changelog/validate.mjs";
6+
import { isValidChangenoteFileOrDir } from "./changelog/validate.mjs";
77

88
const entryPoint = process.argv[1];
99
if (entryPoint && import.meta.url === pathToFileURL(entryPoint).href) {
@@ -45,7 +45,7 @@ function validate(paths: string[]): number {
4545
return 1;
4646
}
4747
for (const path of paths) {
48-
if (!isValidChangenoteFile(path)) {
48+
if (!isValidChangenoteFileOrDir(path)) {
4949
valid = false;
5050
}
5151
}

0 commit comments

Comments
 (0)