Skip to content

Commit

Permalink
add validation for dont-land labels
Browse files Browse the repository at this point in the history
  • Loading branch information
aduh95 committed Dec 19, 2024
1 parent eae03e3 commit e8bb005
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 11 deletions.
14 changes: 10 additions & 4 deletions .github/workflows/lint-release-proposal.yml
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,15 @@ jobs:
gh api \
-H "Accept: application/vnd.github+json" \
-H "X-GitHub-Api-Version: 2022-11-28" \
--jq '.commits | map({ smallSha: .sha[0:10], splitTitle: .commit.message|split("\n\n")|first|split(":") })' \
"/repos/${GITHUB_REPOSITORY}/compare/v${MAJOR}.x...$GITHUB_SHA" --paginate |\
jq -r '.[] | "* [[`" + .smallSha + "`](" + env.GITHUB_SERVER_URL + "/" + env.GITHUB_REPOSITORY + "/commit/" + .smallSha + ")] - **" + (.splitTitle|first) + "**:" + (.splitTitle[1:]|join(":"))' |\
node tools/actions/lint-release-proposal-commit-list.mjs "$CHANGELOG_PATH" "$GITHUB_SHA"
--jq '.commits.[] | { smallSha: .sha[0:10], splitTitle: .commit.message|split("\n\n")|first|split(":") } + (.commit.message|capture("\nPR-URL: (?<prURL>.+)\n"))' \
"/repos/${GITHUB_REPOSITORY}/compare/v${MAJOR}.x...$GITHUB_SHA" --paginate \
| node tools/actions/lint-release-proposal-commit-list.mjs "$CHANGELOG_PATH" "$GITHUB_SHA" \
| while IFS= read -r PR_URL; do
LABEL="dont-land-on-v${MAJOR}.x" gh pr view \
--json labels,url \
--jq 'if (.labels|map(.name==env.LABEL)|any) then error("\(.url) has the \(env.LABEL) label, forbidding it to be in this release proposal") end' \
"$PR_URL" > /dev/null
done
shell: bash # See https://docs.github.com/en/actions/writing-workflows/workflow-syntax-for-github-actions#exit-codes-and-error-action-preference, we want the pipefail option

Check warning on line 73 in .github/workflows/lint-release-proposal.yml

View workflow job for this annotation

GitHub Actions / lint-yaml

73:21 [comments] too few spaces before comment
env:
GH_TOKEN: ${{ github.token }}
39 changes: 32 additions & 7 deletions tools/actions/lint-release-proposal-commit-list.mjs
Original file line number Diff line number Diff line change
@@ -1,36 +1,61 @@
#!/usr/bin/env node

// Takes a stream of JSON objects as inputs, validates the CHANGELOG contains a
// line corresponding, then outputs the prURL value.
//
// $ ./lint-release-proposal-commit-list.mjs "path/to/CHANGELOG.md" "deadbeef00" <<'EOF'
// {"prURL":"https://github.com/nodejs/node/pull/56131","smallSha":"d48b5224c0","splitTitle":["doc"," fix module.md headings"]}
// {"prURL":"https://github.com/nodejs/node/pull/56123","smallSha":"f1c2d2f65e","splitTitle":["doc"," update blog release-post link"]}
// EOF

const [,, CHANGELOG_PATH, RELEASE_COMMIT_SHA] = process.argv;

import assert from 'node:assert';
import { readFile } from 'node:fs/promises';
import { createInterface } from 'node:readline';

// Creating the iterator early to avoid missing any data
// Creating the iterator early to avoid missing any data:
const stdinLineByLine = createInterface(process.stdin)[Symbol.asyncIterator]();

const changelog = await readFile(CHANGELOG_PATH, 'utf-8');
const startCommitListing = changelog.indexOf('\n### Commits\n');
const commitList = changelog.slice(startCommitListing, changelog.indexOf('\n\n<a', startCommitListing))
const commitListingStart = changelog.indexOf('\n### Commits\n');
const commitListingEnd = changelog.indexOf('\n\n<a', commitListingStart);
const commitList = changelog.slice(commitListingStart, commitListingEnd === -1 ? undefined : commitListingEnd + 1)
// Checking for semverness is too expansive, it is left as a exercice for human reviewers.
.replaceAll('**(SEMVER-MINOR)** ', '')
// Correct Markdown escaping is validated by the linter, getting rid of it here helps.
.replaceAll('\\', '');

let expectedNumberOfCommitsLeft = commitList.match(/\n\* \[/g).length;
for await (const line of stdinLineByLine) {
if (line.includes(RELEASE_COMMIT_SHA.slice(0, 10))) {
const { smallSha, splitTitle, prURL } = JSON.parse(line);

if (smallSha === RELEASE_COMMIT_SHA.slice(0, 10)) {
assert.strictEqual(
expectedNumberOfCommitsLeft, 0,
'Some commits are listed without being included in the proposal, or are listed more than once',
);
continue;
}

// Revert commit have a special treatment.
const fixedLine = line.replace(' - **Revert \"', ' - _**Revert**_ "**');
const lineStart = commitList.indexOf(`\n* \[[\`${smallSha}\`]`);

Check failure on line 41 in tools/actions/lint-release-proposal-commit-list.mjs

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Unnecessary escape character: \[
assert.notStrictEqual(lineStart, -1, `Cannot find ${smallSha} on the list`);
const lineEnd = commitList.indexOf('\n', lineStart + 1);

const expectedCommitTitle = `${`**${splitTitle.shift()}`.replace('**Revert \"', '_**Revert**_ "**')}**:${splitTitle.join(':')}`;

Check failure on line 45 in tools/actions/lint-release-proposal-commit-list.mjs

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Unnecessary escape character: \"
try {
assert(commitList.lastIndexOf(`/${smallSha})] - ${expectedCommitTitle} (`, lineEnd) > lineStart, `Commit title doesn't match`);
} catch (e) {
if (e?.code === 'ERR_ASSERTION') {
e.operator = 'includes';
e.expected = expectedCommitTitle;
e.actual = commitList.slice(lineStart + 1, lineEnd);
}
throw e;
}
assert.strictEqual(commitList.slice(lineEnd - prURL.length - 2, lineEnd), `(${prURL})`);

assert(commitList.includes('\n' + fixedLine), `Missing "${fixedLine}" in commit list`);
expectedNumberOfCommitsLeft--;
console.log(prURL);
}
assert.strictEqual(expectedNumberOfCommitsLeft, 0, 'Release commit is not the last commit in the proposal');

0 comments on commit e8bb005

Please sign in to comment.