|
| 1 | +// .github/scripts/gfi_assign_on_comment.js |
| 2 | +// |
| 3 | +// Assigns human user to Good First Issue when they comment "/assign". |
| 4 | +// Posts a comment if the issue is already assigned. |
| 5 | +// All other validation and additional GFI comments are handled by other existing bots which can be refactored with time. |
| 6 | + |
| 7 | +const GOOD_FIRST_ISSUE_LABEL = 'Good First Issue'; |
| 8 | +const UNASSIGNED_GFI_SEARCH_URL = |
| 9 | + 'https://github.com/hiero-ledger/hiero-sdk-python/issues?q=is%3Aissue%20state%3Aopen%20label%3A%22Good%20First%20Issue%22%20no%3Aassignee'; |
| 10 | + |
| 11 | +/// HELPERS FOR ASSIGNING /// |
| 12 | + |
| 13 | +/** |
| 14 | + * Returns true if /assign appears at the start of a line or comment |
| 15 | + * Optionally preceded or followed by whitespace |
| 16 | + */ |
| 17 | +function commentRequestsAssignment(body) { |
| 18 | + return typeof body === 'string' && |
| 19 | + /(^|\n)\s*\/assign(\s|$)/i.test(body); |
| 20 | +} |
| 21 | + |
| 22 | +/** |
| 23 | + * Returns true if the issue has the good first issue label. |
| 24 | + */ |
| 25 | +function issueIsGoodFirstIssue(issue) { |
| 26 | + return issue?.labels?.some(label => label.name === GOOD_FIRST_ISSUE_LABEL); |
| 27 | +} |
| 28 | + |
| 29 | +/// HELPERS FOR COMMENTING /// |
| 30 | + |
| 31 | +/** |
| 32 | + * Returns a formatted @username for the current assignee of the issue. |
| 33 | + */ |
| 34 | +function getCurrentAssigneeMention(issue) { |
| 35 | + const login = issue?.assignees?.[0]?.login; |
| 36 | + return login ? `@${login}` : 'someone'; |
| 37 | +} |
| 38 | + |
| 39 | +/** |
| 40 | + * Builds a comment explaining that the issue is already assigned. |
| 41 | + * Requester username is passed from main |
| 42 | + */ |
| 43 | +function commentAlreadyAssigned(requesterUsername, issue) { |
| 44 | + return ( |
| 45 | + `Hi @${requesterUsername} — this issue is already assigned to ${getCurrentAssigneeMention(issue)}, so I can’t assign it again. |
| 46 | +
|
| 47 | +👉 **Choose a different Good First Issue to work on next:** |
| 48 | +[Browse unassigned Good First Issues](${UNASSIGNED_GFI_SEARCH_URL}) |
| 49 | +
|
| 50 | +Once you find one you like, comment \`/assign\` to get started.` |
| 51 | + ); |
| 52 | +} |
| 53 | + |
| 54 | + |
| 55 | +/// START OF SCRIPT /// |
| 56 | +module.exports = async ({ github, context }) => { |
| 57 | + const { issue, comment } = context.payload; |
| 58 | + const { owner, repo } = context.repo; |
| 59 | + |
| 60 | + // Reject if issue, comment or comment user is missing, reject bots, or if no /assign message |
| 61 | + if ( |
| 62 | + !issue?.number || |
| 63 | + !comment?.body || |
| 64 | + !comment?.user?.login || |
| 65 | + comment.user.type === 'Bot' || |
| 66 | + !commentRequestsAssignment(comment.body) |
| 67 | + ) { |
| 68 | + return; |
| 69 | + } |
| 70 | + // Reject if issue is not a Good First Issue |
| 71 | + if (!issueIsGoodFirstIssue(issue)) return; |
| 72 | + |
| 73 | + // Get requester username and issue number to enable comments and assignments |
| 74 | + const requesterUsername = comment.user.login; |
| 75 | + const issueNumber = issue.number; |
| 76 | + |
| 77 | + // Reject if issue is already assigned |
| 78 | + // Comment failure to the requester |
| 79 | + if (issue.assignees?.length > 0) { |
| 80 | + await github.rest.issues.createComment({ |
| 81 | + owner, |
| 82 | + repo, |
| 83 | + issue_number: issueNumber, |
| 84 | + body: commentAlreadyAssigned(requesterUsername, issue), |
| 85 | + }); |
| 86 | + return; |
| 87 | + } |
| 88 | + |
| 89 | + // All validations passed and user has requested assignment on a GFI |
| 90 | + // Assign the issue to the requester |
| 91 | + // Do not comment on success |
| 92 | + await github.rest.issues.addAssignees({ |
| 93 | + owner, |
| 94 | + repo, |
| 95 | + issue_number: issueNumber, |
| 96 | + assignees: [requesterUsername], |
| 97 | + }); |
| 98 | +}; |
0 commit comments