Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions .github/workflows/linters.yml
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,25 @@ jobs:
sparse-checkout-cone-mode: false
# GH Actions squashes all PR commits, HEAD^ refers to the base branch.
- run: git diff HEAD^ HEAD -G"pr-url:" -- "*.md" | ./tools/lint-pr-url.mjs ${{ github.event.pull_request.html_url }}
suggest-fill-prurl:
needs: lint-pr-url
if: ${{ needs.lint-pr-url.result == 'failure' }}
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@1af3b93b6815bc44a9784bd300feb67ff0d1eeb3 # v6.0.0
with:
persist-credentials: false
sparse-checkout: /tools/actions/suggest-fill-prurl.mjs
sparse-checkout-cone-mode: false
- uses: actions/setup-node@2028fbc5c25fe9cf00d9f06a71cc4710d4507903 # v6.0.0
with:
node-version: latest
- run: node tools/actions/suggest-fill-prurl.mjs
env:
GH_TOKEN: ${{ secrets.GH_USER_TOKEN }
PR_NUMBER: ${{ github.event.number }}
REPO: ${{ github.repository }}
SHA: ${{ github.sha }}
lint-readme:
runs-on: ubuntu-latest
steps:
Expand Down
3 changes: 3 additions & 0 deletions doc/api/buffer.md
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,9 @@ the characters.

<!-- YAML
changes:
- version: REPLACEME
pr-url: https://github.com/LiviaMedeiros/node/pull/FILLME
description: dummy change
- version: v3.0.0
pr-url: https://github.com/nodejs/node/pull/2002
description: The `Buffer` class now inherits from `Uint8Array`.
Expand Down
9 changes: 9 additions & 0 deletions doc/api/fs.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,15 @@ try {
<!-- YAML
added: v10.0.0
changes:
- version: REPLACEME
pr-url: https://github.com/LiviaMedeiros/node/pull/FILLME
description: dummy change
- version: v14.0.0
pr-url: https://github.com/nodejs/node/pull/31553
description: Exposed as `require('fs/promises')`.
- version: REPLACEME
pr-url: https://github.com/LiviaMedeiros/node/pull/FILLME
description: dummy change
- version:
- v11.14.0
- v10.17.0
Expand Down Expand Up @@ -185,6 +191,9 @@ changes:
- v20.10.0
pr-url: https://github.com/nodejs/node/pull/50095
description: The `flush` option is now supported.
- version: REPLACEME
pr-url: https://github.com/LiviaMedeiros/node/pull/FILLME
description: dummy change
- version:
- v15.14.0
- v14.18.0
Expand Down
76 changes: 76 additions & 0 deletions tools/actions/suggest-fill-prurl.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#!/usr/bin/env node

// Replaces:
// pr-url: https://github.com/nodejs/node/pull/FILLME
// With:
// pr-url: https://github.com/nodejs/node/pull/ACTUAL_PR_NUMBER
// And posts it as ```suggestion``` on pull request on GitHub

import { env } from 'node:process';

const { GH_TOKEN, PR_NUMBER, REPO, SHA } = env;
if (!GH_TOKEN || !PR_NUMBER || !REPO || !SHA) {
throw new Error('Missing required environment variables');
}

const PLACEHOLDER = 'FILLME';
const placeholderReg = new RegExp(`^\\+.*${RegExp.escape(`https://github.com/${REPO}/pull/${PLACEHOLDER}`)}`);

const headers = new Headers({
'Accept': 'application/vnd.github+json',
'Authorization': `Bearer ${GH_TOKEN}`,
'User-Agent': 'nodejs-bot',
'X-GitHub-Api-Version': '2022-11-28',
});

// https://docs.github.com/en/rest/pulls/pulls?apiVersion=2022-11-28#list-pull-requests-files
const res = await fetch(
new URL(`repos/${REPO}/pulls/${PR_NUMBER}/files`, 'https://api.github.com'),
{ headers },
);
if (!res.ok) {
throw new Error(`Failed to fetch PR files, status=${res.status}`);
}

const files = await res.json();

const comments = files.flatMap(({ status, filename, patch }) => {
if (!patch || !['added', 'modified'].includes(status)) {
return [];
}

return patch.split('\n').map((line, position) => {
if (!placeholderReg.test(line)) {
return false;
}
const suggestion = line
.slice(1)
.replace(`https://github.com/${REPO}/pull/${PLACEHOLDER}`, `https://github.com/${REPO}/pull/${PR_NUMBER}`);
return {
path: filename,
position,
body: `Replace ${PLACEHOLDER} with PR number ${PR_NUMBER}\n` +
'```suggestion\n' +
`${suggestion}\n` +
'```\n',
};
}).filter(Boolean);
});

if (comments.length) {
const payload = {
comments,
commit_id: SHA,
event: 'COMMENT',
};

// https://docs.github.com/en/rest/pulls/reviews?apiVersion=2022-11-28#create-a-review-for-a-pull-request
await fetch(
new URL(`repos/${REPO}/pulls/${PR_NUMBER}/reviews`, 'https://api.github.com'),
{
method: 'POST',
headers,
body: JSON.stringify(payload),
},
);
}
Loading