Skip to content

Commit ac5bbd7

Browse files
Changelog script fix (#5227)
1 parent da3b807 commit ac5bbd7

File tree

2 files changed

+18
-11
lines changed

2 files changed

+18
-11
lines changed

common/scripts/changelog/check.mjs

+13-6
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,22 @@ import { CHANGE_DIR_BETA, CHANGE_DIR_STABLE } from "./constants.mjs";
1616

1717
async function main() {
1818
const [base, head] = parseArgs(process.argv);
19-
const gitLogStdoutStableChangeFiles = await exec_output(`git log --name-status ${base}..${head} -- ${CHANGE_DIR_STABLE}`);
20-
const gitLogStdoutBetaChangeFiles = await exec_output(`git log --name-status ${base}..${head} -- ${CHANGE_DIR_BETA}`);
2119

22-
const newStableChangeFiles = parseNewChangeFiles(gitLogStdoutStableChangeFiles);
23-
const newBetaChangeFiles = parseNewChangeFiles(gitLogStdoutBetaChangeFiles);
24-
const newChangeFilesCount = (newStableChangeFiles?.length ?? 0) + (newBetaChangeFiles?.length ?? 0);
20+
// Check if the changelog files are present.
21+
// For this, check for new files (`git diff --diff-filter=A`) in the changelog directory(`-- ${CHANGE_DIR}`
22+
// or`-- ${CHANGE_DIR_BETA}`) and return only the file names and statuses(`--name-status`).
23+
// See https://git-scm.com/docs/git-diff for more info.
24+
const changedStableFiles = await exec_output(`git diff --diff-filter=A --name-status ${base}..${head} -- ${CHANGE_DIR_STABLE}`);
25+
const changedBetaFiles = await exec_output(`git diff --diff-filter=A --name-status ${base}..${head} -- ${CHANGE_DIR_BETA}`);
26+
// Process the output to get the list of files
27+
const stableFiles = parseNewChangeFiles(changedStableFiles);
28+
const betaFiles = parseNewChangeFiles(changedBetaFiles);
29+
const finalChangeFiles = [...stableFiles, ...betaFiles];
30+
31+
const newChangeFilesCount = finalChangeFiles.length;
2532

2633
if (newChangeFilesCount === 0) {
27-
console.error('No changefile detected! Please run `rush changelog` to document your change. Or if your changes do not affect the published packages in any way, please add `do not need changelog` label to the PR.');
34+
console.error('No changefile detected! Please run `rush changelog` to document your change. Or if your changes do not affect the published packages in any way, please add `does not need changelog` label to the PR.');
2835
process.exit(1);
2936
}
3037
console.log(`Found ${newChangeFilesCount} changefiles. All is good!`)

common/scripts/changelog/utils.mjs

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
// Copyright (c) Microsoft Corporation.
22
// Licensed under the MIT License.
33

4-
const NEW_CHANGE_FILE_REGEXP = /\s*change\/(.*\.json)\s*/;
5-
64
export function parseNewChangeFiles(stdout) {
5+
// Split the output into lines
76
const lines = stdout.split('\n');
8-
const matches = lines.map(line => line.match(NEW_CHANGE_FILE_REGEXP)).filter(match => !!match);
9-
// Extract the first capture group.
10-
return matches.map(match => match[1]);
7+
// Filter out the empty lines (`lines` list might have empty lines as part of the output)
8+
const matches = lines.filter(line => line && line.toString().trim().length > 0);
9+
// Parse the change file name from the output (remove the status and return only file information)
10+
return matches.map(line => line.split('\t')[1]);
1111
}

0 commit comments

Comments
 (0)