Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve GitHub API/repo interactions in manage-cookie #106

Merged
merged 3 commits into from
Jul 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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