1#!/usr/bin/env python3
2
3# Copyright 2025 Google LLC
4# SPDX-License-Identifier: Apache-2.0
5
6import argparse
7import datetime
8import os
9import sys
10import time
11
12import github
13
14DNM_LABELS = ["DNM", "DNM (manifest)", "TSC", "Architecture Review", "dev-review"]
15
16
17def print_rate_limit(gh, org):
18    response = gh.get_organization(org)
19    for header, value in response.raw_headers.items():
20        if header.startswith("x-ratelimit"):
21            print(f"{header}: {value}")
22
23
24def parse_args(argv):
25    parser = argparse.ArgumentParser(
26        description=__doc__,
27        formatter_class=argparse.RawDescriptionHelpFormatter,
28        allow_abbrev=False,
29    )
30
31    parser.add_argument("-p", "--pull-request", required=True, type=int, help="The PR number")
32
33    return parser.parse_args(argv)
34
35
36WAIT_FOR_WORKFLOWS = set({"Manifest"})
37WAIT_FOR_DELAY_S = 60
38
39
40def workflow_delay(repo, pr):
41    print(f"PR is at {pr.head.sha}")
42
43    while True:
44        runs = repo.get_workflow_runs(head_sha=pr.head.sha)
45
46        completed = set()
47        for run in runs:
48            print(f"{run.name}: {run.status} {run.conclusion} {run.html_url}")
49            if run.status == "completed" and run.conclusion == "success":
50                completed.add(run.name)
51
52        if WAIT_FOR_WORKFLOWS.issubset(completed):
53            return
54
55        ts = datetime.datetime.now()
56        print(f"wait: {ts} completed={completed}")
57        time.sleep(WAIT_FOR_DELAY_S)
58
59
60def main(argv):
61    args = parse_args(argv)
62
63    token = os.environ.get('GITHUB_TOKEN', None)
64    gh = github.Github(token)
65
66    print_rate_limit(gh, "zephyrproject-rtos")
67
68    repo = gh.get_repo("zephyrproject-rtos/zephyr")
69    pr = repo.get_pull(args.pull_request)
70
71    workflow_delay(repo, pr)
72
73    print(f"pr: {pr.html_url}")
74
75    fail = False
76
77    for label in pr.get_labels():
78        print(f"label: {label.name}")
79
80        if label.name in DNM_LABELS:
81            print(f"Pull request is labeled as \"{label.name}\".")
82            fail = True
83
84    if not pr.body:
85        print("Pull request is description is empty.")
86        fail = True
87
88    if fail:
89        print("This workflow fails so that the pull request cannot be merged.")
90        sys.exit(1)
91
92
93if __name__ == "__main__":
94    sys.exit(main(sys.argv[1:]))
95