Skip to content

Commit

Permalink
Skip PR if already exists for current version.
Browse files Browse the repository at this point in the history
This allows to re-run the entire action on failures
without creating noise in the repos where the update
was successful.
  • Loading branch information
grst committed Aug 9, 2023
1 parent ac28931 commit fccd4c3
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions scripts/src/scverse_template_scripts/cruft_prs.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,15 @@ def body(self) -> str:
template_usage="https://cookiecutter-scverse-instance.readthedocs.io/en/latest/template_usage.html",
)

def matches(self, pr: PullRequest) -> bool:
def matches_prefix(self, pr: PullRequest) -> bool:
"""Check if `pr` is either a current or previous template update PR by matching the branch name"""
# Don’t compare title prefix, people might rename PRs
return pr.head.ref.startswith(self.branch_prefix) and pr.user.id == self.con.user.id

def matches_current_version(self, pr: PullRequest) -> bool:
"""Check if `pr` is a template update PR for the current version"""
return pr.head.ref == self.branch and pr.user.id == self.con.user.id


class RepoInfo(TypedDict):
url: str
Expand Down Expand Up @@ -217,6 +222,11 @@ def make_pr(con: GitHubConnection, release: GHRelease, repo_url: str) -> None:
# create fork, populate branch, do PR from it
origin = con.gh.get_repo(repo_url.removeprefix("https://github.com/"))
repo = get_fork(con, origin)

if old_pr := next((p for p in origin.get_pulls("open") if pr.matches_current_version(p)), None):
log.info(f"PR for current version already exists: #{pr.number} with branch name `{pr.head.ref}`. Skipping.")
return

with TemporaryDirectory() as td:
updated = cruft_update(
con,
Expand All @@ -227,7 +237,8 @@ def make_pr(con: GitHubConnection, release: GHRelease, repo_url: str) -> None:
path=Path(td),
)
if updated:
if old_pr := next((p for p in origin.get_pulls("open") if pr.matches(p)), None):
if old_pr := next((p for p in origin.get_pulls("open") if pr.matches_prefix(p)), None):
log.info(f"Closing old PR #{old_pr.number} with branch name `{pr.head.ref}`.")
old_pr.edit(state="closed")
origin.create_pull(pr.title, pr.body, origin.default_branch, pr.namespaced_head)

Expand Down

0 comments on commit fccd4c3

Please sign in to comment.