|
| 1 | +# workflows/assign-github-issue.yml |
| 2 | +# |
| 3 | +# Assign GitHub Issue |
| 4 | +# Automatically assign an issue to the commenter if they use the '/take' command. |
| 5 | + |
| 6 | +name: Assign GitHub Issue |
| 7 | + |
| 8 | +on: |
| 9 | + issue_comment: |
| 10 | + types: [created] |
| 11 | + |
| 12 | +# Required to assign the issue to the commenter |
| 13 | +permissions: |
| 14 | + issues: write |
| 15 | + |
| 16 | +concurrency: |
| 17 | + group: assign-github-issue-${{ github.workflow }}-${{ github.event.issue.number }} |
| 18 | + cancel-in-progress: true |
| 19 | + |
| 20 | +jobs: |
| 21 | + assign-github-issue: |
| 22 | + name: Assign GitHub Issue to Commenter |
| 23 | + runs-on: ubuntu-latest |
| 24 | + if: | |
| 25 | + !github.event.issue.pull_request && |
| 26 | + contains(github.event.comment.body, '/take') |
| 27 | +
|
| 28 | + steps: |
| 29 | + - name: Check if Commenter Can Be Assigned |
| 30 | + id: check_assignee |
| 31 | + run: | |
| 32 | + HTTP_CODE=$(curl -X GET \ |
| 33 | + -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \ |
| 34 | + -H "Accept: application/vnd.github.v3+json" \ |
| 35 | + -o /dev/null -w '%{http_code}\n' -s \ |
| 36 | + "https://api.github.com/repos/${{ github.repository }}/issues/${{ github.event.issue.number }}/assignees/${{ github.event.comment.user.login }}") |
| 37 | +
|
| 38 | + if [ "$HTTP_CODE" -eq "204" ]; then |
| 39 | + echo "can_assign=true" >> $GITHUB_OUTPUT |
| 40 | + else |
| 41 | + echo "can_assign=false" >> $GITHUB_OUTPUT |
| 42 | + fi |
| 43 | +
|
| 44 | + - name: Assign GitHub Issue |
| 45 | + if: steps.check_assignee.outputs.can_assign == 'true' |
| 46 | + run: | |
| 47 | + curl -X POST \ |
| 48 | + -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \ |
| 49 | + -H "Accept: application/vnd.github.v3+json" \ |
| 50 | + -d '{"assignees": ["${{ github.event.comment.user.login }}"]}' \ |
| 51 | + "https://api.github.com/repos/${{ github.repository }}/issues/${{ github.event.issue.number }}/assignees" |
| 52 | +
|
| 53 | + echo "Issue #${{ github.event.issue.number }} assigned to ${{ github.event.comment.user.login }}" |
| 54 | +
|
| 55 | + - name: Notify of Assignment Failure |
| 56 | + if: steps.check_assignee.outputs.can_assign == 'false' |
| 57 | + uses: actions/github-script@v8 |
| 58 | + with: |
| 59 | + github-token: ${{secrets.GITHUB_TOKEN}} |
| 60 | + script: | |
| 61 | + github.rest.issues.createComment({ |
| 62 | + issue_number: context.issue.number, |
| 63 | + owner: context.repo.owner, |
| 64 | + repo: context.repo.name, |
| 65 | + body: '@${{ github.event.comment.user.login }} Unable to assign this issue to you. You may not have the necessary permissions.' |
| 66 | + }) |
0 commit comments