forked from hiero-ledger/hiero-sdk-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbot-assignment-check.sh
More file actions
184 lines (145 loc) · 5.43 KB
/
Copy pathbot-assignment-check.sh
File metadata and controls
184 lines (145 loc) · 5.43 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
#!/bin/bash
set -Eeuo pipefail
# Configuration for labels (overridable from workflow env)
MENTOR_LABEL="${MENTOR_LABEL:-notes: mentor-duty}"
if [ -z "${MENTOR_LABEL}" ]; then
echo "Error: Missing required environment variable (MENTOR_LABEL)." >&2
exit 1
fi
# Validate required env vars
if [ -z "${ASSIGNEE:-}" ] || [ -z "${ISSUE_NUMBER:-}" ] || [ -z "${REPO:-}" ]; then
echo "Error: Missing required environment variables (ASSIGNEE, ISSUE_NUMBER, REPO)."
exit 1
fi
echo "Checking assignment rules for user $ASSIGNEE on issue #$ISSUE_NUMBER"
# Helper functions
get_permission() {
gh api "repos/${REPO}/collaborators/${ASSIGNEE}/permission" --jq '.permission' 2>/dev/null || echo "none"
}
is_spam_user() {
local spam_file=".github/spam-list.txt"
# Check static spam list
if [[ -f "$spam_file" ]]; then
if grep -vE '^\s*#|^\s*$' "$spam_file" | grep -qxF "$ASSIGNEE"; then
return 0
fi
else
echo "Spam list file not found. Treating as empty." >&2
fi
return 1
}
issue_has_gfi() {
local has
has=$(gh api "repos/${REPO}/issues/${ISSUE_NUMBER}" --jq 'any(.labels[]; .name=="Good First Issue")' || echo "false")
[[ "$has" == "true" ]]
}
# Count open assignments for a user
# For triage users (mentors), excludes issues with '${MENTOR_LABEL}' label
# This allows mentors to be assigned to mentorship issues without consuming their assignment limit
assignments_count() {
local permission="${1:-none}"
if [[ "$permission" == "triage" ]]; then
echo "Triage user detected — excluding ${MENTOR_LABEL} issues from count." >&2
# For triage users, exclude issues with the configured mentor label
gh api "repos/${REPO}/issues?per_page=100&page=1" \
-f assignee="${ASSIGNEE}" \
-f state=open \
--jq --arg mentor_label "$MENTOR_LABEL" '.[]
| select(.pull_request == null)
| select(any(.labels[]; .name == $mentor_label) | not)
| .number' | grep -c . || echo 0
else
# For non-triage users, count all open assignments
gh issue list --repo "${REPO}" --assignee "${ASSIGNEE}" --state open --limit 100 --json number --jq 'length'
fi
}
remove_assignee() {
gh issue edit "${ISSUE_NUMBER}" --repo "${REPO}" --remove-assignee "${ASSIGNEE}"
}
post_comment() {
gh issue comment "$ISSUE_NUMBER" --repo "$REPO" --body "$1"
}
msg_spam_non_gfi() {
cat <<EOF
Hi @$ASSIGNEE, this is the Assignment Bot.
:warning: **Assignment Restricted**
Your account currently has limited assignment privileges. You may only be assigned to issues labeled **Good First Issue**.
**Current Restrictions:**
- :white_check_mark: Can be assigned to 'Good First Issue' labeled issues (maximum 1 at a time)
- :x: Cannot be assigned to other issues
**How to have restrictions lifted:**
1. Successfully complete and merge your assigned Good First Issue
2. Demonstrate consistent, quality contributions
3. Contact a maintainer to review your restriction status
Thank you for your understanding!
EOF
}
msg_spam_limit_exceeded() {
local count="$1"
cat <<EOF
Hi @$ASSIGNEE, this is the Assignment Bot.
:warning: **Assignment Limit Exceeded**
Your account currently has limited assignment privileges with a maximum of **1 open assignment** at a time.
You currently have $count open issue(s) assigned. Please complete and merge your existing assignment before requesting a new one.
**Current Restrictions:**
- Maximum 1 open issue assignment at a time
- Can only be assigned to 'Good First Issue' labeled issues
**How to have restrictions lifted:**
1. Successfully complete and merge your current assigned issue
2. Demonstrate consistent, quality contributions
3. Contact a maintainer to review your restriction status
Thank you for your cooperation!
EOF
}
msg_normal_limit_exceeded() {
cat <<EOF
Hi @$ASSIGNEE, this is the Assignment Bot.
Assigning you to this issue would exceed the limit of 2 open assignments.
Please resolve and merge your existing assigned issues before requesting new ones.
EOF
}
# Check permission level
PERMISSION="$(get_permission)"
echo "User permission level: $PERMISSION"
if [[ "$PERMISSION" == "admin" || "$PERMISSION" == "write" ]]; then
echo "User is a maintainer or committer. Limit does not apply."
exit 0
fi
# Check spam status
SPAM=false
if is_spam_user; then
SPAM=true
echo "User is in spam list. Applying restricted assignment rules."
fi
COUNT="$(assignments_count "$PERMISSION")"
# Apply assignment rules
if [[ "$SPAM" == "true" ]]; then
# Spam users can only be assigned to Good First Issues
if ! issue_has_gfi; then
echo "Issue does not have 'Good First Issue' label."
echo "Spam-listed user attempting to assign non-GFI issue. Revoking assignment."
remove_assignee
post_comment "$(msg_spam_non_gfi)"
exit 1
fi
echo "Issue has 'Good First Issue' label."
# Spam users have a limit of 1 open assignment
echo "Spam-listed user has $COUNT open assignments."
if (( COUNT > 1 )); then
echo "Spam user limit exceeded (Max 1 allowed). Revoking assignment."
remove_assignee
post_comment "$(msg_spam_limit_exceeded "$COUNT")"
exit 1
fi
echo "Spam-listed user assignment valid. User has $COUNT assignment(s)."
else
# Normal users have a limit of 2 open assignments
echo "Current open assignments count: $COUNT"
if (( COUNT > 2 )); then
echo "Limit exceeded (Max 2 allowed). Revoking assignment."
remove_assignee
post_comment "$(msg_normal_limit_exceeded)"
exit 1
fi
echo "Assignment valid. User has $COUNT assignments."
fi