Auto-assign on comment #54
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Auto-assign on comment | |
| on: | |
| issue_comment: | |
| types: [created] | |
| permissions: | |
| issues: write | |
| pull-requests: write | |
| jobs: | |
| auto-assign: | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Check for assign request | |
| uses: actions/github-script@v6 | |
| with: | |
| github-token: ${{ secrets.GITHUB_TOKEN }} | |
| script: | | |
| const comment = context.payload.comment.body.toLowerCase().trim(); | |
| const assignee = context.payload.comment.user.login; | |
| const issueNumber = context.issue.number; | |
| const patterns = [ | |
| 'assign me', | |
| 'i want to work on this', | |
| "i'd like to work on this", | |
| 'i would like to work on this', | |
| 'can i work on this', | |
| 'please assign me', | |
| 'assign this to me', | |
| 'i can work on this' | |
| ]; | |
| const shouldAssign = patterns.some(pattern => comment.includes(pattern)); | |
| if (shouldAssign) { | |
| try { | |
| // Check if user is already assigned | |
| const issue = await github.rest.issues.get({ | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| issue_number: issueNumber | |
| }); | |
| const currentAssignees = issue.data.assignees.map(a => a.login); | |
| if (currentAssignees.includes(assignee)) { | |
| await github.rest.issues.createComment({ | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| issue_number: issueNumber, | |
| body: `@${assignee} you are already assigned to this issue.` | |
| }); | |
| return; | |
| } | |
| // Add assignee | |
| await github.rest.issues.addAssignees({ | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| issue_number: issueNumber, | |
| assignees: [assignee] | |
| }); | |
| await github.rest.issues.createComment({ | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| issue_number: issueNumber, | |
| body: `@${assignee} has been assigned to this issue! 🎉\n\nYou can now start working on it. Good luck!` | |
| }); | |
| console.log(`Successfully assigned @${assignee} to issue #${issueNumber}`); | |
| } catch (error) { | |
| console.error(`Failed to assign user: ${error.message}`); | |
| context.log.error(error); | |
| } | |
| } |