11from github import Github
2+ from github .PullRequest import PullRequest
3+ from github .Repository import Repository
24
35
4- def is_author_pr_is_member_of_org (
5- vcs_root_url : str , pr_number : str , token : str
6- ) -> bool :
7- _ , owner , repo_name = vcs_root_url .removesuffix (".git" ).rsplit ("/" , 2 )
8- github = Github (token )
9- repo = github .get_repo (f"{ owner } /{ repo_name } " )
10- pull = repo .get_pull (int (pr_number ))
6+ def validate_author_pr_is_member_of_org (
7+ repo : Repository , github : Github , pull : PullRequest
8+ ):
119 org_login = repo .organization .login
1210 org = github .get_organization (org_login )
1311 token_user = github .get_user (github .get_user ().login )
@@ -17,26 +15,31 @@ def is_author_pr_is_member_of_org(
1715 f"rights 'read:org' so cannot check members of the organization"
1816 )
1917 # repo.organization.has_in_members(user) always returns false ¯\_(ツ)_/¯
20- return org .has_in_members (pull .user )
18+ if not org .has_in_members (pull .user ):
19+ raise ValueError (
20+ f"PR { pull .number } was created by user that is not a member of the "
21+ f"repo organization"
22+ )
2123
2224
23- def is_pr_target_branch_in_valid_branches (target_branch : str , valid_branches : str ):
24- return target_branch in map (str .strip , valid_branches .split ("," ))
25+ def validate_pr_target_branch_in_valid_branches (pull : PullRequest , valid_branches : str ):
26+ target_branch = pull .base .ref
27+ if target_branch not in map (str .strip , valid_branches .split ("," )):
28+ raise ValueError (
29+ f"Target branch { target_branch } is not in valid branches { valid_branches } "
30+ )
2531
2632
2733def verify_user_can_trigger_build (
2834 vcs_root_url : str ,
2935 pr_number : str ,
30- target_branch : str ,
3136 valid_branches : str ,
3237 token : str ,
3338):
34- if not is_pr_target_branch_in_valid_branches (target_branch , valid_branches ):
35- raise ValueError (
36- f"Target branch { target_branch } is not in valid branches { valid_branches } "
37- )
38- if not is_author_pr_is_member_of_org (vcs_root_url , pr_number , token ):
39- raise ValueError (
40- f"PR { pr_number } was created by user that is not a member of the "
41- f"repo organization"
42- )
39+ _ , owner , repo_name = vcs_root_url .removesuffix (".git" ).rsplit ("/" , 2 )
40+ github = Github (token )
41+ repo = github .get_repo (f"{ owner } /{ repo_name } " )
42+ pull = repo .get_pull (int (pr_number ))
43+
44+ validate_pr_target_branch_in_valid_branches (pull , valid_branches )
45+ validate_author_pr_is_member_of_org (repo , github , pull )
0 commit comments