Skip to content

Commit 452e771

Browse files
committed
get target branch from the PR
1 parent 030513c commit 452e771

4 files changed

Lines changed: 26 additions & 26 deletions

File tree

scripts/cli.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,6 @@ def trigger_auto_tests(
8484
)
8585
@click.option("--vcs-root-url", required=True, help="VCS URL")
8686
@click.option("--pr-number", required=True, help="PR number")
87-
@click.option("--target-branch", required=True, help="Target branch of the PR")
8887
@click.option(
8988
"--valid-branches",
9089
default="master",
@@ -105,14 +104,12 @@ def trigger_auto_tests(
105104
def verify_user_can_trigger(
106105
vcs_root_url: str,
107106
pr_number: str,
108-
target_branch: str,
109107
valid_branches: str,
110108
token: str,
111109
):
112110
verify_user_can_trigger_build(
113111
vcs_root_url=vcs_root_url,
114112
pr_number=pr_number,
115-
target_branch=target_branch,
116113
valid_branches=valid_branches,
117114
token=token,
118115
)

scripts/pr_check/pr_check.py

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
from 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

2733
def 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)

tests/test_smth.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import pytest
22

3-
from scripts.pr_check.pr_check import is_pr_target_branch_in_valid_branches
3+
from scripts.pr_check.pr_check import validate_pr_target_branch_in_valid_branches
44

55

66
def test_smth():
77
with pytest.raises(TypeError):
8-
is_pr_target_branch_in_valid_branches()
8+
validate_pr_target_branch_in_valid_branches()

version.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1.0.2
1+
1.0.3

0 commit comments

Comments
 (0)