Skip to content

Commit

Permalink
Merge pull request #106 from smkent/manage
Browse files Browse the repository at this point in the history
Improve GitHub API/repo interactions in manage-cookie
  • Loading branch information
smkent authored Jul 9, 2024
2 parents 36c238b + c3a564f commit 9404738
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
19 changes: 18 additions & 1 deletion cookie_python/manage/repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from typing import Any

import loguru
from github import GithubException

from .github import GithubRepo

Expand Down Expand Up @@ -78,12 +79,19 @@ def cachedir(self) -> Path:
@cached_property
def clone_path(self) -> Path:
subprocess.run(
["git", "clone", self.repo.ssh_url, "repo"],
["git", "clone", self.repo.html_url, "repo"],
cwd=self.tempdir,
check=True,
)
clone_path = self.tempdir / "repo"
for cmd in (
[
"git",
"config",
"--local",
"remote.origin.pushurl",
self.repo.ssh_url,
],
["git", "checkout", "-b", self.branch],
["git", "reset", "--hard", "origin/main"],
):
Expand Down Expand Up @@ -154,7 +162,16 @@ def close_existing_pr(self) -> None:
else:
pr.edit(state="closed")
self.logger.info(f"Closed existing PR {pr.url}")
try:
self.repo.get_branch(self.branch)
except GithubException as e:
if e.status == 404:
return
raise
if self.dry_run:
self.logger.info(
f"Would delete existing remote branch {self.branch}"
)
return
# Delete existing branch
delete_result = self.run(
Expand Down
2 changes: 2 additions & 0 deletions tests/test_manage_cookie.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,15 @@ def mock_pygithub(new_cookie: Path) -> Iterator[MagicMock]:
name=PROJECT_NAME,
full_name=f"{AUTHOR_NAME}/{PROJECT_NAME}",
ssh_url=str(new_cookie),
html_url=str(new_cookie),
get_pulls=MagicMock(
return_value=[
SimpleNamespace(
url="https://unittest.example.com/repo/pulls/1138"
)
],
),
get_branch=lambda name: SimpleNamespace(name=name),
get_latest_release=lambda: SimpleNamespace(title="v1.1.38"),
)
yield obj
Expand Down

0 comments on commit 9404738

Please sign in to comment.