-
-
Notifications
You must be signed in to change notification settings - Fork 0
201 lines (160 loc) · 10.7 KB
/
Copy pathauto-assign-developer.yml
File metadata and controls
201 lines (160 loc) · 10.7 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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
name: Auto Assign Developer and Add Label
on:
issues:
types: [opened]
issue_comment:
types: [created]
jobs:
handle-issue-events:
runs-on: ubuntu-latest
permissions:
issues: write
pull-requests: write
steps:
- name: Check event type and process
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
COMMENT_BODY: ${{ github.event.comment.body }}
run: |
# 处理新创建的 issue
if [ "${{ github.event_name }}" = "issues" ] && [ "${{ github.event.action }}" = "opened" ]; then
echo "Processing new issue..."
ISSUE_NUMBER=${{ github.event.issue.number }}
ISSUE_AUTHOR=${{ github.event.issue.user.login }}
COMMENT="@$ISSUE_AUTHOR 您好,已收到您的问题,我们会尽快处理。\n@xiaoshaziYA 有新的问题需要确认,在此issues下回复「确认」以进行确认。"
# 添加评论
curl -s -H "Authorization: token $GITHUB_TOKEN" \
-H "Accept: application/vnd.github.v3+json" \
-X POST \
-d "{\"body\":\"$COMMENT\"}" \
"https://api.github.com/repos/${{ github.repository }}/issues/${ISSUE_NUMBER}/comments"
# 添加标签
curl -s -H "Authorization: token $GITHUB_TOKEN" \
-H "Accept: application/vnd.github.v3+json" \
-X POST \
-d '{"labels":["×等待开发者确认"]}' \
"https://api.github.com/repos/${{ github.repository }}/issues/${ISSUE_NUMBER}/labels"
# 处理评论事件
elif [ "${{ github.event_name }}" = "issue_comment" ] && [ "${{ github.event.action }}" = "created" ]; then
echo "Processing comment..."
# 获取评论内容和评论者信息
COMMENT_BODY="$COMMENT_BODY"
COMMENTER="${{ github.event.comment.user.login }}"
COMMENT_ID="${{ github.event.comment.id }}"
ISSUE_NUMBER=${{ github.event.issue.number }}
ISSUE_AUTHOR=${{ github.event.issue.user.login }}
# 检查评论内容是否包含"确认"
if echo "$COMMENT_BODY" | grep -q "确认"; then
echo "Comment contains '确认'"
# 检查评论者是否有仓库写入权限
PERMISSION_CHECK=$(curl -s -H "Authorization: token $GITHUB_TOKEN" \
-H "Accept: application/vnd.github.v3+json" \
"https://api.github.com/repos/${{ github.repository }}/collaborators/${COMMENTER}/permission")
PERMISSION_LEVEL=$(echo "$PERMISSION_CHECK" | jq -r '.permission')
# 如果有写入权限或以上权限(admin, write, maintain)
if [ "$PERMISSION_LEVEL" = "admin" ] || [ "$PERMISSION_LEVEL" = "write" ] || [ "$PERMISSION_LEVEL" = "maintain" ]; then
echo "User $COMMENTER has permission level: $PERMISSION_LEVEL"
# 获取当前issue的标签和分配信息
CURRENT_ISSUE=$(curl -s -H "Authorization: token $GITHUB_TOKEN" \
-H "Accept: application/vnd.github.v3+json" \
"https://api.github.com/repos/${{ github.repository }}/issues/${ISSUE_NUMBER}")
HAS_CONFIRMED_LABEL=$(echo "$CURRENT_ISSUE" | jq '.labels[] | select(.name == "✓已确认") | length > 0')
# 如果已经有"已确认"标签
if [ "$HAS_CONFIRMED_LABEL" = "true" ]; then
echo "Issue already confirmed, responding to duplicate confirmation..."
# 回复评论,引用该评论
DUPLICATE_RESPONSE="> @$COMMENTER 确认\n\n已由其他管理人员确认,无须再次确认。"
curl -s -H "Authorization: token $GITHUB_TOKEN" \
-H "Accept: application/vnd.github.v3+json" \
-X POST \
-d "{\"body\":\"$DUPLICATE_RESPONSE\"}" \
"https://api.github.com/repos/${{ github.repository }}/issues/${ISSUE_NUMBER}/comments"
else
echo "First confirmation, updating labels..."
# 移除"等待开发者确认"标签(如果存在)
curl -s -H "Authorization: token $GITHUB_TOKEN" \
-H "Accept: application/vnd.github.v3+json" \
-X DELETE \
"https://api.github.com/repos/${{ github.repository }}/issues/${ISSUE_NUMBER}/labels/×等待开发者确认" || true
# 添加"已确认"标签
curl -s -H "Authorization: token $GITHUB_TOKEN" \
-H "Accept: application/vnd.github.v3+json" \
-X POST \
-d '{"labels":["✓已确认"]}' \
"https://api.github.com/repos/${{ github.repository }}/issues/${ISSUE_NUMBER}/labels"
echo "Label updated to '✓已确认'"
# 添加提示信息,告诉管理员完成后回复"结束:理由"
COMPLETION_PROMPT="> @$COMMENTER 确认\n\n感谢您的确认!当您完成这个issue时,请回复「结束:(结束的理由)」来关闭此issue并通知提出者。"
curl -s -H "Authorization: token $GITHUB_TOKEN" \
-H "Accept: application/vnd.github.v3+json" \
-X POST \
-d "{\"body\":\"$COMPLETION_PROMPT\"}" \
"https://api.github.com/repos/${{ github.repository }}/issues/${ISSUE_NUMBER}/comments"
fi
# 只有有权限的用户才被分配issue
echo "Assigning issue to $COMMENTER (has permission: $PERMISSION_LEVEL)"
# 获取当前已分配的人员
CURRENT_ASSIGNEES=$(echo "$CURRENT_ISSUE" | jq '[.assignees[].login]')
# 如果评论者还没有被分配,则添加到分配列表
if ! echo "$CURRENT_ASSIGNEES" | jq -r ".[]" | grep -q "^$COMMENTER$"; then
# 添加新分配者到现有分配列表
NEW_ASSIGNEES=$(echo "$CURRENT_ASSIGNEES" | jq --arg committer "$COMMENTER" '. + [$committer] | unique')
# 更新分配
curl -s -H "Authorization: token $GITHUB_TOKEN" \
-H "Accept: application/vnd.github.v3+json" \
-X POST \
-d "{\"assignees\":$NEW_ASSIGNEES}" \
"https://api.github.com/repos/${{ github.repository }}/issues/${ISSUE_NUMBER}/assignees"
echo "Assigned issue to $COMMENTER"
else
echo "$COMMENTER is already assigned to this issue"
fi
else
echo "User $COMMENTER does not have sufficient permissions (current: $PERMISSION_LEVEL), skipping assignment and label changes"
# 回复无权限的用户
NO_PERMISSION_RESPONSE="> @$COMMENTER 确认\n\n抱歉,您没有足够的权限来确认此issue。只有具有写入权限的管理人员才能进行确认操作。"
curl -s -H "Authorization: token $GITHUB_TOKEN" \
-H "Accept: application/vnd.github.v3+json" \
-X POST \
-d "{\"body\":\"$NO_PERMISSION_RESPONSE\"}" \
"https://api.github.com/repos/${{ github.repository }}/issues/${ISSUE_NUMBER}/comments"
fi
# 检查评论内容是否包含"结束:"前缀
elif echo "$COMMENT_BODY" | grep -q "^结束:"; then
echo "Comment contains '结束:' prefix"
# 检查评论者是否有仓库写入权限
PERMISSION_CHECK=$(curl -s -H "Authorization: token $GITHUB_TOKEN" \
-H "Accept: application/vnd.github.v3+json" \
"https://api.github.com/repos/${{ github.repository }}/collaborators/${COMMENTER}/permission")
PERMISSION_LEVEL=$(echo "$PERMISSION_CHECK" | jq -r '.permission')
# 如果有写入权限或以上权限(admin, write, maintain)
if [ "$PERMISSION_LEVEL" = "admin" ] || [ "$PERMISSION_LEVEL" = "write" ] || [ "$PERMISSION_LEVEL" = "maintain" ]; then
echo "User $COMMENTER has permission level: $PERMISSION_LEVEL"
# 提取结束理由(去除"结束:"前缀)
REASON=$(echo "$COMMENT_BODY" | sed 's/^结束://')
# 回复评论,引用该评论并通知提出者
COMPLETION_RESPONSE="> @$COMMENTER 结束:$REASON\n\n@$ISSUE_AUTHOR 您的问题已解决:$REASON\n\n此issue将被关闭。"
curl -s -H "Authorization: token $GITHUB_TOKEN" \
-H "Accept: application/vnd.github.v3+json" \
-X POST \
-d "{\"body\":\"$COMPLETION_RESPONSE\"}" \
"https://api.github.com/repos/${{ github.repository }}/issues/${ISSUE_NUMBER}/comments"
# 关闭issue
curl -s -H "Authorization: token $GITHUB_TOKEN" \
-H "Accept: application/vnd.github.v3+json" \
-X PATCH \
-d '{"state":"closed"}' \
"https://api.github.com/repos/${{ github.repository }}/issues/${ISSUE_NUMBER}"
echo "Issue closed by $COMMENTER with reason: $REASON"
else
echo "User $COMMENTER does not have sufficient permissions (current: $PERMISSION_LEVEL), cannot close issue"
# 回复无权限的用户
NO_PERMISSION_RESPONSE="> @$COMMENTER 结束:$REASON\n\n抱歉,您没有足够的权限来关闭此issue。只有具有写入权限的管理人员才能进行此操作。"
curl -s -H "Authorization: token $GITHUB_TOKEN" \
-H "Accept: application/vnd.github.v3+json" \
-X POST \
-d "{\"body\":\"$NO_PERMISSION_RESPONSE\"}" \
"https://api.github.com/repos/${{ github.repository }}/issues/${ISSUE_NUMBER}/comments"
fi
fi
fi