|
1 | 1 | """Script for the GitHub issue self-assign bot.
|
2 | 2 |
|
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. |
11 | 6 | """
|
12 | 7 |
|
13 | 8 | import json
|
|
27 | 22 | pr = context_dict["event"]["issue"].get("pull_request")
|
28 | 23 | comment_body = context_dict["event"]["comment"]["body"]
|
29 | 24 | 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"] |
30 | 27 |
|
31 | 28 | restricted_labels = {"meta-issue"}
|
32 | 29 |
|
|
50 | 47 | mentioned_users.append(commenter)
|
51 | 48 | mentioned_users = set(mentioned_users)
|
52 | 49 |
|
| 50 | + access_error = False |
53 | 51 | 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 |
56 | 60 |
|
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: |
58 | 68 | issue.add_to_assignees(user)
|
59 | 69 | 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 |
| - |
64 | 70 | # search for open issues only
|
65 | 71 | query = f"repo:{repo.full_name} is:issue is:open assignee:{user}"
|
66 | 72 | issues_assigned_to_user = g.search_issues(query)
|
|
73 | 79 | for assigned_issue in issues_assigned_to_user
|
74 | 80 | ]
|
75 | 81 |
|
76 |
| - comment_message = ( |
| 82 | + issue.create_comment( |
77 | 83 | f"@{user}, already has {assigned_count} open issues assigned."
|
78 | 84 | "Users without write access are limited to self-assigning "
|
79 | 85 | "three issues.\n\n"
|
|
82 | 88 | f"- {issue_link}" for issue_link in assigned_issues_list
|
83 | 89 | )
|
84 | 90 | )
|
85 |
| - issue.create_comment(comment_message) |
86 | 91 | else:
|
87 | 92 | issue.add_to_assignees(user)
|
0 commit comments