mirror of
https://github.com/github/codeql-action.git
synced 2026-10-03 09:14:58 +00:00
Simplify getHeader to operate on ChangelogSections
This commit is contained in:
@@ -26,20 +26,26 @@ import { CHANGELOG_FILE } from "./config";
|
||||
const testDate = new Date(2026, 7, 14);
|
||||
|
||||
describe("getHeader", async () => {
|
||||
function Section(headerLine: string): ChangelogSection {
|
||||
return {
|
||||
headerLine,
|
||||
bodyLines: [],
|
||||
};
|
||||
}
|
||||
await it("returns non-headers unchanged", () => {
|
||||
assert.equal("foo", getHeader("foo"));
|
||||
assert.equal("- bar", getHeader("- bar"));
|
||||
assert.equal("foo", getHeader(Section("foo")));
|
||||
assert.equal("- bar", getHeader(Section("- bar")));
|
||||
});
|
||||
await it("strips octothorpes", async () => {
|
||||
assert.equal("foo", getHeader("# foo"));
|
||||
assert.equal("foo", getHeader("## foo"));
|
||||
assert.equal("foo", getHeader("### foo"));
|
||||
assert.equal("foo", getHeader("#### foo"));
|
||||
assert.equal("foo", getHeader("##### foo"));
|
||||
assert.equal("foo", getHeader("###### foo"));
|
||||
assert.equal("foo", getHeader(Section("# foo")));
|
||||
assert.equal("foo", getHeader(Section("## foo")));
|
||||
assert.equal("foo", getHeader(Section("### foo")));
|
||||
assert.equal("foo", getHeader(Section("#### foo")));
|
||||
assert.equal("foo", getHeader(Section("##### foo")));
|
||||
assert.equal("foo", getHeader(Section("###### foo")));
|
||||
});
|
||||
await it("strips whitespace", async () => {
|
||||
assert.equal("foo", getHeader("# foo "));
|
||||
assert.equal("foo", getHeader(Section("# foo ")));
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
@@ -33,9 +33,11 @@ export interface Changelog {
|
||||
sections: ChangelogSection[];
|
||||
}
|
||||
|
||||
/** Returns the text of a CHANGELOG.md header (without the '## ' prefix). */
|
||||
export function getHeader(headerLine: string): string {
|
||||
return headerLine.replace(/^#+\s+/, "").trimEnd();
|
||||
/**
|
||||
* Returns the text of the header (without the '## ' prefix) of the given section.
|
||||
* */
|
||||
export function getHeader(section: ChangelogSection): string {
|
||||
return section.headerLine.replace(/^#+\s+/, "").trimEnd();
|
||||
}
|
||||
|
||||
/** Returns `date` formatted as `DD Mon YYYY`. */
|
||||
@@ -147,7 +149,7 @@ export function addBodyLinesToUnreleasedSection(
|
||||
lines: string[],
|
||||
) {
|
||||
const unreleasedSection = changelog.sections[0];
|
||||
if (getHeader(unreleasedSection.headerLine) !== UNRELEASED_PLACEHOLDER) {
|
||||
if (getHeader(unreleasedSection) !== UNRELEASED_PLACEHOLDER) {
|
||||
throw Error("'[UNRELEASED]' is not the first section of 'CHANGELOG.md'");
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user