Skip to content

Commit aeef68f

Browse files
committed
Add bundle-changelog.ts with tests
1 parent 9b8dc48 commit aeef68f

3 files changed

Lines changed: 270 additions & 0 deletions

File tree

‎pr-checks/bundle-changelog.test.ts‎

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
/**
2+
* Tests for `bundle-changelog.ts`.
3+
*/
4+
5+
import * as assert from "node:assert/strict";
6+
import * as fs from "node:fs";
7+
import * as os from "node:os";
8+
import * as path from "node:path";
9+
import { afterEach, beforeEach, describe, it } from "node:test";
10+
11+
import {
12+
CLI_VERSION_ENV_VAR,
13+
getCLIVersion,
14+
getPRNumber,
15+
getPRUrl,
16+
PR_URL_ENV_VAR,
17+
updateChangelog,
18+
} from "./bundle-changelog";
19+
import {
20+
EMPTY_CHANGELOG,
21+
NO_CHANGES_STR,
22+
UNRELEASED_PLACEHOLDER,
23+
} from "./changelog";
24+
25+
let testDir: string;
26+
27+
beforeEach(() => {
28+
// Set up a temporary directory for testing
29+
testDir = fs.mkdtempSync(path.join(os.tmpdir(), "bundle-changelog-test-"));
30+
});
31+
32+
afterEach(() => {
33+
/** Clean up temporary directories. */
34+
fs.rmSync(testDir, { recursive: true, force: true });
35+
});
36+
37+
describe("getCLIVersion", async () => {
38+
await it("throws if the environment variable is not set", async () => {
39+
delete process.env[CLI_VERSION_ENV_VAR];
40+
assert.throws(() => getCLIVersion());
41+
});
42+
43+
await it("throws if the environment variable is empty", async () => {
44+
process.env[CLI_VERSION_ENV_VAR] = " ";
45+
assert.throws(() => getCLIVersion());
46+
});
47+
48+
await it("returns value of the environment variable if set", async () => {
49+
const testValue = "1.2.3";
50+
process.env[CLI_VERSION_ENV_VAR] = testValue;
51+
assert.deepEqual(getCLIVersion(), testValue);
52+
});
53+
});
54+
55+
const testPrUrl = "https://github.com/github/codeql-action/pulls/42";
56+
57+
describe("getPRUrl", async () => {
58+
await it("throws if the environment variable is not set", async () => {
59+
delete process.env[PR_URL_ENV_VAR];
60+
assert.throws(() => getPRUrl());
61+
});
62+
63+
await it("throws if the environment variable is empty", async () => {
64+
process.env[PR_URL_ENV_VAR] = " ";
65+
assert.throws(() => getPRUrl());
66+
});
67+
68+
await it("returns value of the environment variable if set", async () => {
69+
process.env[PR_URL_ENV_VAR] = testPrUrl;
70+
assert.deepEqual(getPRUrl(), testPrUrl);
71+
});
72+
});
73+
74+
describe("getPRNumber", async () => {
75+
await it("throws if the last part of the input is not a number", async () => {
76+
assert.throws(() => getPRNumber(`${testPrUrl}/foo`));
77+
});
78+
79+
await it("throws if the last part of the input is not a positive number", async () => {
80+
assert.throws(() => getPRNumber(`${testPrUrl}/-100`));
81+
});
82+
83+
await it("returns the PR number from an URL", async () => {
84+
assert.equal(getPRNumber(testPrUrl), 42);
85+
});
86+
});
87+
88+
const testChangelog = `${EMPTY_CHANGELOG.trimEnd()}
89+
90+
## 4.23.7
91+
92+
- Other change
93+
94+
## 4.23.6
95+
96+
${NO_CHANGES_STR}`;
97+
98+
const expectedChangelog = `# CodeQL Action Changelog
99+
100+
## ${UNRELEASED_PLACEHOLDER}
101+
102+
- Update default CodeQL bundle version to
103+
104+
## 4.23.7
105+
106+
- Other change
107+
108+
## 4.23.6
109+
110+
${NO_CHANGES_STR}`;
111+
112+
describe("updateChangelog", async () => {
113+
await it("removes `NO_CHANGES_STR` if present in [UNRELEASED] section", async () => {
114+
const result = updateChangelog(EMPTY_CHANGELOG, "");
115+
assert.ok(!result.includes(NO_CHANGES_STR.trim()));
116+
});
117+
118+
await it("doesn't remove `NO_CHANGES_STR` if present in versioned section", async () => {
119+
const result = updateChangelog(
120+
EMPTY_CHANGELOG.replace(UNRELEASED_PLACEHOLDER, "1.2.3"),
121+
"",
122+
);
123+
assert.ok(result.includes(NO_CHANGES_STR.trim()));
124+
});
125+
126+
await it("throws if there are no sections", async () => {
127+
assert.throws(() => {
128+
updateChangelog(
129+
"# CodeQL Action Changelog",
130+
"- Update default CodeQL bundle version to",
131+
);
132+
});
133+
});
134+
135+
await it("adds note at the end of the first section", async () => {
136+
const result = updateChangelog(
137+
testChangelog,
138+
"- Update default CodeQL bundle version to",
139+
);
140+
assert.deepEqual(result, expectedChangelog);
141+
});
142+
});

‎pr-checks/bundle-changelog.ts‎

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
#!/usr/bin/env npx tsx
2+
3+
/**
4+
* Updates the changelog with a change note for an updated CodeQL CLI bundle.
5+
*/
6+
7+
import * as fs from "node:fs";
8+
9+
import { getErrorMessage } from "../src/util";
10+
11+
import { CHANGELOG_FILE, CLI_BUNDLE_RELEASE_URL_PREFIX } from "./config";
12+
import { NO_CHANGES_STR, parseChangelog, renderChangelog, UNRELEASED_PLACEHOLDER } from "./changelog";
13+
14+
export const CLI_VERSION_ENV_VAR = "CLI_VERSION";
15+
export const PR_URL_ENV_VAR = "PR_URL";
16+
17+
/** Gets the CLI version from the environment. */
18+
export function getCLIVersion() {
19+
const cliVersion = process.env[CLI_VERSION_ENV_VAR];
20+
21+
if (cliVersion === undefined || cliVersion.trim() === "") {
22+
throw new Error(`No CLI version was set in '${CLI_VERSION_ENV_VAR}'.`);
23+
}
24+
25+
return cliVersion;
26+
}
27+
28+
/** Gets the PR URL from the environment. */
29+
export function getPRUrl() {
30+
const prUrl = process.env[PR_URL_ENV_VAR];
31+
32+
if (prUrl === undefined || prUrl.trim() === "") {
33+
throw new Error(`No PR URL was set in '${PR_URL_ENV_VAR}'.`);
34+
}
35+
36+
return prUrl;
37+
}
38+
39+
/**
40+
* Gets the PR number from something like a PR URL.
41+
*/
42+
export function getPRNumber(prUrl: string) {
43+
const prUrlParts = prUrl.split("/");
44+
const prNumberStr = prUrlParts[prUrlParts.length - 1];
45+
46+
const prNumber = Number.parseInt(prNumberStr, 10);
47+
48+
if (!Number.isInteger(prNumber) || prNumber <= 0) {
49+
throw new Error(
50+
`Invalid PR URL '${prUrl}': last part is not a positive number`,
51+
);
52+
}
53+
54+
return prNumber;
55+
}
56+
57+
/**
58+
* Updates `changelog` by adding `changelogNote` to the first section.
59+
*
60+
* @param contents The existing changelog contents.
61+
* @param changelogNote The note to add to the first section.
62+
*/
63+
export function updateChangelog(contents: string, changelogNote: string) {
64+
// If the "[UNRELEASED]" section starts with "no user facing changes", remove that line.
65+
contents = contents.replace(
66+
`## ${UNRELEASED_PLACEHOLDER}\n\nNo user facing changes.`,
67+
`## ${UNRELEASED_PLACEHOLDER}\n`,
68+
);
69+
70+
const changelog = parseChangelog(contents);
71+
72+
if (changelog.sections.length === 0) {
73+
throw new Error("The changelog contains no existing sections.");
74+
}
75+
76+
// Add the changelog note to the bottom of the first section.
77+
const firstSection = changelog.sections[0];
78+
const lastLine = firstSection.bodyLines.pop();
79+
80+
if (lastLine !== undefined && lastLine.trim() != "") {
81+
// We expect the last line to be empty. If it isn't for some reason,
82+
// add it back.
83+
firstSection.bodyLines.push(lastLine);
84+
}
85+
86+
firstSection.bodyLines.push(changelogNote);
87+
88+
// If the last line is empty as expected, then add it back in after the new note.
89+
if (lastLine !== undefined && lastLine.trim() == "") {
90+
firstSection.bodyLines.push(lastLine);
91+
}
92+
93+
return renderChangelog(changelog);
94+
}
95+
96+
function main() {
97+
try {
98+
const cliVersion = getCLIVersion();
99+
const prUrl = getPRUrl();
100+
101+
// The GitHub Release for the new bundle version.
102+
const bundleReleaseUrl = `${CLI_BUNDLE_RELEASE_URL_PREFIX}${cliVersion}`;
103+
104+
// Get the PR number from the PR URL.
105+
const prNumber = getPRNumber(prUrl);
106+
const changelogNote = `- Update default CodeQL bundle version to [${cliVersion}](${bundleReleaseUrl}). [#${prNumber}](${prUrl})`;
107+
108+
let changelog = fs.readFileSync(CHANGELOG_FILE, "utf-8");
109+
110+
changelog = updateChangelog(changelog, changelogNote);
111+
112+
fs.writeFileSync(CHANGELOG_FILE, changelog);
113+
114+
return 0;
115+
} catch (err) {
116+
console.error(`Failed to bundle changelog: ${getErrorMessage(err)}`);
117+
return -1;
118+
}
119+
}
120+
121+
// Only call `main` if this script was run directly.
122+
if (require.main === module) {
123+
process.exit(main());
124+
}

‎pr-checks/config.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ export const API_COMPATIBILITY_FILE = path.join(
3737
"api-compatibility.json",
3838
);
3939

40+
/** The prefix of CodeQL CLI bundle release URLs. */
41+
export const CLI_BUNDLE_RELEASE_URL_PREFIX =
42+
"https://github.com/github/codeql-action/releases/tag/codeql-bundle-v";
43+
4044
/** A common interface for operations that support dry runs. */
4145
export interface DryRunOption {
4246
/** A value indicating whether to perform operations with side effects. */

0 commit comments

Comments
 (0)