22import re
33import sys
44
5- from github .PullRequest import PullRequest
6- from github .Repository import Repository
7- from github .MainClass import Github
8- from github .GithubException import UnknownObjectException
9- from github .Organization import Organization
10- from github .Team import Team
11-
125from constants import (
136 ALL_LABELS_DICT ,
7+ APPROVED ,
148 CANCEL_ACTION ,
159 CHANGED_REQUESTED_BY_LABEL_PREFIX ,
1610 COMMENTED_BY_LABEL_PREFIX ,
2216 SUPPORTED_LABELS ,
2317 VERIFIED_LABEL_STR ,
2418 WELCOME_COMMENT ,
25- APPROVED ,
2619)
20+ from github .GithubException import UnknownObjectException
21+ from github .MainClass import Github
22+ from github .Organization import Organization
23+ from github .PullRequest import PullRequest
24+ from github .Repository import Repository
25+ from github .Team import Team
2726from simple_logger .logger import get_logger
2827
2928LOGGER = get_logger (name = "pr_labeler" )
@@ -35,7 +34,7 @@ class SupportedActions:
3534 pr_size_action_name : str = "add-pr-size-label"
3635 welcome_comment_action_name : str = "add-welcome-comment-set-assignee"
3736 build_push_pr_image_action_name : str = "push-container-on-comment"
38- supported_actions : set [str ] = {
37+ supported_actions : set [str ] = { # noqa: RUF012
3938 pr_size_action_name ,
4039 add_remove_labels_action_name ,
4140 welcome_comment_action_name ,
@@ -48,7 +47,7 @@ def __init__(self) -> None:
4847 self .gh_client : Github
4948
5049 self .repo_name = os .environ ["GITHUB_REPOSITORY" ]
51- self .pr_number = int (os .getenv ("GITHUB_PR_NUMBER" , 0 ))
50+ self .pr_number = int (os .getenv ("GITHUB_PR_NUMBER" , "0" ))
5251 self .action = os .getenv ("ACTION" )
5352 self .event_action = os .getenv ("GITHUB_EVENT_ACTION" )
5453 self .event_name = os .getenv ("GITHUB_EVENT_NAME" )
@@ -110,7 +109,7 @@ def verify_allowed_user(self) -> bool:
110109 # check if the user is a member of opendatahub-tests-contributors
111110 membership = team .get_team_membership (member = self .user_login )
112111 LOGGER .info (f"User { self .user_login } is a member of the test contributor team. { membership } " )
113- return True
112+ return True # noqa: TRY300
114113 except UnknownObjectException :
115114 LOGGER .error (f"User { self .user_login } is not allowed for this action. Exiting." )
116115 return False
@@ -123,23 +122,19 @@ def verify_labeler_config(self) -> None:
123122 if not self .user_login :
124123 sys .exit ("`GITHUB_USER_LOGIN` is not set" )
125124
126- if (
127- self .event_name == "issue_comment" or self .event_name == "pull_request_review"
128- ) and not self .comment_body :
125+ if (self .event_name in {"issue_comment" , "pull_request_review" }) and not self .comment_body :
129126 LOGGER .info ("No comment, nothing to do. Exiting." )
130127 sys .exit (0 )
131128
132129 def run_pr_label_action (self ) -> None :
133130 if self .action == self .SupportedActions .pr_size_action_name :
134131 self .set_pr_size ()
135132
136- if self .action == self .SupportedActions .build_push_pr_image_action_name :
137- if not self .verify_allowed_user ():
138- sys .exit (1 )
133+ if self .action == self .SupportedActions .build_push_pr_image_action_name and not self .verify_allowed_user ():
134+ sys .exit (1 )
139135
140- if self .action == self .SupportedActions .add_remove_labels_action_name :
141- if self .verify_allowed_user ():
142- self .add_remove_pr_labels ()
136+ if self .action == self .SupportedActions .add_remove_labels_action_name and self .verify_allowed_user ():
137+ self .add_remove_pr_labels ()
143138
144139 if self .action == self .SupportedActions .welcome_comment_action_name :
145140 self .add_welcome_comment_set_assignee ()
@@ -187,10 +182,9 @@ def set_label_in_repository(self, label: str) -> None:
187182 LOGGER .info (f"repo labels: { repo_labels } " )
188183
189184 try :
190- if _repo_label := self .repo .get_label (name = label ):
191- if _repo_label .color != label_color :
192- LOGGER .info (f"Edit repository label: { label } , color: { label_color } " )
193- _repo_label .edit (name = _repo_label .name , color = label_color )
185+ if (_repo_label := self .repo .get_label (name = label )) and _repo_label .color != label_color :
186+ LOGGER .info (f"Edit repository label: { label } , color: { label_color } " )
187+ _repo_label .edit (name = _repo_label .name , color = label_color )
194188
195189 except UnknownObjectException :
196190 LOGGER .info (f"Add repository label: { label } , color: { label_color } " )
@@ -249,16 +243,15 @@ def add_remove_pr_labels(self) -> None:
249243
250244 return
251245
252- elif self .event_name == "pull_request_review" :
246+ elif (
247+ self .event_name == "pull_request_review"
248+ or self .event_name == "workflow_run"
249+ and self .event_action == "submitted"
250+ ):
253251 self .pull_request_review_label_actions ()
254252
255253 return
256254
257- # We will only reach here if the PR was created from a fork
258- elif self .event_name == "workflow_run" and self .event_action == "submitted" :
259- self .pull_request_review_label_actions ()
260- return
261-
262255 LOGGER .warning ("`add_remove_pr_label` called without a supported event" )
263256
264257 def pull_request_review_label_actions (
@@ -317,7 +310,7 @@ def issue_comment_label_actions(
317310 if not action [CANCEL_ACTION ] or self .event_action == "deleted" :
318311 self .approve_pr ()
319312
320- label_in_pr = any ([ label == _label .lower () for _label in self .pr_labels ] )
313+ label_in_pr = any (label == _label .lower () for _label in self .pr_labels )
321314 LOGGER .info (f"Processing label: { label } , action: { action } " )
322315
323316 if action [CANCEL_ACTION ] or self .event_action == "deleted" :
0 commit comments