Skip to content

Commit b289d58

Browse files
[MNT] issue-assign-bot - only users with write access should be able to assign others (#2739)
* only admins can assign others * improve comment message * Automatic `pre-commit` fixes * fixes according to latest code * Automatic `pre-commit` fixes * changes according to latest code * resolve merge conflicts * Automatic `pre-commit` fixes * adjust according to latest code * merge --------- Co-authored-by: Matthew Middlehurst <pfm15hbu@gmail.com>
1 parent 1b6192c commit b289d58

File tree

1 file changed

+22
-17
lines changed

1 file changed

+22
-17
lines changed

.github/utilities/issue_assign.py

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,8 @@
11
"""Script for the GitHub issue self-assign bot.
22
3-
It checks if a comment on an issue or PR includes the trigger
4-
phrase (as defined) and a mentioned user.
5-
If it does, it assigns the issue to the mentioned user.
6-
7-
Users without write access can only have up to 2 open issues assigned.
8-
Users with write access (or admin) are exempt from this limit.
9-
If a non-write user already has 2 or more open issues, the bot
10-
comments on the issue with links to the currently assigned open issues.
3+
Checks if a comment on an issue or PR includes the trigger phrase and a mentioned user.
4+
If it does, it assigns or unassigns the issue to the mentioned user if they have
5+
permissions.
116
"""
127

138
import json
@@ -27,6 +22,8 @@
2722
pr = context_dict["event"]["issue"].get("pull_request")
2823
comment_body = context_dict["event"]["comment"]["body"]
2924
commenter = context_dict["event"]["comment"]["user"]["login"]
25+
commenter_permission = repo.get_collaborator_permission(commenter)
26+
has_write_permission = commenter_permission not in ["admin", "write"]
3027

3128
restricted_labels = {"meta-issue"}
3229

@@ -50,17 +47,26 @@
5047
mentioned_users.append(commenter)
5148
mentioned_users = set(mentioned_users)
5249

50+
access_error = False
5351
for user in mentioned_users:
54-
user_obj = g.get_user(user)
55-
permission = repo.get_collaborator_permission(user_obj)
52+
# Can only assign others if the commenter has write access
53+
if user != commenter and not has_write_permission:
54+
if not access_error:
55+
issue.create_comment(
56+
"Cannot assign other users to issues without write access."
57+
)
58+
access_error = True
59+
continue
5660

57-
if permission in ["admin", "write"]:
61+
# If the user is already assigned to this issue, remove them
62+
if user in [assignee.login for assignee in issue.assignees]:
63+
issue.remove_from_assignees(user)
64+
continue
65+
66+
# If the commenter has write access, just assign
67+
if has_write_permission:
5868
issue.add_to_assignees(user)
5969
else:
60-
# First check if the user is already assigned to this issue
61-
if user in [assignee.login for assignee in issue.assignees]:
62-
continue
63-
6470
# search for open issues only
6571
query = f"repo:{repo.full_name} is:issue is:open assignee:{user}"
6672
issues_assigned_to_user = g.search_issues(query)
@@ -73,7 +79,7 @@
7379
for assigned_issue in issues_assigned_to_user
7480
]
7581

76-
comment_message = (
82+
issue.create_comment(
7783
f"@{user}, already has {assigned_count} open issues assigned."
7884
"Users without write access are limited to self-assigning "
7985
"three issues.\n\n"
@@ -82,6 +88,5 @@
8288
f"- {issue_link}" for issue_link in assigned_issues_list
8389
)
8490
)
85-
issue.create_comment(comment_message)
8691
else:
8792
issue.add_to_assignees(user)

0 commit comments

Comments
 (0)