diff --git a/.github/workflows/take.yml b/.github/workflows/take.yml new file mode 100644 index 0000000000..ddfa734a81 --- /dev/null +++ b/.github/workflows/take.yml @@ -0,0 +1,58 @@ +name: Assign Issue via 'take' Comment + +on: + issue_comment: + types: [created] + +permissions: + issues: write + +jobs: + issue_assign: + runs-on: ubuntu-latest + if: | + !github.event.issue.pull_request && + contains(github.event.comment.body, '/take') + concurrency: + group: ${{ github.workflow }}-${{ github.event.issue.number }} + cancel-in-progress: true + + steps: + - name: Check if commenter can be assigned + id: check_assignee + run: | + HTTP_CODE=$(curl -X GET \ + -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \ + -H "Accept: application/vnd.github.v3+json" \ + -o /dev/null -w '%{http_code}\n' -s \ + "https://api.github.com/repos/${{ github.repository }}/issues/${{ github.event.issue.number }}/assignees/${{ github.event.comment.user.login }}") + + if [ "$HTTP_CODE" -eq "204" ]; then + echo "can_assign=true" >> $GITHUB_OUTPUT + else + echo "can_assign=false" >> $GITHUB_OUTPUT + fi + + - name: Assign issue + if: steps.check_assignee.outputs.can_assign == 'true' + run: | + curl -X POST \ + -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \ + -H "Accept: application/vnd.github.v3+json" \ + -d '{"assignees": ["${{ github.event.comment.user.login }}"]}' \ + "https://api.github.com/repos/${{ github.repository }}/issues/${{ github.event.issue.number }}/assignees" + + echo "Issue #${{ github.event.issue.number }} assigned to ${{ github.event.comment.user.login }}" + + - name: Comment if assignment failed + if: steps.check_assignee.outputs.can_assign == 'false' + uses: actions/github-script@v6 + with: + github-token: ${{secrets.GITHUB_TOKEN}} + script: | + github.rest.issues.createComment({ + issue_number: context.issue.number, + owner: context.repo.owner, + repo: context.repo.name, + body: '@${{ github.event.comment.user.login }} Unable to assign this issue to you. You may not have the necessary permissions.' + })