Skip to content

Commit b33af45

Browse files
Merge pull request #82 from moevm/77-Created-GitHubRepoAPI-class
77 created class GitHubRepoAPI
2 parents 79006b4 + 2dd1d2b commit b33af45

File tree

1 file changed

+167
-0
lines changed

1 file changed

+167
-0
lines changed

GitHubRepoAPI.py

Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
from interface_wrapper import (
2+
logging,
3+
Repository,
4+
Contributor,
5+
Commit,
6+
Issue,
7+
PullRequest,
8+
WikiPage,
9+
Branch,
10+
IRepositoryAPI,
11+
)
12+
from github import Github
13+
14+
class GitHubRepoAPI(IRepositoryAPI):
15+
16+
def __init__(self, client):
17+
self.client = client
18+
19+
def get_repository(self, id: str) -> Repository | None:
20+
try:
21+
repo = self.client.get_repo(id)
22+
return Repository(_id=repo.full_name, name=repo.name, url=repo.html_url)
23+
except Exception as e:
24+
logging.error(f"Failed to get repository {id} from GitHub: {e}")
25+
return None
26+
27+
def get_commits(self, repo: Repository) -> list[Commit]:
28+
try:
29+
commits = self.client.get_repo(repo._id).get_commits()
30+
return [
31+
Commit(
32+
_id=c.sha,
33+
message=c.commit.message,
34+
author=Contributor(c.author.login if c.author else "unknown", c.commit.author.email),
35+
date=c.commit.author.date
36+
) for c in commits
37+
]
38+
except Exception as e:
39+
logging.error(f"Failed to get commits from GitHub for repo {repo.name}: {e}")
40+
return []
41+
42+
def get_contributors(self, repo: Repository) -> list[Contributor]:
43+
try:
44+
contributors = self.client.get_repo(repo._id).get_contributors()
45+
return [Contributor(c.login, c.email or "") for c in contributors]
46+
except Exception as e:
47+
logging.error(f"Failed to get contributors from GitHub for repo {repo.name}: {e}")
48+
return []
49+
50+
def get_issues(self, repo: Repository) -> list[Issue]:
51+
try:
52+
issues = self.client.get_repo(repo._id).get_issues(state='all')
53+
return [
54+
Issue(
55+
_id=i.number,
56+
title=i.title,
57+
author=Contributor(i.user.login, i.user.email or ""),
58+
state=i.state
59+
) for i in issues
60+
]
61+
except Exception as e:
62+
logging.error(f"Failed to get issues from GitHub for repo {repo.name}: {e}")
63+
return []
64+
65+
def get_pull_requests(self, repo: Repository) -> list[PullRequest]:
66+
try:
67+
pulls = self.client.get_repo(repo._id).get_pulls(state='all')
68+
return [
69+
PullRequest(
70+
_id=p.number,
71+
title=p.title,
72+
author=Contributor(p.user.login, p.user.email or ""),
73+
state=p.state
74+
) for p in pulls
75+
]
76+
except Exception as e:
77+
logging.error(f"Failed to get pull requests from GitHub for repo {repo.name}: {e}")
78+
return []
79+
80+
def get_branches(self, repo: Repository) -> list[Branch]:
81+
try:
82+
repo_client = self.client.get_repo(repo._id)
83+
branches = repo_client.get_branches()
84+
result = []
85+
86+
for branch in branches:
87+
commit = repo_client.get_commit(branch.commit.sha)
88+
89+
90+
author = commit.author
91+
contributor = Contributor(
92+
username=author.login if author else "unknown",
93+
email=commit.commit.author.email or ""
94+
)
95+
96+
commit_obj = Commit(
97+
_id=commit.sha,
98+
message=commit.commit.message,
99+
author=contributor,
100+
date=commit.commit.author.date
101+
)
102+
103+
result.append(
104+
Branch(
105+
name=branch.name,
106+
last_commit=commit_obj
107+
)
108+
)
109+
110+
return result
111+
112+
except Exception as e:
113+
logging.error(f"Failed to get branches from GitHub for repo {repo.name}: {e}")
114+
return []
115+
116+
def get_wiki_pages(self, repo: Repository) -> list[WikiPage]:
117+
pass
118+
119+
120+
# Точка входа для тестирования
121+
if __name__ == "__main__":
122+
# Создайте клиент GitHub (используйте ваш токен)
123+
client = Github("tocken")
124+
api = GitHubRepoAPI(client)
125+
126+
# Укажите ваш репозиторий
127+
repo_name = ""
128+
129+
# Получение репозитория
130+
repo = api.get_repository(repo_name)
131+
if not repo:
132+
print("Repository not found.")
133+
exit()
134+
135+
# Вывод информации о репозитории
136+
print(f"Repository: {repo.name}, URL: {repo.url}")
137+
138+
# Получение коммитов
139+
commits = api.get_commits(repo)
140+
print(f"Total commits: {len(commits)}")
141+
for commit in commits[:10]: # Выведем первые 10 коммитов
142+
print(f"Commit: {commit._id}, Message: {commit.message}, Author: {commit.author.username}")
143+
144+
# Получение контрибьюторов
145+
contributors = api.get_contributors(repo)
146+
print(f"Total contributors: {len(contributors)}")
147+
for contributor in contributors:
148+
print(f"Contributor: {contributor.username}, Email: {contributor.email}")
149+
150+
# Получение issues
151+
issues = api.get_issues(repo)
152+
print(f"Total issues: {len(issues)}")
153+
for issue in issues[:10]: # Выведем первые 10 issues
154+
print(f"Issue: {issue._id}, Title: {issue.title}, State: {issue.state}")
155+
156+
# Получение pull requests
157+
pulls = api.get_pull_requests(repo)
158+
print(f"Total pull requests: {len(pulls)}")
159+
for pull in pulls[:10]: # Выведем первые 10 pull requests
160+
print(f"Pull Request: {pull._id}, Title: {pull.title}, State: {pull.state}")
161+
162+
163+
# Получение веток
164+
branches = api.get_branches(repo)
165+
print(f"Total branches: {len(branches)}")
166+
for branch in branches:
167+
print(f"Branch: {branch.name}, Last Commit: {branch.last_commit._id if branch.last_commit else 'None'}")

0 commit comments

Comments
 (0)