-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithub.py
More file actions
41 lines (35 loc) · 1.17 KB
/
github.py
File metadata and controls
41 lines (35 loc) · 1.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import contextlib
import os
from functools import cached_property, lru_cache
import github
from github.PullRequest import PullRequest
from github.Repository import Repository
class GithubRepo:
def __init__(self) -> None:
self._gh = github.Github(os.environ["GITHUB_API_TOKEN"])
@cached_property
def username(self) -> str:
return self._gh.get_user().login
@lru_cache # noqa: B019
def find_repo(self, search: str) -> Repository:
if "/" not in search:
search = f"{self.username}/{search}"
if "github.com" in search:
with contextlib.suppress(IndexError):
search = os.path.splitext(search.split(":")[1])[0]
return self._gh.get_repo(search)
def find_pr(
self, repo: Repository, head: str, base: str = "main"
) -> PullRequest | None:
pulls = [
pr
for pr in repo.get_pulls(
head=f"{self.username}:{head}",
base=base, # noqa: E231
)
]
if not pulls:
return None
if len(pulls) > 1:
raise Exception(f"Multiple PRs found matching {head}")
return pulls[0]