|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 3 | +# SPDX-License-Identifier: Apache-2.0 |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | +"""Check whether a PR's base is too far behind the target branch. |
| 17 | +
|
| 18 | +Fails (or warns, in warn-only mode) when the merge-base between the PR head |
| 19 | +and the target branch is older than configured thresholds. See |
| 20 | +``reports/TRTLLM-12092-design.md`` for the rationale. |
| 21 | +""" |
| 22 | + |
| 23 | +import os |
| 24 | +import subprocess |
| 25 | +import sys |
| 26 | + |
| 27 | + |
| 28 | +def _git(*args: str) -> str: |
| 29 | + return subprocess.run(["git", *args], capture_output=True, text=True, check=True).stdout.strip() |
| 30 | + |
| 31 | + |
| 32 | +def _env_int(name: str, default: int) -> int: |
| 33 | + raw = os.environ.get(name, "").strip() |
| 34 | + if not raw: |
| 35 | + return default |
| 36 | + try: |
| 37 | + return int(raw) |
| 38 | + except ValueError: |
| 39 | + print(f"::warning::{name}='{raw}' is not an integer; using default {default}") |
| 40 | + return default |
| 41 | + |
| 42 | + |
| 43 | +def _env_bool(name: str, default: bool = False) -> bool: |
| 44 | + return os.environ.get(name, str(default)).strip().lower() in {"1", "true", "yes"} |
| 45 | + |
| 46 | + |
| 47 | +def main() -> int: |
| 48 | + pr_head = os.environ.get("PR_HEAD_SHA", "").strip() |
| 49 | + target_ref = os.environ.get("TARGET_REF", "origin/main").strip() |
| 50 | + commits_limit = _env_int("COMMITS_BEHIND_LIMIT", 200) |
| 51 | + age_limit_days = _env_int("BASE_AGE_LIMIT_DAYS", 14) |
| 52 | + enforce = _env_bool("ENFORCE") |
| 53 | + |
| 54 | + if not pr_head: |
| 55 | + print("::error::PR_HEAD_SHA is not set") |
| 56 | + return 1 |
| 57 | + |
| 58 | + merge_base = _git("merge-base", pr_head, target_ref) |
| 59 | + commits_behind = int(_git("rev-list", "--count", f"{merge_base}..{target_ref}")) |
| 60 | + |
| 61 | + target_ts = int(_git("show", "-s", "--format=%ct", target_ref)) |
| 62 | + base_ts = int(_git("show", "-s", "--format=%ct", merge_base)) |
| 63 | + age_days = max(0.0, (target_ts - base_ts) / 86400.0) |
| 64 | + |
| 65 | + base_summary = _git("show", "-s", "--format=%h %s", merge_base) |
| 66 | + target_summary = _git("show", "-s", "--format=%h %s", target_ref) |
| 67 | + |
| 68 | + commits_exceeded = commits_behind > commits_limit |
| 69 | + age_exceeded = age_days > age_limit_days |
| 70 | + stale = commits_exceeded or age_exceeded |
| 71 | + |
| 72 | + summary_lines = [ |
| 73 | + "PR base freshness report", |
| 74 | + f" target ref: {target_ref}", |
| 75 | + f" commits behind target: {commits_behind} (limit: {commits_limit})", |
| 76 | + f" base commit age: {age_days:.1f} days (limit: {age_limit_days})", |
| 77 | + f" merge base: {base_summary}", |
| 78 | + f" target HEAD: {target_summary}", |
| 79 | + ] |
| 80 | + print("\n".join(summary_lines)) |
| 81 | + |
| 82 | + gh_summary = os.environ.get("GITHUB_STEP_SUMMARY") |
| 83 | + if gh_summary: |
| 84 | + with open(gh_summary, "a", encoding="utf-8") as fh: |
| 85 | + fh.write("## PR Base Freshness\n\n") |
| 86 | + fh.write("| metric | value | limit |\n") |
| 87 | + fh.write("| --- | --- | --- |\n") |
| 88 | + fh.write(f"| commits behind `{target_ref}` | {commits_behind} | {commits_limit} |\n") |
| 89 | + fh.write(f"| merge-base age (days) | {age_days:.1f} | {age_limit_days} |\n\n") |
| 90 | + fh.write(f"- merge base: `{base_summary}`\n") |
| 91 | + fh.write(f"- target HEAD: `{target_summary}`\n") |
| 92 | + |
| 93 | + if not stale: |
| 94 | + print("PR base freshness OK.") |
| 95 | + return 0 |
| 96 | + |
| 97 | + reasons = [] |
| 98 | + if commits_exceeded: |
| 99 | + reasons.append(f"{commits_behind} commits behind (limit {commits_limit})") |
| 100 | + if age_exceeded: |
| 101 | + reasons.append(f"base is {age_days:.1f} days old (limit {age_limit_days})") |
| 102 | + reason_str = "; ".join(reasons) |
| 103 | + |
| 104 | + guidance = ( |
| 105 | + "To resolve: rebase onto the target branch (preferred) or merge it into this " |
| 106 | + "branch, then push again." |
| 107 | + ) |
| 108 | + |
| 109 | + if not enforce: |
| 110 | + print( |
| 111 | + f"::warning::[warn-only] PR base is stale: {reason_str}. " |
| 112 | + "This check will start blocking merges once enforcement is enabled. " |
| 113 | + f"{guidance}" |
| 114 | + ) |
| 115 | + return 0 |
| 116 | + |
| 117 | + print(f"::error::PR base is stale: {reason_str}. {guidance}") |
| 118 | + return 1 |
| 119 | + |
| 120 | + |
| 121 | +if __name__ == "__main__": |
| 122 | + sys.exit(main()) |
0 commit comments