invite-command #63
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
| # trigger by chatops '/invite' | |
| name: Invite command | |
| on: | |
| repository_dispatch: | |
| types: [invite-command] | |
| jobs: | |
| publish: | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Link to this workflow in command comment | |
| uses: peter-evans/create-or-update-comment@v2 | |
| with: | |
| token: ${{ secrets.BSDATA_BOT_TOKEN }} | |
| repository: ${{ github.event.client_payload.github.payload.repository.full_name }} | |
| comment-id: ${{ github.event.client_payload.github.payload.comment.id }} | |
| body: | | |
| <!-- Command output --> | |
| --- | |
| [Workflow run](https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}) | |
| - name: Create invite | |
| uses: actions/github-script@v6 | |
| id: invite_script | |
| with: | |
| github-token: ${{ secrets.BSDATA_BOT_TOKEN }} | |
| result-encoding: string | |
| script: | | |
| let ctx = context.payload.client_payload.github; | |
| let login = ctx.payload.issue.user.login; | |
| let access = 'push'; | |
| try { | |
| if (!/join request/gi.test(ctx.payload.issue.title)) | |
| { | |
| throw 'Issue must have a "[Join Request]" title'; | |
| } | |
| let { data: response, status } = await github.rest.repos.addCollaborator({ | |
| owner: ctx.payload.repository.owner.login, | |
| repo: ctx.payload.repository.name, | |
| username: login, | |
| permission: access | |
| }); | |
| if (status === 201) | |
| { | |
| core.setOutput('inviteUrl', response.html_url); | |
| return `:robot: Invited @${login} with **${access}** access. Invite can be accepted on ${response.html_url}`; | |
| } | |
| else if (status === 204) | |
| { | |
| throw "User already is a collaborator (code 204)."; | |
| } | |
| else { | |
| throw `Unknown error (code ${status})`; | |
| } | |
| } catch (error) { | |
| let msg = `Failed to invite @${login} with **${access}** access:\n${error}`; | |
| core.setOutput('error', msg); | |
| throw error; | |
| } | |
| - name: Add reaction to command comment on success | |
| uses: peter-evans/create-or-update-comment@v2 | |
| with: | |
| token: ${{ secrets.BSDATA_BOT_TOKEN }} | |
| repository: ${{ github.event.client_payload.github.payload.repository.full_name }} | |
| comment-id: ${{ github.event.client_payload.github.payload.comment.id }} | |
| body: ${{ steps.invite_script.outputs.result }} | |
| reactions: hooray | |
| - name: Add reaction to command comment on failure | |
| uses: peter-evans/create-or-update-comment@v2 | |
| if: failure() | |
| with: | |
| token: ${{ secrets.BSDATA_BOT_TOKEN }} | |
| repository: ${{ github.event.client_payload.github.payload.repository.full_name }} | |
| comment-id: ${{ github.event.client_payload.github.payload.comment.id }} | |
| body: "**Error**: ${{ steps.invite_script.outputs.error }}" | |
| reactions: "-1" | |
| - name: Checkout chatops | |
| uses: actions/checkout@v3 | |
| - name: Format closing comment | |
| uses: Amadevus/pwsh-script@v2 | |
| id: format_comment | |
| env: | |
| INVITE_URL: ${{ steps.invite_script.outputs.inviteUrl }} | |
| with: | |
| script: | | |
| $text = Get-Content ./docs/InviteTemplate.md -Raw | |
| $ctx = $github.event.client_payload.github | |
| $login = $ctx.payload.issue.user.login | |
| $inviteUrl = $ctx.payload.repository.html_url | |
| return ($text -f $login, $env:INVITE_URL) | |
| - name: Close issue | |
| uses: peter-evans/close-issue@v2 | |
| with: | |
| token: ${{ secrets.BSDATA_BOT_TOKEN }} | |
| repository: ${{ github.event.client_payload.github.payload.repository.full_name }} | |
| issue-number: ${{ github.event.client_payload.github.payload.issue.number }} | |
| comment: ${{ steps.format_comment.outputs.result }} |