|
1 | 1 | #!/usr/bin/env npx tsx |
2 | 2 |
|
3 | 3 | /** |
4 | | - * Updates the changelog with a change note for an updated CodeQL CLI bundle. |
| 4 | + * Creates a changenote file for an update to the default CodeQL CLI bundle. |
5 | 5 | */ |
6 | 6 |
|
7 | 7 | import * as fs from "node:fs"; |
| 8 | +import * as path from "node:path"; |
8 | 9 |
|
9 | | -import { |
10 | | - parseChangelog, |
11 | | - renderChangelog, |
12 | | - UNRELEASED_PLACEHOLDER, |
13 | | -} from "./changelog"; |
14 | | -import { CHANGELOG_FILE, CLI_BUNDLE_RELEASE_URL_PREFIX } from "./config"; |
| 10 | +import { CHANGENOTES_DIR, CLI_BUNDLE_RELEASE_URL_PREFIX } from "./config"; |
15 | 11 | import { getErrorMessage } from "./util"; |
16 | 12 |
|
17 | 13 | export const CLI_VERSION_ENV_VAR = "CLI_VERSION"; |
@@ -58,65 +54,39 @@ export function getPRNumber(prUrl: string) { |
58 | 54 | } |
59 | 55 |
|
60 | 56 | /** |
61 | | - * Updates `changelog` by adding `changelogNote` to the first section. |
| 57 | + * Returns the contents of the changenote file for an update to the default |
| 58 | + * CodeQL CLI bundle. |
62 | 59 | * |
63 | | - * @param contents The existing changelog contents. |
64 | | - * @param changelogNote The note to add to the first section. |
| 60 | + * @param cliVersion The CodeQL CLI version of the new default bundle. |
| 61 | + * @param prUrl The URL of the PR that updates the default bundle. |
| 62 | + * @throws If the PR number can't be determined from `prUrl`. |
65 | 63 | */ |
66 | | -export function updateChangelog(contents: string, changelogNote: string) { |
67 | | - // If the "[UNRELEASED]" section starts with "no user facing changes", remove that line. |
68 | | - contents = contents.replace( |
69 | | - `## ${UNRELEASED_PLACEHOLDER}\n\nNo user facing changes.`, |
70 | | - `## ${UNRELEASED_PLACEHOLDER}\n`, |
71 | | - ); |
| 64 | +export function getChangenote(cliVersion: string, prUrl: string): string { |
| 65 | + // The GitHub Release for the new bundle version. |
| 66 | + const bundleReleaseUrl = `${CLI_BUNDLE_RELEASE_URL_PREFIX}${cliVersion}`; |
72 | 67 |
|
73 | | - const changelog = parseChangelog(contents); |
74 | | - |
75 | | - if (changelog.sections.length === 0) { |
76 | | - throw new Error("The changelog contains no existing sections."); |
77 | | - } |
78 | | - |
79 | | - // Add the changelog note to the bottom of the first section. |
80 | | - const firstSection = changelog.sections[0]; |
81 | | - const lastLine = firstSection.bodyLines.pop(); |
82 | | - |
83 | | - if (lastLine !== undefined && lastLine.trim() !== "") { |
84 | | - // We expect the last line to be empty. If it isn't for some reason, |
85 | | - // add it back. |
86 | | - firstSection.bodyLines.push(lastLine); |
87 | | - } |
88 | | - |
89 | | - firstSection.bodyLines.push(changelogNote); |
90 | | - |
91 | | - // If the last line is empty as expected, then add it back in after the new note. |
92 | | - if (lastLine?.trim() === "") { |
93 | | - firstSection.bodyLines.push(lastLine); |
94 | | - } |
95 | | - |
96 | | - return renderChangelog(changelog); |
| 68 | + const prNumber = getPRNumber(prUrl); |
| 69 | + return `- Update default CodeQL bundle version to [${cliVersion}](${bundleReleaseUrl}). [#${prNumber}](${prUrl})\n`; |
97 | 70 | } |
98 | 71 |
|
99 | 72 | function main() { |
100 | 73 | try { |
101 | 74 | const cliVersion = getCLIVersion(); |
102 | 75 | const prUrl = getPRUrl(); |
103 | 76 |
|
104 | | - // The GitHub Release for the new bundle version. |
105 | | - const bundleReleaseUrl = `${CLI_BUNDLE_RELEASE_URL_PREFIX}${cliVersion}`; |
106 | | - |
107 | | - // Get the PR number from the PR URL. |
108 | | - const prNumber = getPRNumber(prUrl); |
109 | | - const changelogNote = `- Update default CodeQL bundle version to [${cliVersion}](${bundleReleaseUrl}). [#${prNumber}](${prUrl})`; |
110 | | - |
111 | | - let changelog = fs.readFileSync(CHANGELOG_FILE, "utf-8"); |
112 | | - |
113 | | - changelog = updateChangelog(changelog, changelogNote); |
114 | | - |
115 | | - fs.writeFileSync(CHANGELOG_FILE, changelog); |
| 77 | + // Include the CLI version in the file name so that changenotes for |
| 78 | + // different bundle updates don't conflict. |
| 79 | + const changenotePath = path.join( |
| 80 | + CHANGENOTES_DIR, |
| 81 | + `update-bundle-${cliVersion}.md`, |
| 82 | + ); |
| 83 | + fs.writeFileSync(changenotePath, getChangenote(cliVersion, prUrl)); |
116 | 84 |
|
117 | 85 | return 0; |
118 | 86 | } catch (err) { |
119 | | - console.error(`Failed to bundle changelog: ${getErrorMessage(err)}`); |
| 87 | + console.error( |
| 88 | + `Failed to create changenote for bundle update: ${getErrorMessage(err)}`, |
| 89 | + ); |
120 | 90 | return -1; |
121 | 91 | } |
122 | 92 | } |
|
0 commit comments