@@ -110,10 +110,15 @@ def body(self) -> str:
110110 template_usage = "https://cookiecutter-scverse-instance.readthedocs.io/en/latest/template_usage.html" ,
111111 )
112112
113- def matches (self , pr : PullRequest ) -> bool :
113+ def matches_prefix (self , pr : PullRequest ) -> bool :
114+ """Check if `pr` is either a current or previous template update PR by matching the branch name"""
114115 # Don’t compare title prefix, people might rename PRs
115116 return pr .head .ref .startswith (self .branch_prefix ) and pr .user .id == self .con .user .id
116117
118+ def matches_current_version (self , pr : PullRequest ) -> bool :
119+ """Check if `pr` is a template update PR for the current version"""
120+ return pr .head .ref == self .branch and pr .user .id == self .con .user .id
121+
117122
118123class RepoInfo (TypedDict ):
119124 url : str
@@ -217,6 +222,11 @@ def make_pr(con: GitHubConnection, release: GHRelease, repo_url: str) -> None:
217222 # create fork, populate branch, do PR from it
218223 origin = con .gh .get_repo (repo_url .removeprefix ("https://github.com/" ))
219224 repo = get_fork (con , origin )
225+
226+ if old_pr := next ((p for p in origin .get_pulls ("open" ) if pr .matches_current_version (p )), None ):
227+ log .info (f"PR for current version already exists: #{ pr .number } with branch name `{ pr .head .ref } `. Skipping." )
228+ return
229+
220230 with TemporaryDirectory () as td :
221231 updated = cruft_update (
222232 con ,
@@ -227,7 +237,8 @@ def make_pr(con: GitHubConnection, release: GHRelease, repo_url: str) -> None:
227237 path = Path (td ),
228238 )
229239 if updated :
230- if old_pr := next ((p for p in origin .get_pulls ("open" ) if pr .matches (p )), None ):
240+ if old_pr := next ((p for p in origin .get_pulls ("open" ) if pr .matches_prefix (p )), None ):
241+ log .info (f"Closing old PR #{ old_pr .number } with branch name `{ pr .head .ref } `." )
231242 old_pr .edit (state = "closed" )
232243 origin .create_pull (pr .title , pr .body , origin .default_branch , pr .namespaced_head )
233244
0 commit comments