|
| 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 | +}; |
0 commit comments