From 53ae4f13dbbd2028528b681a5964ddd2619908b2 Mon Sep 17 00:00:00 2001 From: ammallya Date: Mon, 18 Aug 2025 13:00:02 -0700 Subject: [PATCH] Fix for commits --- .github/scripts/import_subrepo_prs.py | 102 ++++++++++++++++---------- 1 file changed, 62 insertions(+), 40 deletions(-) diff --git a/.github/scripts/import_subrepo_prs.py b/.github/scripts/import_subrepo_prs.py index d2cc1150f8..3b15fcd020 100644 --- a/.github/scripts/import_subrepo_prs.py +++ b/.github/scripts/import_subrepo_prs.py @@ -1,7 +1,7 @@ import os import sys import subprocess -from github import Github, GithubIntegration +from github import Github from git import Repo def run(cmd, **kwargs): @@ -9,74 +9,96 @@ def run(cmd, **kwargs): subprocess.check_call(cmd, shell=True, **kwargs) def main(): + # 1) Read and validate env vars + token = os.getenv("GITHUB_TOKEN") + repo_full= os.getenv("GITHUB_REPOSITORY") + prefix = os.getenv("SUBPREFIX") + subrepo = os.getenv("SUBREPO") + upstream = os.getenv("UPSTREAM") + target = os.getenv("TARGET", "develop") + pr_list = os.getenv("PR_LIST", "") + + if not all([token, repo_full, prefix, subrepo, upstream, pr_list]): + print("ERROR: Missing one or more required environment variables.") + sys.exit(1) + + pr_numbers = [p.strip() for p in pr_list.split(",") if p.strip()] + + # 2) Init local repo and configure Git user + repo = Repo(os.getcwd()) run("git config user.name 'systems-assistant[bot]'") run("git config user.email 'systems-assistant[bot]@users.noreply.github.com'") - token = os.environ["GITHUB_TOKEN"] - prefix = os.environ["SUBPREFIX"] - subrepo = os.environ["SUBREPO"] - upstream = os.environ["UPSTREAM"] - target = os.environ["TARGET"] - pr_list = os.environ["PR_LIST"].split(",") - repo_path = os.getcwd() - repo = Repo(repo_path) + # 3) Init GitHub clients gh = Github(token) - super_repo = gh.get_repo(os.environ["GITHUB_REPOSITORY"]) + super_repo = gh.get_repo(repo_full) sub_repo = gh.get_repo(upstream) - # Ensure we have target branch locally + # 4) Ensure target branch is checked out run(f"git fetch origin {target}") - run(f"git checkout {target}") + try: + run(f"git checkout {target}") + except subprocess.CalledProcessError: + run(f"git checkout -b {target} origin/{target}") - for pr_num in pr_list: - pr_num = pr_num.strip() + # 5) Loop over each PR + for pr_num in pr_numbers: + print(f"\n=== Importing PR #{pr_num} ===") pr = sub_repo.get_pull(int(pr_num)) - title = pr.title - body = pr.body or "" - head_ref= pr.head.ref - head_url= pr.head.repo.clone_url - is_draft= pr.draft - author = pr.user.login + title = pr.title + body = pr.body or "" + head_ref = pr.head.ref + head_url = pr.head.repo.clone_url + is_draft = pr.draft + author = pr.user.login - # Create a sanitized branch name - tclean = target.replace("/", "_") - src_clean = subrepo.replace("/", "_") - branch = f"import/{tclean}/{src_clean}/pr-{pr_num}" + # Build a clean branch name + tclean = target.replace("/", "_") + src_clean= subrepo.replace("/", "_") + branch = f"import/{tclean}/{src_clean}/pr-{pr_num}" - # Checkout new branch - run(f"git checkout -b {branch}") + # 5a) Checkout new branch (delete/recreate if exists) + try: + run(f"git checkout -b {branch}") + except subprocess.CalledProcessError: + run(f"git branch -D {branch}") + run(f"git checkout -b {branch}") - # Pull in the subtree from the PR head - run(f"git subtree pull --prefix={prefix} {head_url} {head_ref}") + # 5b) Pull in the subtree + try: + run(f"git subtree pull --prefix={prefix} {head_url} {head_ref}") + except subprocess.CalledProcessError: + print(f"ERROR: subtree pull failed for PR #{pr_num}, skipping.") + run(f"git checkout {target}") + continue - # Push the branch + # 5c) Push the branch up run(f"git push origin {branch}") - # Build PR body + # 5d) Build the PR body with footer footer = ( - f"\n\n---\n" - f"๐Ÿ” Imported from [{upstream}#{pr_num}](https://github.com/{upstream}/pull/{pr_num})\n" + "\n\n---\n" + f"๐Ÿ” Imported from [{upstream}#{pr_num}]" + f"(https://github.com/{upstream}/pull/{pr_num})\n" f"๐Ÿง‘โ€๐Ÿ’ป Originally authored by @{author}" ) full_body = body + footer - # Create the PR on the superโ€repo - super_repo.create_pull( + # 5e) Create the PR and label it + new_pr = super_repo.create_pull( title=title, body=full_body, head=branch, base=target, draft=is_draft, ) + new_pr.add_to_labels("imported pr") - # label it - pr_created = super_repo.get_pulls(head=f"{repo.remote().url.split('/')[-1].replace('.git','')}:{branch}", - state="open")[0] - pr_created.add_to_labels("imported pr") - - # checkout back to target before next iteration + # 5f) Go back to target for next iteration run(f"git checkout {target}") + print("\nAll imports complete.") + if __name__ == "__main__": main()