@@ -161,13 +161,17 @@ def run_cruft(cwd: Path, git_tag: str, log_name: str) -> CompletedProcess:
161161# Due to exponential backoff, we’ll maximally wait 2⁹ sec, or 8.5 min
162162
163163
164- def cruft_update (con : GitHubConnection , tag_name : str , repo : GHRepo , path : Path , pr : PR ) -> bool :
164+ def cruft_update ( # noqa: PLR0913
165+ con : GitHubConnection , tag_name : str , repo : GHRepo , origin : GHRepo , path : Path , pr : PR
166+ ) -> bool :
165167 clone = retry_with_backoff (
166168 lambda : Repo .clone_from (con .auth (repo .clone_url ), path ),
167169 retries = n_retries ,
168170 exc_cls = GitCommandError ,
169171 )
170- branch = clone .create_head (pr .branch , clone .active_branch )
172+ upstream = clone .create_remote (name = pr .repo_id , url = origin .git_url )
173+ upstream .fetch ()
174+ branch = clone .create_head (pr .branch , f"{ pr .repo_id } /{ origin .default_branch } " )
171175 branch .checkout ()
172176
173177 run_cruft (path , tag_name , pr .branch )
@@ -189,21 +193,12 @@ def cruft_update(con: GitHubConnection, tag_name: str, repo: GHRepo, path: Path,
189193 return True
190194
191195
192- def get_fork (con : GitHubConnection , repo : GHRepo , name : str ) -> GHRepo :
193- if fork := next (
194- (f for f in repo .get_forks () if f .owner .id == con .user .id and f .name == name ),
195- None ,
196- ):
197- log .info (f"Using existing fork for { repo } with name { fork .name } ." )
198- log .debug (fork )
199- return fork
200- log .info (f"Creating fork for { repo } with name { name } " )
201- fork = repo .create_fork (name = name )
202- return retry_with_backoff (
203- lambda : con .gh .get_repo (fork .id ),
204- retries = n_retries ,
205- exc_cls = UnknownObjectException ,
206- )
196+ def get_fork (con : GitHubConnection , repo : GHRepo ) -> GHRepo :
197+ """Fork target repo into the scverse-bot namespace and wait until the fork has been created.
198+ If the fork already exists it is reused.
199+ """
200+ fork = repo .create_fork ()
201+ return retry_with_backoff (lambda : con .gh .get_repo (fork .id ), retries = n_retries , exc_cls = UnknownObjectException )
207202
208203
209204def make_pr (con : GitHubConnection , release : GHRelease , repo_url : str ) -> None :
@@ -215,7 +210,7 @@ def make_pr(con: GitHubConnection, release: GHRelease, repo_url: str) -> None:
215210 origin = con .gh .get_repo (repo_url .removeprefix ("https://github.com/" ))
216211 repo = get_fork (con , origin , repo_id )
217212 with TemporaryDirectory () as td :
218- updated = cruft_update (con , release .tag_name , repo , Path (td ), pr )
213+ updated = cruft_update (con , release .tag_name , repo , origin , Path (td ), pr )
219214 if updated :
220215 if old_pr := next ((p for p in origin .get_pulls ("open" ) if pr .matches (p )), None ):
221216 old_pr .edit (state = "closed" )
0 commit comments