|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# Copyright 2026 Google LLC |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | + |
| 16 | +"""Helper script for ADK PR Triage verification and remote update.""" |
| 17 | + |
| 18 | +from __future__ import annotations |
| 19 | + |
| 20 | +import argparse |
| 21 | +import json |
| 22 | +import subprocess |
| 23 | +import sys |
| 24 | + |
| 25 | + |
| 26 | +def run_command(args: list[str]) -> tuple[int, str, str]: |
| 27 | + """Runs a shell command and returns its exit code, stdout, and stderr.""" |
| 28 | + try: |
| 29 | + res = subprocess.run(args, capture_output=True, text=True, check=False) |
| 30 | + return res.returncode, res.stdout.strip(), res.stderr.strip() |
| 31 | + except Exception as e: |
| 32 | + return -1, "", str(e) |
| 33 | + |
| 34 | + |
| 35 | +def verify_cla(pr_number: str) -> bool: |
| 36 | + """Verifies if the Google CLA is signed for the given PR number.""" |
| 37 | + print(f"[*] Fetching status checks for PR #{pr_number}...") |
| 38 | + cmd = [ |
| 39 | + "gh", |
| 40 | + "pr", |
| 41 | + "view", |
| 42 | + pr_number, |
| 43 | + "--repo", |
| 44 | + "google/adk-python", |
| 45 | + "--json", |
| 46 | + "statusCheckRollup", |
| 47 | + ] |
| 48 | + code, stdout, stderr = run_command(cmd) |
| 49 | + if code != 0: |
| 50 | + print( |
| 51 | + f"Error: Failed to fetch PR details from GitHub: {stderr}", |
| 52 | + file=sys.stderr, |
| 53 | + ) |
| 54 | + sys.exit(1) |
| 55 | + |
| 56 | + try: |
| 57 | + data = json.loads(stdout) |
| 58 | + except json.JSONDecodeError: |
| 59 | + print("Error: Failed to parse GitHub API JSON response.", file=sys.stderr) |
| 60 | + sys.exit(1) |
| 61 | + |
| 62 | + status_checks = data.get("statusCheckRollup", []) |
| 63 | + cla_check = None |
| 64 | + for check in status_checks: |
| 65 | + if check.get("name") == "cla/google": |
| 66 | + cla_check = check |
| 67 | + break |
| 68 | + |
| 69 | + if not cla_check: |
| 70 | + print("\n" + "=" * 80) |
| 71 | + print("🚨 CRITICAL COMPLIANCE REFUSAL: GOOGLE CLA NOT SIGNED/VERIFIED 🚨") |
| 72 | + print("=" * 80) |
| 73 | + print( |
| 74 | + "Error: The mandatory 'cla/google' status check is completely missing" |
| 75 | + " on GitHub." |
| 76 | + ) |
| 77 | + print( |
| 78 | + "The contributor HAS NOT signed the Google Contributor License" |
| 79 | + " Agreement." |
| 80 | + ) |
| 81 | + print( |
| 82 | + "Legal policy strictly prohibits triaging, downloading, or reviewing" |
| 83 | + " this PR." |
| 84 | + ) |
| 85 | + print("=" * 80 + "\n") |
| 86 | + return False |
| 87 | + |
| 88 | + conclusion = cla_check.get("conclusion") |
| 89 | + if conclusion != "SUCCESS": |
| 90 | + print("\n" + "=" * 80) |
| 91 | + print("🚨 CRITICAL COMPLIANCE REFUSAL: GOOGLE CLA NOT SIGNED/VERIFIED 🚨") |
| 92 | + print("=" * 80) |
| 93 | + print( |
| 94 | + "Error: The 'cla/google' status check has the status:" |
| 95 | + f" '{conclusion or 'UNKNOWN'}'." |
| 96 | + ) |
| 97 | + print( |
| 98 | + "The contributor HAS NOT successfully signed or verified the Google" |
| 99 | + " CLA." |
| 100 | + ) |
| 101 | + print( |
| 102 | + "Legal policy strictly prohibits triaging, downloading, or reviewing" |
| 103 | + " this PR." |
| 104 | + ) |
| 105 | + print("=" * 80 + "\n") |
| 106 | + return False |
| 107 | + |
| 108 | + print("✅ Google CLA is verified and signed (status SUCCESS).") |
| 109 | + return True |
| 110 | + |
| 111 | + |
| 112 | +def update_pr_branch(pr_number: str) -> None: |
| 113 | + """Updates the remote PR branch with the latest changes from the base branch.""" |
| 114 | + print( |
| 115 | + f"\n[*] Attempting to update PR #{pr_number} branch via remote REBASE..." |
| 116 | + ) |
| 117 | + rebase_cmd = [ |
| 118 | + "gh", |
| 119 | + "pr", |
| 120 | + "update-branch", |
| 121 | + pr_number, |
| 122 | + "--rebase", |
| 123 | + "--repo", |
| 124 | + "google/adk-python", |
| 125 | + ] |
| 126 | + code, stdout, stderr = run_command(rebase_cmd) |
| 127 | + if code == 0: |
| 128 | + print( |
| 129 | + "✅ Successfully updated PR branch on GitHub by rebasing onto base" |
| 130 | + " branch!" |
| 131 | + ) |
| 132 | + if stdout: |
| 133 | + print(stdout) |
| 134 | + return |
| 135 | + |
| 136 | + print(f"Warning: Remote rebase-update failed: {stderr}") |
| 137 | + print("[*] Falling back to standard remote MERGE commit update...") |
| 138 | + |
| 139 | + merge_cmd = [ |
| 140 | + "gh", |
| 141 | + "pr", |
| 142 | + "update-branch", |
| 143 | + pr_number, |
| 144 | + "--repo", |
| 145 | + "google/adk-python", |
| 146 | + ] |
| 147 | + code, stdout, stderr = run_command(merge_cmd) |
| 148 | + if code == 0: |
| 149 | + print( |
| 150 | + "✅ Successfully updated PR branch on GitHub via standard merge commit!" |
| 151 | + ) |
| 152 | + if stdout: |
| 153 | + print(stdout) |
| 154 | + return |
| 155 | + |
| 156 | + print( |
| 157 | + "\n[!] Warning: Remote branch update failed completely on GitHub server:" |
| 158 | + f" {stderr}" |
| 159 | + ) |
| 160 | + print(" This is typical if edits are disabled on the contributor's fork.") |
| 161 | + print( |
| 162 | + " No worries! We will automatically rebase locally after checking out." |
| 163 | + ) |
| 164 | + |
| 165 | + |
| 166 | +def main() -> None: |
| 167 | + parser = argparse.ArgumentParser( |
| 168 | + description="Triage PR verification and sync helper." |
| 169 | + ) |
| 170 | + parser.add_argument( |
| 171 | + "pr_number", help="The GitHub Pull Request number (e.g. 5875)." |
| 172 | + ) |
| 173 | + parser.add_argument( |
| 174 | + "--skip-update", |
| 175 | + action="store_true", |
| 176 | + help="Skip updating the remote PR branch on GitHub.", |
| 177 | + ) |
| 178 | + args = parser.parse_args() |
| 179 | + |
| 180 | + # Step 1: Verify CLA |
| 181 | + if not verify_cla(args.pr_number): |
| 182 | + sys.exit(2) # Exit code 2 indicates compliance refusal |
| 183 | + |
| 184 | + # Step 2: Update branch |
| 185 | + if not args.skip_update: |
| 186 | + update_pr_branch(args.pr_number) |
| 187 | + |
| 188 | + print("\n[*] Verification complete. Safe to proceed with checkout.") |
| 189 | + sys.exit(0) |
| 190 | + |
| 191 | + |
| 192 | +if __name__ == "__main__": |
| 193 | + main() |
0 commit comments