Skip to content

Commit 973a94d

Browse files
committed
contributors parser implimented, black
1 parent 5deaad3 commit 973a94d

File tree

12 files changed

+354
-121
lines changed

12 files changed

+354
-121
lines changed

GitHubRepoAPI.py

Lines changed: 43 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@
1111
)
1212
from github import Github
1313

14+
1415
class GitHubRepoAPI(IRepositoryAPI):
15-
16+
1617
def __init__(self, client):
1718
self.client = client
1819

@@ -31,20 +32,27 @@ def get_commits(self, repo: Repository) -> list[Commit]:
3132
Commit(
3233
_id=c.sha,
3334
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
35+
author=Contributor(
36+
c.author.login if c.author else "unknown", c.commit.author.email
37+
),
38+
date=c.commit.author.date,
39+
)
40+
for c in commits
3741
]
3842
except Exception as e:
39-
logging.error(f"Failed to get commits from GitHub for repo {repo.name}: {e}")
43+
logging.error(
44+
f"Failed to get commits from GitHub for repo {repo.name}: {e}"
45+
)
4046
return []
4147

4248
def get_contributors(self, repo: Repository) -> list[Contributor]:
4349
try:
4450
contributors = self.client.get_repo(repo._id).get_contributors()
4551
return [Contributor(c.login, c.email or "") for c in contributors]
4652
except Exception as e:
47-
logging.error(f"Failed to get contributors from GitHub for repo {repo.name}: {e}")
53+
logging.error(
54+
f"Failed to get contributors from GitHub for repo {repo.name}: {e}"
55+
)
4856
return []
4957

5058
def get_issues(self, repo: Repository) -> list[Issue]:
@@ -55,8 +63,9 @@ def get_issues(self, repo: Repository) -> list[Issue]:
5563
_id=i.number,
5664
title=i.title,
5765
author=Contributor(i.user.login, i.user.email or ""),
58-
state=i.state
59-
) for i in issues
66+
state=i.state,
67+
)
68+
for i in issues
6069
]
6170
except Exception as e:
6271
logging.error(f"Failed to get issues from GitHub for repo {repo.name}: {e}")
@@ -70,47 +79,46 @@ def get_pull_requests(self, repo: Repository) -> list[PullRequest]:
7079
_id=p.number,
7180
title=p.title,
7281
author=Contributor(p.user.login, p.user.email or ""),
73-
state=p.state
74-
) for p in pulls
82+
state=p.state,
83+
)
84+
for p in pulls
7585
]
7686
except Exception as e:
77-
logging.error(f"Failed to get pull requests from GitHub for repo {repo.name}: {e}")
87+
logging.error(
88+
f"Failed to get pull requests from GitHub for repo {repo.name}: {e}"
89+
)
7890
return []
79-
91+
8092
def get_branches(self, repo: Repository) -> list[Branch]:
8193
try:
8294
repo_client = self.client.get_repo(repo._id)
8395
branches = repo_client.get_branches()
8496
result = []
85-
97+
8698
for branch in branches:
8799
commit = repo_client.get_commit(branch.commit.sha)
88-
89-
100+
90101
author = commit.author
91102
contributor = Contributor(
92103
username=author.login if author else "unknown",
93-
email=commit.commit.author.email or ""
104+
email=commit.commit.author.email or "",
94105
)
95-
106+
96107
commit_obj = Commit(
97108
_id=commit.sha,
98109
message=commit.commit.message,
99110
author=contributor,
100-
date=commit.commit.author.date
111+
date=commit.commit.author.date,
101112
)
102-
103-
result.append(
104-
Branch(
105-
name=branch.name,
106-
last_commit=commit_obj
107-
)
108-
)
109-
113+
114+
result.append(Branch(name=branch.name, last_commit=commit_obj))
115+
110116
return result
111-
117+
112118
except Exception as e:
113-
logging.error(f"Failed to get branches from GitHub for repo {repo.name}: {e}")
119+
logging.error(
120+
f"Failed to get branches from GitHub for repo {repo.name}: {e}"
121+
)
114122
return []
115123

116124
def get_wiki_pages(self, repo: Repository) -> list[WikiPage]:
@@ -123,7 +131,7 @@ def get_wiki_pages(self, repo: Repository) -> list[WikiPage]:
123131
client = Github("tocken")
124132
api = GitHubRepoAPI(client)
125133

126-
# Укажите ваш репозиторий
134+
# Укажите ваш репозиторий
127135
repo_name = ""
128136

129137
# Получение репозитория
@@ -139,7 +147,9 @@ def get_wiki_pages(self, repo: Repository) -> list[WikiPage]:
139147
commits = api.get_commits(repo)
140148
print(f"Total commits: {len(commits)}")
141149
for commit in commits[:10]: # Выведем первые 10 коммитов
142-
print(f"Commit: {commit._id}, Message: {commit.message}, Author: {commit.author.username}")
150+
print(
151+
f"Commit: {commit._id}, Message: {commit.message}, Author: {commit.author.username}"
152+
)
143153

144154
# Получение контрибьюторов
145155
contributors = api.get_contributors(repo)
@@ -159,9 +169,10 @@ def get_wiki_pages(self, repo: Repository) -> list[WikiPage]:
159169
for pull in pulls[:10]: # Выведем первые 10 pull requests
160170
print(f"Pull Request: {pull._id}, Title: {pull.title}, State: {pull.state}")
161171

162-
163172
# Получение веток
164173
branches = api.get_branches(repo)
165174
print(f"Total branches: {len(branches)}")
166175
for branch in branches:
167-
print(f"Branch: {branch.name}, Last Commit: {branch.last_commit._id if branch.last_commit else 'None'}")
176+
print(
177+
f"Branch: {branch.name}, Last Commit: {branch.last_commit._id if branch.last_commit else 'None'}"
178+
)

commits_parser.py

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,17 @@
66
EMPTY_FIELD = 'Empty field'
77
TIMEDELTA = 0.05
88
TIMEZONE = 'Europe/Moscow'
9-
FIELDNAMES = ('repository name', 'author name', 'author login', 'author email', 'date and time', 'changed files', 'commit id', 'branch')
9+
FIELDNAMES = (
10+
'repository name',
11+
'author name',
12+
'author login',
13+
'author email',
14+
'date and time',
15+
'changed files',
16+
'commit id',
17+
'branch',
18+
)
19+
1020

1121
def log_commit_to_csv(info, csv_name):
1222
with open(csv_name, 'a', newline='') as file:
@@ -33,27 +43,38 @@ def log_repository_commits(repository: Repository, csv_name, start, finish, bran
3343
print(f'Processing branch {branch}')
3444
# TODO add support of since and until in https://pygithub.readthedocs.io/en/stable/github_objects/Repository.html#github.Repository.Repository.get_commits
3545
for commit in repository.get_commits(sha=branch):
36-
if commit.commit.author.date.astimezone(
37-
pytz.timezone(TIMEZONE)) < start or commit.commit.author.date.astimezone(
38-
pytz.timezone(TIMEZONE)) > finish:
46+
if (
47+
commit.commit.author.date.astimezone(pytz.timezone(TIMEZONE)) < start
48+
or commit.commit.author.date.astimezone(pytz.timezone(TIMEZONE))
49+
> finish
50+
):
3951
continue
4052
if commit.commit is not None:
4153
nvl = lambda val: val or EMPTY_FIELD
42-
commit_data = [repository.full_name, commit.commit.author.name, nvl(commit.author.login if commit.author else None), nvl(commit.commit.author.email),
43-
commit.commit.author.date, '; '.join([file.filename for file in commit.files]), commit.commit.sha, branch]
54+
commit_data = [
55+
repository.full_name,
56+
commit.commit.author.name,
57+
nvl(commit.author.login if commit.author else None),
58+
nvl(commit.commit.author.email),
59+
commit.commit.author.date,
60+
'; '.join([file.filename for file in commit.files]),
61+
commit.commit.sha,
62+
branch,
63+
]
4464
info = dict(zip(FIELDNAMES, commit_data))
4565

4666
log_commit_to_csv(info, csv_name)
4767
log_commit_to_stdout(info)
4868
sleep(TIMEDELTA)
4969

5070

51-
def log_commits(client: Github, working_repos, csv_name, start, finish, branch, fork_flag):
71+
def log_commits(
72+
client: Github, working_repos, csv_name, start, finish, branch, fork_flag
73+
):
5274
with open(csv_name, 'w', newline='') as file:
5375
writer = csv.writer(file)
5476
writer.writerow(FIELDNAMES)
5577

56-
5778
for repo in working_repos:
5879
try:
5980
print('=' * 20, repo.full_name, '=' * 20)

contributors_parser.py

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,32 @@
66
EMPTY_FIELD = 'Empty field'
77
TIMEDELTA = 0.05
88
TIMEZONE = 'Europe/Moscow'
9-
FIELDNAMES = ('repository name', 'login', 'name', 'email', 'url', 'permissions', 'total_commits', 'id', 'node_id', 'type', 'bio', 'site_admin')
9+
FIELDNAMES = (
10+
'repository name',
11+
'login',
12+
'name',
13+
'email',
14+
'url',
15+
'permissions',
16+
'total_commits',
17+
'id',
18+
'node_id',
19+
'type',
20+
'bio',
21+
'site_admin',
22+
)
1023

1124

1225
def log_contributors_to_csv(info: dict, csv_name: str):
1326
with open(csv_name, 'a', newline='') as file:
1427
writer = csv.DictWriter(file, fieldnames=FIELDNAMES)
1528
writer.writerow(info)
1629

30+
1731
def log_contributors_to_stdout(info: dict):
1832
print(info)
1933

34+
2035
def log_repository_contributors(repository: Repository, csv_name: str):
2136
contributors_stats = get_contributors_stats(repository)
2237

@@ -46,17 +61,18 @@ def log_repository_contributors(repository: Repository, csv_name: str):
4661

4762
sleep(TIMEDELTA)
4863

64+
4965
def get_contributors_stats(repository: Repository) -> dict:
5066
contributors_stats = dict()
51-
67+
5268
for commit in repository.get_commits():
5369
contributor = commit.author
5470

5571
if not contributor.login in contributors_stats:
5672
contributors_stats[contributor.login] = {
5773
'total_commits': 0,
5874
'email': commit.commit.author.email,
59-
'contributor_object': contributor
75+
'contributor_object': contributor,
6076
}
6177

6278
contributors_stats[contributor.login]['total_commits'] += 1
@@ -65,7 +81,10 @@ def get_contributors_stats(repository: Repository) -> dict:
6581

6682
return contributors_stats
6783

68-
def log_contributors(client: Github, working_repos: Generator, csv_name: str, fork_flag: bool):
84+
85+
def log_contributors(
86+
client: Github, working_repos: Generator, csv_name: str, fork_flag: bool
87+
):
6988
with open(csv_name, 'w', newline='') as file:
7089
writer = csv.writer(file)
7190
writer.writerow(FIELDNAMES)
@@ -83,4 +102,4 @@ def log_contributors(client: Github, working_repos: Generator, csv_name: str, fo
83102

84103
except GithubException as e:
85104
print(e)
86-
exit(1)
105+
exit(1)

export_sheets.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
11
import pygsheets
22
import pandas as pd
33

4-
INT_MASS = [{
5-
"one": 1,
6-
"two": 2,
7-
"what?": 3
8-
}]
4+
INT_MASS = [{"one": 1, "two": 2, "what?": 3}]
5+
96

107
def write_data_to_table(csv_path, google_token, table_id, sheet_id):
11-
if google_token and sheet_id and table_id :
8+
if google_token and sheet_id and table_id:
129
gc = pygsheets.authorize(service_file=google_token)
1310
sh = gc.open_by_key(table_id)
1411

forgejo/main.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,13 @@
44
config = dotenv_values(".env")
55

66
if not "API_TOKEN" in config:
7-
print("Cannot find API_TOKEN in .env file. Check if it exists and has correct token. Aborting...")
7+
print(
8+
"Cannot find API_TOKEN in .env file. Check if it exists and has correct token. Aborting..."
9+
)
810
exit(1)
9-
client = PyforgejoApi(base_url="https://codeberg.org/api/v1", api_key=config["API_TOKEN"])
11+
client = PyforgejoApi(
12+
base_url="https://codeberg.org/api/v1", api_key=config["API_TOKEN"]
13+
)
1014

1115
repo = client.repository.repo_get(owner="harabat", repo="pyforgejo")
1216

git_logger.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
TIMEDELTA = 0.05
55
TIMEZONE = 'Europe/Moscow'
66

7+
78
def login(token):
89
client = Github(login_or_token=token)
910

@@ -34,8 +35,11 @@ def get_next_repo(client: Github, repositories):
3435

3536
def get_assignee_story(github_object):
3637
assignee_result = ""
37-
events = github_object.get_issue_events() if type(
38-
github_object) is PullRequest.PullRequest else github_object.get_events()
38+
events = (
39+
github_object.get_issue_events()
40+
if type(github_object) is PullRequest.PullRequest
41+
else github_object.get_events()
42+
)
3943
for event in events:
4044
if event.event in ["assigned", "unassigned"]:
4145
date = event.created_at

0 commit comments

Comments
 (0)