Skip to content

Commit 9404738

Browse files
authored
Merge pull request #106 from smkent/manage
Improve GitHub API/repo interactions in manage-cookie
2 parents 36c238b + c3a564f commit 9404738

2 files changed

Lines changed: 20 additions & 1 deletion

File tree

cookie_python/manage/repo.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from typing import Any
1313

1414
import loguru
15+
from github import GithubException
1516

1617
from .github import GithubRepo
1718

@@ -78,12 +79,19 @@ def cachedir(self) -> Path:
7879
@cached_property
7980
def clone_path(self) -> Path:
8081
subprocess.run(
81-
["git", "clone", self.repo.ssh_url, "repo"],
82+
["git", "clone", self.repo.html_url, "repo"],
8283
cwd=self.tempdir,
8384
check=True,
8485
)
8586
clone_path = self.tempdir / "repo"
8687
for cmd in (
88+
[
89+
"git",
90+
"config",
91+
"--local",
92+
"remote.origin.pushurl",
93+
self.repo.ssh_url,
94+
],
8795
["git", "checkout", "-b", self.branch],
8896
["git", "reset", "--hard", "origin/main"],
8997
):
@@ -154,7 +162,16 @@ def close_existing_pr(self) -> None:
154162
else:
155163
pr.edit(state="closed")
156164
self.logger.info(f"Closed existing PR {pr.url}")
165+
try:
166+
self.repo.get_branch(self.branch)
167+
except GithubException as e:
168+
if e.status == 404:
169+
return
170+
raise
157171
if self.dry_run:
172+
self.logger.info(
173+
f"Would delete existing remote branch {self.branch}"
174+
)
158175
return
159176
# Delete existing branch
160177
delete_result = self.run(

tests/test_manage_cookie.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,15 @@ def mock_pygithub(new_cookie: Path) -> Iterator[MagicMock]:
7171
name=PROJECT_NAME,
7272
full_name=f"{AUTHOR_NAME}/{PROJECT_NAME}",
7373
ssh_url=str(new_cookie),
74+
html_url=str(new_cookie),
7475
get_pulls=MagicMock(
7576
return_value=[
7677
SimpleNamespace(
7778
url="https://unittest.example.com/repo/pulls/1138"
7879
)
7980
],
8081
),
82+
get_branch=lambda name: SimpleNamespace(name=name),
8183
get_latest_release=lambda: SimpleNamespace(title="v1.1.38"),
8284
)
8385
yield obj

0 commit comments

Comments
 (0)