Skip to content

Commit 791a7d7

Browse files
authored
Create workflow for PR draft nudge commenter (#5)
1 parent 319e90c commit 791a7d7

3 files changed

Lines changed: 97 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: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
###
2+
# This workflow nudges users to mark their pull requests as draft if they are not ready for review.
3+
#
4+
# To disable the comment, set the `PR_DRAFT_NUDGE_COMMENT_DISABLE` variable in the repository settings to `true`.
5+
# The default value is `false`, which means the comment will be posted.
6+
# To set the variable, you can use the GitHub CLI:
7+
# gh variable set PR_DRAFT_NUDGE_COMMENT_DISABLE --body "true"
8+
###
9+
10+
name: PR Draft Nudge
11+
12+
on:
13+
pull_request:
14+
types: [opened]
15+
16+
permissions: {}
17+
18+
jobs:
19+
pr-draft-nudge:
20+
name: PR Draft Nudge
21+
runs-on: ubuntu-latest
22+
timeout-minutes: 10
23+
permissions:
24+
contents: read
25+
pull-requests: write
26+
steps:
27+
- uses: actions/checkout@v4
28+
29+
- name: Run
30+
uses: actions/github-script@v7
31+
env:
32+
PR_DRAFT_NUDGE_COMMENT_DISABLE: ${{ vars.PR_DRAFT_NUDGE_COMMENT_DISABLE || 'false' }}
33+
with:
34+
script: |
35+
const script = require('./.github/scripts/pr-draft-nudge.js');
36+
await script({ github, context, core });

0 commit comments

Comments
 (0)