Skip to content

Commit cc8636b

Browse files
committed
Block presubmit runs for PRs from 3rd-party forks.
Going forward these builds will be blocked before running any code, and must be unblocked by someone who has "Build & Read" permissions for the corresponding pipeline. This commit also fixes is_pull_request() which returned incorrect results when the presubmit ran for a PR in a branch of the bazelbuild/bazel repo.
1 parent 7ac1c0c commit cc8636b

File tree

1 file changed

+47
-7
lines changed

1 file changed

+47
-7
lines changed

buildkite/bazelci.py

+47-7
Original file line numberDiff line numberDiff line change
@@ -1525,9 +1525,17 @@ def PrepareRepoInCwd(print_cmd_groups, initial_setup=False):
15251525
upload_corrupted_outputs(capture_corrupted_outputs_dir_index, tmpdir)
15261526

15271527
if platform == "windows":
1528-
execute_batch_commands(task_config.get("post_batch_commands", None), True, ":batch: Post Processing (Batch Commands)")
1528+
execute_batch_commands(
1529+
task_config.get("post_batch_commands", None),
1530+
True,
1531+
":batch: Post Processing (Batch Commands)",
1532+
)
15291533
else:
1530-
execute_shell_commands(task_config.get("post_shell_commands", None), True, ":bash: Post Processing (Shell Commands)")
1534+
execute_shell_commands(
1535+
task_config.get("post_shell_commands", None),
1536+
True,
1537+
":bash: Post Processing (Shell Commands)",
1538+
)
15311539

15321540
finally:
15331541
terminate_background_process(sc_process)
@@ -1634,8 +1642,21 @@ def get_release_name_from_branch_name():
16341642

16351643

16361644
def is_pull_request():
1637-
third_party_repo = os.getenv("BUILDKITE_PULL_REQUEST_REPO", "")
1638-
return len(third_party_repo) > 0
1645+
try:
1646+
return int(os.getenv("BUILDKITE_PULL_REQUEST")) > 0
1647+
except:
1648+
return False
1649+
1650+
1651+
def is_third_party_fork():
1652+
if ":" in os.getenv(
1653+
"BUILDKITE_BRANCH", ""
1654+
): # Only works if "Prefix third-party fork branch names" is enabled
1655+
return True
1656+
1657+
pr_repo = os.getenv("BUILDKITE_PULL_REQUEST_REPO", "")
1658+
# We don't accept PRs for GoB repos.
1659+
return pr_repo and not pr_repo.startswith("https://github.com/bazelbuild/")
16391660

16401661

16411662
def print_bazel_version_info(bazel_binary, platform):
@@ -1817,7 +1838,9 @@ def clone_git_repository(git_repository, platform, git_commit=None):
18171838
return clone_path
18181839

18191840

1820-
def execute_batch_commands(commands, print_group=True, group_message=":batch: Setup (Batch Commands)"):
1841+
def execute_batch_commands(
1842+
commands, print_group=True, group_message=":batch: Setup (Batch Commands)"
1843+
):
18211844
if not commands:
18221845
return
18231846

@@ -1828,7 +1851,9 @@ def execute_batch_commands(commands, print_group=True, group_message=":batch: Se
18281851
return subprocess.run(batch_commands, shell=True, check=True, env=os.environ).returncode
18291852

18301853

1831-
def execute_shell_commands(commands, print_group=True, group_message=":bash: Setup (Shell Commands)"):
1854+
def execute_shell_commands(
1855+
commands, print_group=True, group_message=":bash: Setup (Shell Commands)"
1856+
):
18321857
if not commands:
18331858
return
18341859

@@ -2181,7 +2206,9 @@ def calculate_targets(
21812206

21822207
build_targets = [] if test_only else list(task_config.get("build_targets", []))
21832208
test_targets = [] if build_only else list(task_config.get("test_targets", []))
2184-
coverage_targets = [] if (build_only or test_only) else list(task_config.get("coverage_targets", []))
2209+
coverage_targets = (
2210+
[] if (build_only or test_only) else list(task_config.get("coverage_targets", []))
2211+
)
21852212
index_targets = [] if (build_only or test_only) else list(task_config.get("index_targets", []))
21862213

21872214
index_targets_query = (
@@ -2767,6 +2794,19 @@ def print_project_pipeline(
27672794
if is_git_on_borg_repo(buildkite_repo):
27682795
show_gerrit_review_link(buildkite_repo, pipeline_steps)
27692796

2797+
# Only run presubmits from third-party forks after getting approval from someone with "Build & Read" permissions.
2798+
if is_pull_request() and is_third_party_fork():
2799+
pipeline_steps.append(
2800+
{
2801+
"block": ":cop: Authorize third-party presubmit run?",
2802+
"prompt": (
2803+
":rotating_light: :warning: This is an untrusted pull request from a third-party fork. "
2804+
"Only unblock the build if the code is not malicious."
2805+
),
2806+
"blocked_state": "running",
2807+
}
2808+
)
2809+
27702810
task_configs = filter_tasks_that_should_be_skipped(task_configs, pipeline_steps)
27712811

27722812
# In Bazel Downstream Project pipelines, git_repository and project_name must be specified.

0 commit comments

Comments
 (0)