@@ -18,32 +18,66 @@ def validate_author_pr_is_member_of_org(
1818 # repo.organization.has_in_members(user) always returns false ¯\_(ツ)_/¯
1919 if not org .has_in_members (pull .user ):
2020 raise ValueError (
21- f"PR { pull .number } was created by user that is not a member of the "
21+ f"PR { pull .number } was created by a user that is not a member of the "
2222 f"repo organization"
2323 )
2424
2525
26- def validate_pr_target_branch_in_valid_branches (pull : PullRequest , valid_branches : str ):
26+ def validate_pr_target_branch_in_valid_branches (
27+ pull : PullRequest , valid_branches : list [str ]
28+ ):
2729 target_branch = pull .base .ref
28- if target_branch not in map (str .strip , valid_branches .split ("," )):
29- raise ValueError (
30- f"Target branch { target_branch } is not in valid branches { valid_branches } "
30+ if target_branch not in map (str .strip , valid_branches ):
31+ emsg = (
32+ f"The target branch { target_branch } is not in valid "
33+ f"branches { valid_branches } "
3134 )
35+ raise ValueError (emsg )
36+
37+
38+ def get_pull_request_number (branch_name : str ) -> int :
39+ return int (branch_name .removeprefix ("pull/" ))
40+
41+
42+ def is_pull_request_branch (branch_name : str ) -> bool :
43+ try :
44+ get_pull_request_number (branch_name )
45+ except ValueError :
46+ result = False
47+ else :
48+ result = True
49+ return result
50+
51+
52+ def get_branch_name (branch_name : str ) -> str :
53+ return branch_name .removeprefix ("refs/heads/" )
54+
55+
56+ def print_stop_tc_build_msg (comment : str , re_add_to_queue : bool = False ):
57+ str_re_add = str (re_add_to_queue ).lower ()
58+ click .echo (f"##teamcity[buildStop comment='{ comment } ' readdToQueue='{ str_re_add } ']" )
3259
3360
3461def verify_user_can_trigger_build (
3562 vcs_root_url : str ,
36- pr_number : str ,
37- valid_branches : str ,
63+ branch_name : str ,
64+ valid_branches : list [ str ] ,
3865 token : str ,
3966):
4067 _ , owner , repo_name = vcs_root_url .removesuffix (".git" ).rsplit ("/" , 2 )
4168 github = Github (token )
4269 repo = github .get_repo (f"{ owner } /{ repo_name } " )
43- pull = repo .get_pull (int (pr_number ))
4470
45- try :
46- validate_pr_target_branch_in_valid_branches (pull , valid_branches )
47- validate_author_pr_is_member_of_org (repo , github , pull )
48- except ValueError as e :
49- click .echo (f"##teamcity[buildStop comment='{ str (e )} ' readdToQueue='false']" )
71+ if is_pull_request_branch (branch_name ):
72+ pull = repo .get_pull (get_pull_request_number (branch_name ))
73+ try :
74+ validate_pr_target_branch_in_valid_branches (pull , valid_branches )
75+ validate_author_pr_is_member_of_org (repo , github , pull )
76+ except ValueError as e :
77+ print_stop_tc_build_msg (str (e ))
78+ else :
79+ if get_branch_name (branch_name ) not in valid_branches :
80+ print_stop_tc_build_msg (
81+ f"The branch '{ branch_name } ' is not in valid branches "
82+ f"'{ valid_branches } '"
83+ )
0 commit comments