Skip to content

Commit cd0b1f2

Browse files
committed
rename cli option "--pr-number" to "--branch-name"; accept build for branches in valid branches not only PRs
1 parent e3c334a commit cd0b1f2

File tree

3 files changed

+56
-18
lines changed

3 files changed

+56
-18
lines changed

scripts/cli.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,11 @@ def trigger_auto_tests(
8383
),
8484
)
8585
@click.option("--vcs-root-url", required=True, help="VCS URL")
86-
@click.option("--pr-number", required=True, help="PR number")
86+
@click.option(
87+
"--branch-name",
88+
required=True,
89+
help="The branch name. Branch contains pull request id or branch name",
90+
)
8791
@click.option(
8892
"--valid-branches",
8993
default="master",
@@ -103,14 +107,14 @@ def trigger_auto_tests(
103107
)
104108
def verify_user_can_trigger(
105109
vcs_root_url: str,
106-
pr_number: str,
110+
branch_name: str,
107111
valid_branches: str,
108112
token: str,
109113
):
110114
verify_user_can_trigger_build(
111115
vcs_root_url=vcs_root_url,
112-
pr_number=pr_number,
113-
valid_branches=valid_branches,
116+
branch_name=branch_name,
117+
valid_branches=valid_branches.split(","),
114118
token=token,
115119
)
116120

scripts/pr_check/pr_check.py

Lines changed: 47 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -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

3461
def 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+
)

version.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1.0.5
1+
2.0.0

0 commit comments

Comments
 (0)