Skip to content

Commit 001b407

Browse files
Merge pull request #94 from moevm/add-liner
Add linter
1 parent 7f369be commit 001b407

15 files changed

+220
-71
lines changed

.flake8

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[flake8]
2+
max-line-length = 120
3+
exclude = .git,__pycache__,docs
4+
statistics = True

.github/workflows/linter.yml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
name: Linter
2+
3+
on:
4+
push:
5+
branches: [ "master" ]
6+
pull_request:
7+
branches: [ "master" ]
8+
9+
jobs:
10+
linter:
11+
runs-on: ubuntu-latest
12+
13+
steps:
14+
- uses: actions/checkout@v4
15+
16+
- name: Set up Python
17+
uses: actions/setup-python@v4
18+
with:
19+
python-version: '3.x'
20+
21+
- name: Install flake8
22+
run: pip install flake8
23+
24+
- name: Run flake8
25+
run: flake8 .

ForgejoRepoAPI.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from interface_wrapper import (
2+
IRepositoryAPI,
3+
)
4+
5+
6+
class ForgejoRepoAPI(IRepositoryAPI):
7+
...

GitHubRepoAPI.py

Lines changed: 52 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,33 @@ def __init__(self, client):
2020
self.client = client
2121

2222
def get_user_data(self, user) -> User:
23-
return User(login=user.login, username=user.name, email=user.email, html_url=user.html_url, node_id=user.node_id, type=user.type, bio=user.bio, site_admin=user.site_admin, _id=user.id)
24-
23+
return User(
24+
login=user.login,
25+
username=user.name,
26+
email=user.email,
27+
html_url=user.html_url,
28+
node_id=user.node_id,
29+
type=user.type,
30+
bio=user.bio,
31+
site_admin=user.site_admin,
32+
_id=user.id,
33+
)
2534

2635
def get_repository(self, id: str) -> Repository | None:
2736
try:
2837
repo = self.client.get_repo(id)
29-
return Repository(_id=repo.full_name, name=repo.name, url=repo.html_url, default_branch=Branch(name=repo.default_branch, last_commit=None), owner=User(login=repo.owner.login,username=repo.owner.name,email=repo.owner.email,html_url=repo.owner.html_url))
38+
return Repository(
39+
_id=repo.full_name,
40+
name=repo.name,
41+
url=repo.html_url,
42+
default_branch=Branch(name=repo.default_branch, last_commit=None),
43+
owner=User(
44+
login=repo.owner.login,
45+
username=repo.owner.name,
46+
email=repo.owner.email,
47+
html_url=repo.owner.html_url,
48+
),
49+
)
3050
except Exception as e:
3151
logging.error(f"Failed to get repository {id} from GitHub: {e}")
3252
return None
@@ -43,10 +63,7 @@ def get_commits(self, repo: Repository, files: bool = True) -> list[Commit]:
4363
message=c.commit.message,
4464
author=self.get_user_data(c.author),
4565
date=c.commit.author.date,
46-
files=[
47-
f.filename
48-
for f in c.files
49-
] if files else None
66+
files=[f.filename for f in c.files] if files else None,
5067
)
5168
for c in commits
5269
]
@@ -79,11 +96,8 @@ def get_issues(self, repo: Repository) -> list[Issue]:
7996
closed_by=self.get_user_data(i.closed_by) if i.closed_by else None,
8097
body=i.body,
8198
user=self.get_user_data(i.user),
82-
labels= [
83-
l.name
84-
for l in i.labels
85-
],
86-
milestone=i.milestone.title if i.milestone else None
99+
labels=[label.name for label in i.labels],
100+
milestone=i.milestone.title if i.milestone else None,
87101
)
88102
for i in issues
89103
]
@@ -106,16 +120,10 @@ def get_pull_requests(self, repo: Repository) -> list[PullRequest]:
106120
head_ref=p.head.ref,
107121
base_ref=p.base.ref,
108122
merged_by=self.get_user_data(p.merged_by) if p.merged_by else None,
109-
files=[
110-
f.filename
111-
for f in p.get_files()
112-
],
123+
files=[file.filename for file in p.get_files()],
113124
issue_url=p.issue_url,
114-
labels= [
115-
l.name
116-
for l in p.labels
117-
],
118-
milestone=p.milestone.title if p.milestone else None
125+
labels=[label.name for label in p.labels],
126+
milestone=p.milestone.title if p.milestone else None,
119127
)
120128
for p in pulls
121129
]
@@ -164,12 +172,14 @@ def get_forks(self, repo: Repository) -> list[Repository]:
164172
repo_client = self.client.get_repo(repo._id)
165173
result = []
166174
for r in repo_client.get_forks():
167-
result.append(Repository(_id=repo.full_name, name=repo.name, url=repo.html_url))
175+
result.append(
176+
Repository(_id=repo.full_name, name=repo.name, url=repo.html_url)
177+
)
168178
return result
169179

170180
def get_comments(self, repo, obj) -> list[Comment]:
171181
result = []
172-
if type(obj) == Issue:
182+
if isinstance(obj, Issue):
173183
# TODO оптимизировать
174184
issues = self.client.get_repo(repo._id).get_issues(state='all')
175185
issue = None
@@ -178,8 +188,14 @@ def get_comments(self, repo, obj) -> list[Comment]:
178188
issue = i
179189
break
180190
for c in issue.get_comments():
181-
result.append(Comment(body=c.body,created_at=c.created_at,author=self.get_user_data(c.user)))
182-
elif type(obj) == PullRequest:
191+
result.append(
192+
Comment(
193+
body=c.body,
194+
created_at=c.created_at,
195+
author=self.get_user_data(c.user),
196+
)
197+
)
198+
elif isinstance(obj, PullRequest):
183199
# TODO оптимизировать
184200
pulls = self.client.get_repo(repo._id).get_pulls(state='all')
185201
pull = None
@@ -188,7 +204,13 @@ def get_comments(self, repo, obj) -> list[Comment]:
188204
pull = p
189205
break
190206
for c in pull.get_comments():
191-
result.append(Comment(body=c.body,created_at=c.created_at,author=self.get_user_data(c.user.login)))
207+
result.append(
208+
Comment(
209+
body=c.body,
210+
created_at=c.created_at,
211+
author=self.get_user_data(c.user.login),
212+
)
213+
)
192214

193215
return result
194216

@@ -200,7 +222,7 @@ def get_invites(self, repo: Repository) -> list[Invite]:
200222
_id=i._id,
201223
invitee=self.get_user_data(i.invitee),
202224
created_at=i.created_at,
203-
html_url=i.html_url
225+
html_url=i.html_url,
204226
)
205227
for i in invites
206228
]
@@ -210,11 +232,12 @@ def get_invites(self, repo: Repository) -> list[Invite]:
210232
)
211233
return []
212234

235+
213236
# Точка входа для тестирования
214237
if __name__ == "__main__":
215238
# Создайте клиент GitHub (используйте ваш токен)
216-
client = Github("tocken")
217-
api = GitHubRepoAPI(client)
239+
# client = Github("tocken")
240+
api = GitHubRepoAPI('client')
218241

219242
# Укажите ваш репозиторий
220243
repo_name = ""

commits_parser.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
from utils import logger
22
import pytz
33
from time import sleep
4-
#from github import Github, Repository, GithubException, PullRequest
4+
5+
# from github import Github, Repository, GithubException, PullRequest
56
from interface_wrapper import IRepositoryAPI, Repository
67

78
EMPTY_FIELD = 'Empty field'
@@ -19,7 +20,9 @@
1920
)
2021

2122

22-
def log_repository_commits(client: IRepositoryAPI, repository: Repository, csv_name, start, finish, branch):
23+
def log_repository_commits(
24+
client: IRepositoryAPI, repository: Repository, csv_name, start, finish, branch
25+
):
2326
branches = []
2427
match branch:
2528
case 'all':
@@ -56,7 +59,9 @@ def log_repository_commits(client: IRepositoryAPI, repository: Repository, csv_n
5659
sleep(TIMEDELTA)
5760

5861

59-
def log_commits(client: IRepositoryAPI, working_repos, csv_name, start, finish, branch, fork_flag):
62+
def log_commits(
63+
client: IRepositoryAPI, working_repos, csv_name, start, finish, branch, fork_flag
64+
):
6065
logger.log_to_csv(csv_name, FIELDNAMES)
6166

6267
for repo, token in working_repos:
@@ -66,7 +71,9 @@ def log_commits(client: IRepositoryAPI, working_repos, csv_name, start, finish,
6671
if fork_flag:
6772
for forked_repo in client.get_forks(repo):
6873
logger.log_title("FORKED:", forked_repo.full_name)
69-
log_repository_commits(client, forked_repo, csv_name, start, finish, branch)
74+
log_repository_commits(
75+
client, forked_repo, csv_name, start, finish, branch
76+
)
7077
sleep(TIMEDELTA)
7178
sleep(TIMEDELTA)
7279
except Exception as e:

contributors_parser.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from utils import logger
22
from time import sleep
33
from typing import Generator
4-
from interface_wrapper import IRepositoryAPI, Repository, User
4+
from interface_wrapper import IRepositoryAPI, Repository
55

66
EMPTY_FIELD = 'Empty field'
77
TIMEDELTA = 0.05
@@ -22,14 +22,19 @@
2222
)
2323

2424

25-
def log_repository_contributors(client: IRepositoryAPI, repository: Repository, csv_name: str):
25+
def log_repository_contributors(
26+
client: IRepositoryAPI, repository: Repository, csv_name: str
27+
):
2628
contributors_stats = get_contributors_stats(client, repository)
2729

28-
nvl = lambda val: val or EMPTY_FIELD
30+
def nvl(val):
31+
return val or EMPTY_FIELD
2932

3033
for contributor_stat in contributors_stats.values():
3134
contributor = contributor_stat["contributor_object"]
32-
contributor_permissions = client.get_collaborator_permission(repository, contributor)
35+
contributor_permissions = client.get_collaborator_permission(
36+
repository, contributor
37+
)
3338

3439
info_tmp = {
3540
'repository name': repository.name,
@@ -50,14 +55,15 @@ def log_repository_contributors(client: IRepositoryAPI, repository: Repository,
5055

5156
sleep(TIMEDELTA)
5257

58+
5359
def get_contributors_stats(client: IRepositoryAPI, repository: Repository) -> dict:
5460
contributors_stats = dict()
5561
commits = client.get_commits(repository, False)
5662

5763
for commit in commits:
5864
contributor = commit.author
5965

60-
if not contributor.login in contributors_stats:
66+
if contributor.login not in contributors_stats:
6167
contributors_stats[contributor.login] = {
6268
'total_commits': 0,
6369
'email': contributor.email,
@@ -71,7 +77,9 @@ def get_contributors_stats(client: IRepositoryAPI, repository: Repository) -> di
7177
return contributors_stats
7278

7379

74-
def log_contributors(client: IRepositoryAPI, working_repos: Generator, csv_name: str, fork_flag: bool):
80+
def log_contributors(
81+
client: IRepositoryAPI, working_repos: Generator, csv_name: str, fork_flag: bool
82+
):
7583
logger.log_to_csv(csv_name, FIELDNAMES)
7684

7785
for repo, token in working_repos:

export_sheets.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ def write_data_to_table(csv_path, google_token, table_id, sheet_id):
1111

1212
try:
1313
sh.worksheets('title', sheet_id)
14-
except:
14+
except Exception:
1515
sh.add_worksheet(sheet_id)
1616

1717
wk_content = sh.worksheet_by_title(sheet_id)

forgejo/main.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
from dotenv import dotenv_values
22
from pyforgejo import PyforgejoApi
33

4+
45
def require_env_arg(config, name):
5-
if not name in config:
6+
if name not in config:
67
print("Cannot find " + name + " in .env file. Aborting...")
78
exit(1)
89

10+
911
config = dotenv_values(".env")
1012
require_env_arg(config, "API_TOKEN")
1113
require_env_arg(config, "BASE_URL")
1214
require_env_arg(config, "REPO_OWNER")
1315
require_env_arg(config, "REPO_NAME")
1416

15-
client = PyforgejoApi(
16-
base_url=config["BASE_URL"], api_key=config["API_TOKEN"]
17-
)
17+
client = PyforgejoApi(base_url=config["BASE_URL"], api_key=config["API_TOKEN"])
1818

1919
repo = client.repository.repo_get(owner=config["REPO_OWNER"], repo=config["REPO_NAME"])
2020

git_logger.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
from github import Github, GithubException, PullRequest
2-
from interface_wrapper import IRepositoryAPI, RepositoryFactory, Repository, User, Branch
2+
from interface_wrapper import (
3+
RepositoryFactory,
4+
Repository,
5+
Branch,
6+
)
37
from time import sleep
48

59
TIMEDELTA = 0.05
@@ -76,7 +80,13 @@ def get_next_repo(clients: GithubClients, repositories):
7680
exit(1)
7781
else:
7882
print(cur_client['token'])
79-
yield Repository(_id=repo.full_name,name=repo.name,url=repo.html_url, default_branch=Branch(name=repo.default_branch, last_commit=None), owner=cur_client['api'].get_user_data(repo.owner)), cur_client['token']
83+
yield Repository(
84+
_id=repo.full_name,
85+
name=repo.name,
86+
url=repo.html_url,
87+
default_branch=Branch(name=repo.default_branch, last_commit=None),
88+
owner=cur_client['api'].get_user_data(repo.owner),
89+
), cur_client['token']
8090

8191

8292
def get_assignee_story(github_object):

0 commit comments

Comments
 (0)