-
Notifications
You must be signed in to change notification settings - Fork 9
153 lines (137 loc) · 5.2 KB
/
add-discord-role.yml
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
name: add-discord-role
on:
workflow_call:
inputs:
team_slug:
required: true
type: string
discord_handle:
required: true
type: string
secrets:
PAT:
required: true
DISCORD_DEVBOT_TOKEN:
required: true
DISCORD_SOLANA_GUILD_ID:
required: true
jobs:
main:
runs-on: ubuntu-latest
steps:
- name: parse-info
shell: bash
id: info
run: |
set -e
discord_handle="${{ inputs.discord_handle }}"
userid=""
if [[ "$discord_handle" =~ ([^#]+)#([0-9]+)$ ]]; then
username="${BASH_REMATCH[1]}"
discriminator="${BASH_REMATCH[2]}"
userid=$(
curl -s -H "Authorization: Bot ${{ secrets.DISCORD_DEVBOT_TOKEN }}" \
"https://discord.com/api/guilds/${{ secrets.DISCORD_SOLANA_GUILD_ID }}/members/search?limit=1000&query=$username" | \
jq --raw-output --arg discriminator "$discriminator" '.[] | select(.user.discriminator == $discriminator) | .user.id'
)
else
result=$(
curl -s -H "Authorization: Bot ${{ secrets.DISCORD_DEVBOT_TOKEN }}" \
"https://discord.com/api/guilds/${{ secrets.DISCORD_SOLANA_GUILD_ID }}/members/search?limit=1000&query=$discord_handle" | \
jq --raw-output --arg username "$discord_handle" '.[] | select(.user.username == $username) | .user.id'
)
results=($result)
if [[ "${#results[@]}" -gt 1 ]]; then
echo "too many results"
exit 1
fi
userid="${results[0]}"
fi
if [[ -z $userid ]]; then
echo "get empty user id"
exit 1
fi
echo "userid=$userid" >> $GITHUB_OUTPUT
case ${{ inputs.team_slug }} in
"monorepo-triage")
echo "new_role_name=Monorepo-Triage" >> $GITHUB_OUTPUT
;;
"monorepo-write")
echo "new_role_name=Monorepo-Write" >> $GITHUB_OUTPUT
echo "old_role_name=Monorepo-Triage" >> $GITHUB_OUTPUT
;;
"spl-triage")
echo "new_role_name=SPL-Triage" >> $GITHUB_OUTPUT
;;
"spl-write")
echo "new_role_name=SPL-Write" >> $GITHUB_OUTPUT
echo "old_role_name=SPL-Triage" >> $GITHUB_OUTPUT
;;
*)
echo "unexpected team slug: ${{ inputs.team_slug }}"
exit 1
;;
esac
- name: add contributor role
shell: bash
run: |
roleid=$(
curl -s -H "Authorization: Bot ${{ secrets.DISCORD_DEVBOT_TOKEN }}" \
"https://discord.com/api/guilds/${{ secrets.DISCORD_SOLANA_GUILD_ID }}/roles" | \
jq -r '.[] | select(.name=="Contributor") | .id'
)
if [[ $roleid = "null" ]]; then
echo "failed to get role id"
exit 1
fi
curl -s \
-X PUT \
-H "Authorization: Bot ${{ secrets.DISCORD_DEVBOT_TOKEN }}" \
-H "Content-Length: 0" \
"https://discord.com/api/guilds/${{ secrets.DISCORD_SOLANA_GUILD_ID }}/members/${{ steps.info.outputs.userid }}/roles/$roleid"
- name: add new role
if: ${{ steps.info.outputs.new_role_name }}
shell: bash
run: |
roleid=$(
curl -s -H "Authorization: Bot ${{ secrets.DISCORD_DEVBOT_TOKEN }}" \
"https://discord.com/api/guilds/${{ secrets.DISCORD_SOLANA_GUILD_ID }}/roles" | \
jq -r '.[] | select(.name=="${{ steps.info.outputs.new_role_name }}") | .id'
)
if [[ $roleid = "null" ]]; then
echo "failed to get role id"
exit 1
fi
curl -s \
-X PUT \
-H "Authorization: Bot ${{ secrets.DISCORD_DEVBOT_TOKEN }}" \
-H "Content-Length: 0" \
"https://discord.com/api/guilds/${{ secrets.DISCORD_SOLANA_GUILD_ID }}/members/${{ steps.info.outputs.userid }}/roles/$roleid"
- name: remove old role
if: ${{ steps.info.outputs.old_role_name }}
shell: bash
run: |
roleid=$(
curl -s -H "Authorization: Bot ${{ secrets.DISCORD_DEVBOT_TOKEN }}" \
"https://discord.com/api/guilds/${{ secrets.DISCORD_SOLANA_GUILD_ID }}/roles" | \
jq -r '.[] | select(.name=="${{ steps.info.outputs.old_role_name }}") | .id'
)
if [[ $roleid = "null" ]]; then
echo "failed to get role id"
exit 1
fi
curl -s \
-X DELETE \
-H "Authorization: Bot ${{ secrets.DISCORD_DEVBOT_TOKEN }}" \
-H "Content-Length: 0" \
"https://discord.com/api/guilds/${{ secrets.DISCORD_SOLANA_GUILD_ID }}/members/${{ steps.info.outputs.userid }}/roles/$roleid"
- uses: actions/github-script@v6
with:
github-token: ${{ secrets.PAT }}
script: |
await github.rest.issues.addLabels({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
labels: ["discord"]
})