Skip to content

Commit

Permalink
Fetch main branch from upstream
Browse files Browse the repository at this point in the history
  • Loading branch information
grst committed Aug 4, 2023
1 parent cb46a7c commit 809e9aa
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 20 deletions.
31 changes: 13 additions & 18 deletions scripts/src/scverse_template_scripts/cruft_prs.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,13 +161,17 @@ def run_cruft(cwd: Path, git_tag: str, log_name: str) -> CompletedProcess:
# Due to exponential backoff, we’ll maximally wait 2⁹ sec, or 8.5 min


def cruft_update(con: GitHubConnection, tag_name: str, repo: GHRepo, path: Path, pr: PR) -> bool:
def cruft_update( # noqa: PLR0913
con: GitHubConnection, tag_name: str, repo: GHRepo, origin: GHRepo, path: Path, pr: PR
) -> bool:
clone = retry_with_backoff(
lambda: Repo.clone_from(con.auth(repo.clone_url), path),
retries=n_retries,
exc_cls=GitCommandError,
)
branch = clone.create_head(pr.branch, clone.active_branch)
upstream = clone.create_remote(name=pr.repo_id, url=origin.git_url)
upstream.fetch()
branch = clone.create_head(pr.branch, f"{pr.repo_id}/{origin.default_branch}")
branch.checkout()

run_cruft(path, tag_name, pr.branch)
Expand All @@ -189,21 +193,12 @@ def cruft_update(con: GitHubConnection, tag_name: str, repo: GHRepo, path: Path,
return True


def get_fork(con: GitHubConnection, repo: GHRepo, name: str) -> GHRepo:
if fork := next(
(f for f in repo.get_forks() if f.owner.id == con.user.id and f.name == name),
None,
):
log.info(f"Using existing fork for {repo} with name {fork.name}.")
log.debug(fork)
return fork
log.info(f"Creating fork for {repo} with name {name}")
fork = repo.create_fork(name=name)
return retry_with_backoff(
lambda: con.gh.get_repo(fork.id),
retries=n_retries,
exc_cls=UnknownObjectException,
)
def get_fork(con: GitHubConnection, repo: GHRepo) -> GHRepo:
"""Fork target repo into the scverse-bot namespace and wait until the fork has been created.
If the fork already exists it is reused.
"""
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 All @@ -215,7 +210,7 @@ def make_pr(con: GitHubConnection, release: GHRelease, repo_url: str) -> None:
origin = con.gh.get_repo(repo_url.removeprefix("https://github.com/"))
repo = get_fork(con, origin, repo_id)
with TemporaryDirectory() as td:
updated = cruft_update(con, release.tag_name, repo, Path(td), pr)
updated = cruft_update(con, release.tag_name, repo, origin, Path(td), pr)
if updated:
if old_pr := next((p for p in origin.get_pulls("open") if pr.matches(p)), None):
old_pr.edit(state="closed")
Expand Down
5 changes: 3 additions & 2 deletions scripts/tests/test_cruft.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
class MockGHRepo:
git_url: str # git://github.com/foo/bar.git
clone_url: str # https://github.com/foo/bar.git
default_branch: str # main


@dataclass
Expand All @@ -39,7 +40,7 @@ def repo(git_repo: GitRepo) -> GHRepo:
(git_repo.workspace / "b").write_text("b content")
git_repo.api.index.add(["a", "b"])
git_repo.api.index.commit("initial commit")
return cast(GHRepo, MockGHRepo(git_repo.uri, git_repo.uri))
return cast(GHRepo, MockGHRepo(git_repo.uri, git_repo.uri, "main"))


@pytest.fixture
Expand All @@ -53,7 +54,7 @@ def test_cruft_update(con, repo, tmp_path, pr, git_repo: GitRepo, monkeypatch: p
"scverse_template_scripts.cruft_prs.run_cruft",
lambda p, _, __: (p / "b").write_text("b modified"),
)
changed = cruft_update(con, "main", repo, tmp_path, pr)
changed = cruft_update(con, "main", repo, repo, tmp_path, pr)
assert changed # TODO: add test for short circuit
main_branch = git_repo.api.active_branch
assert main_branch.name == old_active_branch_name, "Shouldn’t change active branch"
Expand Down

0 comments on commit 809e9aa

Please sign in to comment.