Skip to content

Commit 7ef5da8

Browse files
committed
Use PyGithub to resolve repository from CLI params
1 parent 101cf80 commit 7ef5da8

2 files changed

Lines changed: 35 additions & 3 deletions

File tree

cookie_python/manage/github.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import contextlib
2+
import os
3+
from functools import cached_property, lru_cache
4+
5+
from github import Github
6+
from github.Repository import Repository
7+
8+
9+
class GithubRepo:
10+
def __init__(self) -> None:
11+
self._gh = Github(os.environ["GITHUB_ACCESS_TOKEN"])
12+
13+
@cached_property
14+
def username(self) -> str:
15+
return self._gh.get_user().login
16+
17+
@lru_cache # noqa: B019
18+
def find_repo(self, search: str) -> Repository:
19+
if "/" not in search:
20+
search = f"{self.username}/{search}"
21+
if "github.com" in search:
22+
with contextlib.suppress(IndexError):
23+
search = os.path.splitext(search.split(":")[1])[0]
24+
return self._gh.get_repo(search)

cookie_python/manage/repo.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,13 @@
1313

1414
import loguru
1515

16+
from .github import GithubRepo
17+
1618

1719
class RepoSandbox:
1820
def __init__(self, repo: str, dry_run: bool = False) -> None:
1921
self._stack = contextlib.ExitStack()
20-
self.repo = repo
22+
self.repo = self.gh.find_repo(repo)
2123
self.branch = "update-cookie"
2224
self.dry_run = dry_run
2325

@@ -32,9 +34,13 @@ def __exit__(
3234
) -> None:
3335
self._stack.close()
3436

37+
@cached_property
38+
def gh(self) -> GithubRepo:
39+
return GithubRepo()
40+
3541
@cached_property
3642
def logger(self) -> "loguru.Logger":
37-
return loguru.logger.bind(repo=self.repo)
43+
return loguru.logger.bind(repo=self.repo.full_name)
3844

3945
@cached_property
4046
def tempdir(self) -> Path:
@@ -47,7 +53,9 @@ def tempdir(self) -> Path:
4753
@cached_property
4854
def clone_path(self) -> Path:
4955
subprocess.run(
50-
["git", "clone", self.repo, "repo"], cwd=self.tempdir, check=True
56+
["git", "clone", self.repo.ssh_url, "repo"],
57+
cwd=self.tempdir,
58+
check=True,
5159
)
5260
clone_path = self.tempdir / "repo"
5361
for cmd in (

0 commit comments

Comments
 (0)