-
Notifications
You must be signed in to change notification settings - Fork 410
179 lines (161 loc) · 6.85 KB
/
Copy pathtake.yml
File metadata and controls
179 lines (161 loc) · 6.85 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
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
name: Assign/unassign the issue via `take` or `untake` comment
on:
issue_comment:
types: created
permissions:
issues: write
jobs:
issue_assign:
runs-on: ubuntu-slim
if: (!github.event.issue.pull_request) && (github.event.comment.body == 'take' || github.event.comment.body == 'untake')
concurrency:
group: ${{ github.workflow }}-${{ github.event.issue.number }}
queue: max
steps:
- name: Take or untake issue
env:
COMMENT_BODY: ${{ github.event.comment.body }}
ISSUE_NUMBER: ${{ github.event.issue.number }}
USER_LOGIN: ${{ github.event.comment.user.login }}
REPO: ${{ github.repository }}
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
shell: bash
run: |
set -euo pipefail
API_VERSION="2026-03-10"
ISSUE_ENDPOINT="repos/$REPO/issues/$ISSUE_NUMBER"
ASSIGNEES_ENDPOINT="$ISSUE_ENDPOINT/assignees"
COMMENTS_ENDPOINT="$ISSUE_ENDPOINT/comments"
post_comment() {
local body="$1"
gh api --silent \
--method POST \
--header "X-GitHub-Api-Version: $API_VERSION" \
"$COMMENTS_ENDPOINT" \
--raw-field "body=$body"
}
reply() {
local body="$1"
echo "$body"
post_comment "$body"
}
fail_with_comment() {
local body="$1"
echo "$body" >&2
if ! post_comment "$body"; then
echo "Could not post the failure message to issue $ISSUE_NUMBER" >&2
fi
exit 1
}
if [[ "$COMMENT_BODY" == "take" ]]; then
if ! CURRENT_ASSIGNEES="$(
gh api \
--header "X-GitHub-Api-Version: $API_VERSION" \
"$ISSUE_ENDPOINT" \
--jq '[.assignees[].login]'
)"; then
fail_with_comment \
"I could not inspect the current assignees. Please try again later."
fi
if jq -e --arg user "$USER_LOGIN" \
'any(.[]; . != $user)' <<<"$CURRENT_ASSIGNEES" >/dev/null; then
CURRENT_HOLDERS="$(
jq -r --arg user "$USER_LOGIN" \
'[.[] | select(. != $user)] | join(", ")' \
<<<"$CURRENT_ASSIGNEES"
)"
reply \
"Issue #$ISSUE_NUMBER is already assigned to $CURRENT_HOLDERS, so it was not reassigned."
exit 0
fi
if jq -e --arg user "$USER_LOGIN" \
'index($user) != null' <<<"$CURRENT_ASSIGNEES" >/dev/null; then
echo "Issue #$ISSUE_NUMBER is already assigned to $USER_LOGIN."
exit 0
fi
echo "Assigning issue $ISSUE_NUMBER to $USER_LOGIN"
if ! ASSIGNMENT_RESPONSE="$(
gh api \
--method POST \
--header "X-GitHub-Api-Version: $API_VERSION" \
"$ASSIGNEES_ENDPOINT" \
--field "assignees[]=$USER_LOGIN"
)"; then
fail_with_comment \
"I could not assign this issue because the GitHub API request failed. Please try again later."
fi
if ! jq -e '.assignees | type == "array"' \
<<<"$ASSIGNMENT_RESPONSE" >/dev/null; then
fail_with_comment \
"I could not verify the new assignee. Please try again later."
fi
if ! jq -e --arg user "$USER_LOGIN" \
'.assignees | map(.login) | index($user) != null' \
<<<"$ASSIGNMENT_RESPONSE" >/dev/null; then
reply \
"GitHub did not add $USER_LOGIN as an assignee. The account may not be assignable in this repository."
exit 0
fi
if ! jq -e --arg user "$USER_LOGIN" \
'.assignees | length == 1 and .[0].login == $user' \
<<<"$ASSIGNMENT_RESPONSE" >/dev/null; then
CURRENT_HOLDERS="$(
jq -r --arg user "$USER_LOGIN" \
'[.assignees[].login | select(. != $user)] | join(", ")' \
<<<"$ASSIGNMENT_RESPONSE"
)"
if ! ROLLBACK_RESPONSE="$(
gh api \
--method DELETE \
--header "X-GitHub-Api-Version: $API_VERSION" \
"$ASSIGNEES_ENDPOINT" \
--field "assignees[]=$USER_LOGIN"
)"; then
fail_with_comment \
"Another assignee appeared while take was running, and I could not undo the conflicting assignment."
fi
if ! jq -e --arg user "$USER_LOGIN" \
'.assignees | type == "array" and (map(.login) | index($user) == null)' \
<<<"$ROLLBACK_RESPONSE" >/dev/null; then
fail_with_comment \
"Another assignee appeared while take was running, and I could not verify that the conflicting assignment was undone."
fi
reply \
"Issue #$ISSUE_NUMBER was assigned to $CURRENT_HOLDERS while take was running, so it was not reassigned."
exit 0
fi
elif [[ "$COMMENT_BODY" == "untake" ]]; then
echo "Unassigning issue $ISSUE_NUMBER from $USER_LOGIN"
if ! UNASSIGNMENT_RESPONSE="$(
gh api \
--method DELETE \
--header "X-GitHub-Api-Version: $API_VERSION" \
"$ASSIGNEES_ENDPOINT" \
--field "assignees[]=$USER_LOGIN"
)"; then
fail_with_comment \
"I could not unassign this issue because the GitHub API request failed. Please try again later."
fi
if ! jq -e --arg user "$USER_LOGIN" \
'.assignees | type == "array" and (map(.login) | index($user) == null)' \
<<<"$UNASSIGNMENT_RESPONSE" >/dev/null; then
fail_with_comment \
"I could not verify that $USER_LOGIN was unassigned. Please try again later."
fi
fi