Skip to content

Commit b7ae2a5

Browse files
apply black (#84)
1 parent b33af45 commit b7ae2a5

File tree

11 files changed

+400
-130
lines changed

11 files changed

+400
-130
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)

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

interface_wrapper.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,53 +5,60 @@
55

66
# Настройка логирования
77
logging.basicConfig(
8-
level=logging.INFO,
9-
format="%(asctime)s - %(levelname)s - %(message)s"
8+
level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s"
109
)
1110

11+
1212
# Модельные классы
1313
@dataclass
1414
class Repository:
1515
_id: str
1616
name: str
1717
url: str
1818

19+
1920
@dataclass
2021
class Contributor:
2122
username: str
2223
email: str
2324

25+
2426
@dataclass
2527
class Commit:
2628
_id: str
2729
message: str
2830
author: Contributor
2931
date: datetime
3032

33+
3134
@dataclass
3235
class Issue:
3336
_id: str
3437
title: str
3538
author: Contributor
3639
state: str
3740

41+
3842
@dataclass
3943
class PullRequest:
4044
_id: str
4145
title: str
4246
author: Contributor
4347
state: str
4448

49+
4550
@dataclass
4651
class WikiPage:
4752
title: str
4853
content: str
4954

55+
5056
@dataclass
5157
class Branch:
5258
name: str
5359
last_commit: Commit | None
5460

61+
5562
# Интерфейс API
5663
class IRepositoryAPI(ABC):
5764
@abstractmethod
@@ -83,12 +90,12 @@ def get_pull_requests(self, repo: Repository) -> list[PullRequest]:
8390
def get_branches(self, repo: Repository) -> list[Branch]:
8491
"""Получить список веток для репозитория."""
8592
pass
86-
93+
8794
@abstractmethod
8895
def get_wiki_pages(self, repo: Repository) -> list[WikiPage]:
8996
"""Получить список wiki-страниц для репозитория."""
9097
pass
91-
98+
9299

93100
# Фабрика для создания API
94101
class RepositoryFactory:
@@ -103,6 +110,7 @@ def create_api(source: str, client) -> IRepositoryAPI:
103110
else:
104111
raise ValueError(f"Unsupported source: {source}")
105112

113+
106114
# Сервис для расчёта метрик
107115
class CommitmentCalculator:
108116
def __init__(self, api: IRepositoryAPI):

invites_parser.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,21 @@
66
from github import Github, Repository, GithubException, PullRequest
77
import csv
88

9-
FIELDNAMES = ('repository name', 'invited login', 'invite creation date', 'invitation url')
9+
FIELDNAMES = (
10+
'repository name',
11+
'invited login',
12+
'invite creation date',
13+
'invitation url',
14+
)
15+
1016

1117
def log_inviter(repo, invite, writer):
12-
invite_info = [repo.full_name, invite.invitee.login, invite.created_at.strftime("%d/%m/%Y, %H:%M:%S"), invite.html_url]
18+
invite_info = [
19+
repo.full_name,
20+
invite.invitee.login,
21+
invite.created_at.strftime("%d/%m/%Y, %H:%M:%S"),
22+
invite.html_url,
23+
]
1324
writer.writerow(invite_info)
1425
print(invite_info)
1526

0 commit comments

Comments
 (0)