Skip to content

Commit 037fe40

Browse files
committed
Create workflow for PR draft nudge commenter
1 parent 319e90c commit 037fe40

3 files changed

Lines changed: 88 additions & 0 deletions

File tree

.github/scripts/pr-draft-nudge.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
module.exports = async ({ github, context, core }) => {
2+
try {
3+
if (process.env.PR_DRAFT_NUDGE_COMMENT_DISABLE === "true") {
4+
core.info("PR draft nudge comment is disabled for this repository.");
5+
return;
6+
}
7+
8+
const pr = context.payload.pull_request;
9+
10+
if (!pr) {
11+
core.info("This action is only applicable to pull requests.");
12+
return;
13+
}
14+
15+
// We only want to comment on PRs that are opened ready for review with reviewers.
16+
if (
17+
context.payload.action !== "opened" ||
18+
pr.draft ||
19+
!pr.requested_reviewers ||
20+
pr.requested_reviewers.length === 0
21+
) {
22+
core.info(
23+
'PR is a draft, has no reviewers, or this is not an "opened" event. Skipping.'
24+
);
25+
return;
26+
}
27+
28+
const prCreator = pr.user.login;
29+
const commentIdentifier = "<!-- pr-draft-nudge-comment -->";
30+
const commentBody = `:wave: @${prCreator}, thanks for creating this pull request!\n\nTo help reviewers, please consider creating future PRs as drafts first. This allows you to self-review and make any final changes before notifying the team.\n\nOnce you're ready, you can mark it as "Ready for review" to request feedback. Thanks!\n\n${commentIdentifier}`;
31+
32+
// Check if a comment with the identifier already exists
33+
const { data: comments } = await github.rest.issues.listComments({
34+
owner: context.repo.owner,
35+
repo: context.repo.repo,
36+
issue_number: pr.number,
37+
});
38+
39+
const existingComment = comments.find((comment) =>
40+
comment.body.includes(commentIdentifier)
41+
);
42+
43+
if (existingComment) {
44+
core.info("A PR draft nudge comment already exists on this PR.");
45+
return;
46+
}
47+
48+
// Create the comment
49+
await github.rest.issues.createComment({
50+
owner: context.repo.owner,
51+
repo: context.repo.repo,
52+
issue_number: pr.number,
53+
body: commentBody,
54+
});
55+
56+
core.info("Successfully posted PR draft nudge comment.");
57+
} catch (error) {
58+
core.setFailed(`Action failed with error: ${error.message}`);
59+
}
60+
};

.github/workflows/dependency-review-vulnerability-license.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ jobs:
2525
name: License and Vulnerabilities
2626
permissions:
2727
contents: read
28+
pull-requests: write
2829
runs-on: ubuntu-latest
2930
# Skip on merge group events
3031
if: ${{ github.event_name == 'pull_request' }}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
name: PR Draft Nudge
2+
3+
on:
4+
pull_request:
5+
types: [opened]
6+
7+
permissions: {}
8+
9+
jobs:
10+
pr-draft-nudge:
11+
name: PR Draft Nudge
12+
runs-on: ubuntu-latest
13+
timeout-minutes: 10
14+
permissions:
15+
contents: read
16+
pull-requests: write
17+
steps:
18+
- uses: actions/checkout@v4
19+
20+
- name: Run
21+
uses: actions/github-script@v7
22+
env:
23+
PR_DRAFT_NUDGE_COMMENT_DISABLE: ${{ vars.PR_DRAFT_NUDGE_COMMENT_DISABLE || 'false' }}
24+
with:
25+
script: |
26+
const script = require('./.github/scripts/pr-draft-nudge.js');
27+
await script({ github, context, core });

0 commit comments

Comments
 (0)