zephyr/scripts/ci/do_not_merge.py
Fabio Baltieri 42e8837085 scripts: do_not_merge: wait for manifest before checking
Seems like github relabel rerun is susceptible to race conditions and
may not rerun the label check script if the manifest workflow runs at
the same time.

Delay the check to explicitly wait until the currently checked sha had a
successful manifest run.

Signed-off-by: Fabio Baltieri <fabiobaltieri@google.com>
2025-07-03 11:59:56 -05:00

95 lines
2.2 KiB
Python
Executable File

#!/usr/bin/env python3
# Copyright 2025 Google LLC
# SPDX-License-Identifier: Apache-2.0
import argparse
import datetime
import os
import sys
import time
import github
DNM_LABELS = ["DNM", "DNM (manifest)", "TSC", "Architecture Review", "dev-review"]
def print_rate_limit(gh, org):
response = gh.get_organization(org)
for header, value in response.raw_headers.items():
if header.startswith("x-ratelimit"):
print(f"{header}: {value}")
def parse_args(argv):
parser = argparse.ArgumentParser(
description=__doc__,
formatter_class=argparse.RawDescriptionHelpFormatter,
allow_abbrev=False,
)
parser.add_argument("-p", "--pull-request", required=True, type=int, help="The PR number")
return parser.parse_args(argv)
WAIT_FOR_WORKFLOWS = set({"Manifest"})
WAIT_FOR_DELAY_S = 60
def workflow_delay(repo, pr):
print(f"PR is at {pr.head.sha}")
while True:
runs = repo.get_workflow_runs(head_sha=pr.head.sha)
completed = set()
for run in runs:
print(f"{run.name}: {run.status} {run.conclusion} {run.html_url}")
if run.status == "completed" and run.conclusion == "success":
completed.add(run.name)
if WAIT_FOR_WORKFLOWS.issubset(completed):
return
ts = datetime.datetime.now()
print(f"wait: {ts} completed={completed}")
time.sleep(WAIT_FOR_DELAY_S)
def main(argv):
args = parse_args(argv)
token = os.environ.get('GITHUB_TOKEN', None)
gh = github.Github(token)
print_rate_limit(gh, "zephyrproject-rtos")
repo = gh.get_repo("zephyrproject-rtos/zephyr")
pr = repo.get_pull(args.pull_request)
workflow_delay(repo, pr)
print(f"pr: {pr.html_url}")
fail = False
for label in pr.get_labels():
print(f"label: {label.name}")
if label.name in DNM_LABELS:
print(f"Pull request is labeled as \"{label.name}\".")
fail = True
if not pr.body:
print("Pull request is description is empty.")
fail = True
if fail:
print("This workflow fails so that the pull request cannot be merged.")
sys.exit(1)
if __name__ == "__main__":
sys.exit(main(sys.argv[1:]))