@@ -145,7 +145,119 @@ async def fetch_files(client, owner, repo_name, pr_url, file_cursor):
145145 return page_info , files
146146
147147
148+ import github
149+ from github import Github
150+ from github import GithubException
151+
152+ import snoop
153+
154+
155+ def sync_fork (upstream , fork , branch ):
156+ try :
157+ print (f"Successfully synced { fork } branch { branch } with { upstream } " )
158+ except Exception as error :
159+ raise Exception (
160+ f"Unable to sync { fork } with { upstream } due to { error } "
161+ ) from error
162+
163+
164+ @snoop
165+ def make_github_operations (
166+ token , repo_urls , new_branch_name , transform_file_content
167+ ):
168+ g = Github (token )
169+ user = g .get_user ()
170+
171+ for repo_url in repo_urls :
172+ try :
173+ # Fork the repository
174+ org , repo_name = repo_url .split ("/" )[- 2 :]
175+ repo = g .get_repo (f"{ org } /{ repo_name } " )
176+ fork = user .create_fork (repo )
177+
178+ # Create a new branch from the default branch
179+ fork_default_branch = fork .get_branch (fork .default_branch )
180+ try :
181+ fork .create_git_ref (
182+ ref = "refs/heads/" + new_branch_name ,
183+ sha = fork_default_branch .commit .sha ,
184+ )
185+ except github .GithubException as error :
186+ if "Reference already exists" not in str (error ):
187+ raise
188+
189+ # Get the sha of the last commit to the branch in the upstream repo
190+ upstream_default_branch_sha = repo .get_branch (
191+ repo .default_branch
192+ ).commit .sha
193+
194+ # Get the git ref of the branch in the forked repo
195+ new_branch_ref = fork .get_git_ref (f"heads/{ new_branch_name } " )
196+
197+ # Update the git ref to point to the new sha
198+ new_branch_ref .edit (upstream_default_branch_sha , force = True )
199+
200+ # Update the contents of testing.yml
201+ file_path = ".github/workflows/testing.yml"
202+ try :
203+ pygithub_fileobj = fork .get_contents (
204+ file_path , ref = new_branch_name
205+ )
206+ except GithubException as error :
207+ raise Exception (
208+ f"{ file_path } does not exist in { repo_url } , unable to update."
209+ ) from error
210+
211+ # Transform the content
212+ content = transform_file_content (pygithub_fileobj .decoded_content )
213+
214+ # Update file content
215+ fork .update_file (
216+ pygithub_fileobj .path ,
217+ "Update testing.yml" ,
218+ content ,
219+ pygithub_fileobj .sha ,
220+ branch = new_branch_name ,
221+ )
222+
223+ # Create Pull request
224+ base_repo = repo
225+ snoop .pp (fork )
226+ base = base_repo .default_branch
227+ head = f"{ user .login } :{ fork .name } :{ new_branch_name } "
228+ base_repo .create_pull (
229+ title = "Update testing.yml" , body = "" , head = head , base = base
230+ )
231+
232+ except GithubException as e :
233+ print (
234+ f"Unable to complete operations for { repo_url } due to { str (e )} "
235+ )
236+
237+
238+ import pathlib
239+
240+
241+ repo_urls = list (["/" .join (["https://github.com" , * repo ]) for repo in REPOS ])
242+ token = TOKEN
243+ new_branch_name = "update-testing-workflow"
244+ file_content = (
245+ pathlib .Path (__file__ )
246+ .parents [3 ]
247+ .joinpath (
248+ ".github" ,
249+ "workflows" ,
250+ "testing.yml" ,
251+ )
252+ .read_text ()
253+ )
254+
255+
148256async def main (repos , file_name ):
257+ make_github_operations (
258+ token , repo_urls , new_branch_name , lambda content : content .upper ()
259+ )
260+ return
149261 async with aiohttp .ClientSession (trust_env = True ) as client :
150262 tasks = [
151263 fetch_pull_requests (client , owner , repo , file_name )
0 commit comments