github: Fix release automation /branch command with new repo
authorTom Stellard <tstellar@redhat.com>
Tue, 26 Jul 2022 22:05:03 +0000 (15:05 -0700)
committerTom Stellard <tstellar@redhat.com>
Tue, 26 Jul 2022 22:05:05 +0000 (15:05 -0700)
We started using the llvm/llvm-project-release-prs repo for
backport pull requests, but since this repo is not a fork of
llvm/llvm-project it will reject pull requests from other repos. In
order to fix this, when ever someone uses the /branch command to request
a branch be merged into the release branch, we first copy the branch to
the llvm-project-release-prs repo and then create the pull request.

Reviewed By: thieta

Differential Revision: https://reviews.llvm.org/D126940

llvm/utils/git/github-automation.py

index 232a12e..3862ade 100755 (executable)
@@ -14,6 +14,7 @@ import github
 import os
 import re
 import sys
+import time
 from typing import *
 
 class IssueSubscriber:
@@ -224,7 +225,29 @@ class ReleaseWorkflow:
         release_branch_for_issue = self.release_branch_for_issue
         if release_branch_for_issue is None:
             return False
-        head = f"{owner}:{branch}"
+        head_branch = branch
+        if not repo.fork:
+            # If the target repo is not a fork of llvm-project, we need to copy
+            # the branch into the target repo.  GitHub only supports cross-repo pull
+            # requests on forked repos.
+            head_branch = f'{owner}-{branch}'
+            local_repo = Repo(self.llvm_project_dir)
+            push_done = False
+            for i in range(0,5):
+                try:
+                    local_repo.git.fetch(f'https://github.com/{owner}/llvm-project', f'{branch}:{branch}')
+                    local_repo.git.push(self.push_url, f'{branch}:{head_branch}', force=True)
+                    push_done = True
+                    break
+                except Exception as e:
+                    print(e)
+                    time.sleep(30)
+                    continue
+            if not push_done:
+                raise Exception("Failed to mirror branch into {}".format(self.push_url))
+            owner = repo.owner.login
+
+        head = f"{owner}:{head_branch}"
         if self.check_if_pull_request_exists(repo, head):
             print("PR already exists...")
             return True