-
Notifications
You must be signed in to change notification settings - Fork 1
101 lines (95 loc) · 3.97 KB
/
invite-command.yml
File metadata and controls
101 lines (95 loc) · 3.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# 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 }}