mirror of
https://github.com/github/codeql-action.git
synced 2026-10-03 09:14:58 +00:00
Refactor changenotes validate to use helper getChangenotes
This reduces duplicate code between `assemble` and `validate`. It also has the benefit of fixing a bug in the current implementation of `validate`, where `isValidChangenoteFile` receives a relative file name where it should receive an absolute one.
This commit is contained in:
@@ -119,14 +119,3 @@ export function isValidChangenoteFile(filename: string): boolean {
|
|||||||
|
|
||||||
return isValid;
|
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);
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -1,13 +1,10 @@
|
|||||||
import assert from "node:assert/strict";
|
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 { describe, it } from "node:test";
|
||||||
|
|
||||||
import { withTmpDir, withTmpFile } from "../../src/util";
|
import { withTmpFile } from "../../src/util";
|
||||||
|
|
||||||
import {
|
import {
|
||||||
hasValidChangenoteCategory,
|
hasValidChangenoteCategory,
|
||||||
isValidAllChangenoteFiles,
|
|
||||||
isValidChangenoteContent,
|
isValidChangenoteContent,
|
||||||
isValidChangenoteFile,
|
isValidChangenoteFile,
|
||||||
isValidChangenoteFilename,
|
isValidChangenoteFilename,
|
||||||
@@ -187,39 +184,3 @@ 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);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ import {
|
|||||||
renderChangelog,
|
renderChangelog,
|
||||||
withChangelog,
|
withChangelog,
|
||||||
} from "./changelog";
|
} from "./changelog";
|
||||||
import { isValidAllChangenoteFiles } from "./changelog/validate.mjs";
|
import { isValidChangenoteFile } from "./changelog/validate.mjs";
|
||||||
import { CHANGENOTES_DIR } from "./config";
|
import { CHANGENOTES_DIR } from "./config";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -116,7 +116,11 @@ function assemble(): ExitCode {
|
|||||||
|
|
||||||
function validate(): ExitCode {
|
function validate(): ExitCode {
|
||||||
try {
|
try {
|
||||||
if (isValidAllChangenoteFiles(fs.readdirSync(CHANGENOTES_DIR))) {
|
const allChangenotesValid = getChangenotes().reduce(
|
||||||
|
(r, changenote) => r && isValidChangenoteFile(changenote.absolutePath),
|
||||||
|
true,
|
||||||
|
);
|
||||||
|
if (allChangenotesValid) {
|
||||||
console.log(`All changenotes in '${CHANGENOTES_DIR}' are valid.`);
|
console.log(`All changenotes in '${CHANGENOTES_DIR}' are valid.`);
|
||||||
return ExitCode.Success;
|
return ExitCode.Success;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user