Skip to content

Commit 809e9aa

Browse files
committed
Fetch main branch from upstream
1 parent cb46a7c commit 809e9aa

2 files changed

Lines changed: 16 additions & 20 deletions

File tree

scripts/src/scverse_template_scripts/cruft_prs.py

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -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

209204
def 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")

scripts/tests/test_cruft.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
class MockGHRepo:
1616
git_url: str # git://github.com/foo/bar.git
1717
clone_url: str # https://github.com/foo/bar.git
18+
default_branch: str # main
1819

1920

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

4445

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

0 commit comments

Comments
 (0)