Skip to content

Commit

Permalink
Cruft PRs: wait for fork creation (#202)
Browse files Browse the repository at this point in the history
  • Loading branch information
flying-sheep authored and grst committed Jun 6, 2023
1 parent 1929d97 commit e03a54d
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 3 deletions.
3 changes: 2 additions & 1 deletion scripts/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ line-length = 120
[tool.ruff]
target-version = "py311"
line-length = 120
allowed-confusables = [""]
allowed-confusables = ["", "×"]
select = [
"A",
"ARG",
Expand Down Expand Up @@ -96,6 +96,7 @@ select = [
ignore = [
"S101", # assert should be allowed
"S603", # subprocess with shell=False should be allowed
"S311", # we don’t need cryptographically secure RNG
]
unfixable = ["RUF001"] # never “fix” “confusables”

Expand Down
23 changes: 23 additions & 0 deletions scripts/src/scverse_template_scripts/backoff.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import random
import time
from collections.abc import Callable
from typing import TypeVar

T = TypeVar("T")


def retry_with_backoff(
fn: Callable[[], T],
retries: int = 5,
backoff_in_seconds: int | float = 1,
exc_cls: type = Exception,
) -> T:
exc = None
for x in range(retries):
try:
return fn()
except exc_cls as _exc:
exc = _exc
sleep = backoff_in_seconds * 2**x + random.uniform(0, 1)
time.sleep(sleep)
raise exc
14 changes: 12 additions & 2 deletions scripts/src/scverse_template_scripts/cruft_prs.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
Uses `template-repos.yml` from `scverse/ecosystem-packages`.
"""

import math
import os
import sys
from collections.abc import Generator
Expand All @@ -17,13 +18,15 @@
from furl import furl
from git.repo import Repo
from git.util import Actor
from github import ContentFile, Github
from github import ContentFile, Github, UnknownObjectException
from github.GitRelease import GitRelease as GHRelease
from github.NamedUser import NamedUser
from github.PullRequest import PullRequest
from github.Repository import Repository as GHRepo
from yaml import safe_load

from .backoff import retry_with_backoff

log = getLogger(__name__)

PR_BODY_TEMPLATE = """\
Expand Down Expand Up @@ -163,10 +166,17 @@ def cruft_update(con: GitHubConnection, repo: GHRepo, path: Path, pr: PR) -> boo
return True


# GitHub says that up to 5 minutes of wait are OK,
# So we error our once we wait longer, i.e. when 2ⁿ = 5 min × 60 sec/min
n_retries = math.ceil(math.log(5 * 60) / math.log(2)) # = ⌈~8.22⌉ = 9
# Due to exponential backoff, we’ll maximally wait 2⁹ sec, or 8.5 min


def get_fork(con: GitHubConnection, repo: GHRepo) -> GHRepo:
if fork := next((f for f in repo.get_forks() if f.owner.id == con.user.id), None):
return fork
return repo.create_fork()
fork = repo.create_fork()
return retry_with_backoff(lambda: con.gh.get_repo(fork.id), retries=n_retries, exc_cls=UnknownObjectException)


def make_pr(con: GitHubConnection, release: GHRelease, repo_url: str) -> None:
Expand Down

0 comments on commit e03a54d

Please sign in to comment.