Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 18 additions & 8 deletions src/ForgejoRepoAPI.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@


class ForgejoRepoAPI(IRepositoryAPI):
def __init__(self, client):
def __init__(self, client: PyforgejoApi):
self.client = client

def get_user_data(self, user) -> User:
Expand Down Expand Up @@ -141,6 +141,8 @@ def get_pull_requests(self, repo: Repository) -> list[PullRequest]:
issue_url=None, # TODO если возможно - пока не нашел
labels=[label.name for label in p.labels] if p.labels else [],
milestone=p.milestone.title if p.milestone else None,
comments=p.comments,
review_comments=p.review_comments
)
for p in pulls
]
Expand Down Expand Up @@ -241,13 +243,21 @@ def get_comments(self, repo, obj) -> list[Comment]:
]

elif isinstance(obj, PullRequest):
comments = self.client.repository.repo_get_pull_review_comments(
repo.owner.login, repo.name, obj._id, 100000
)
# нет id комментария - сейчас не работает, т.к. требуется ID ревью - нужно сначала получить \
# список ревью /repos/{owner}/{repo}/pulls/{index}/reviews
# TODO: нужны отдельные запросы для получения комментариев и комментариев ревью #163, \
# в основной сущности PullRequest есть поля "comments": int, "review_comments": int
pull_request = obj
comments = []
pr_data = (repo.owner.login, repo.name, pull_request._id)
if pull_request.comments:
# simple comments in PR
comments.extend(self.client.issue.get_comments(*pr_data))
if pull_request.review_comments:
# get all PR reviews
pr_reviewes = self.get_all_data_from_pages(self.client.repository.repo_list_pull_reviews, *pr_data)
for review in pr_reviewes:
comments.extend(
self.client.repository.repo_get_pull_review_comments(
*pr_data, review.id
)
)
result = [
Comment(
body=c.body,
Expand Down
186 changes: 93 additions & 93 deletions src/commits_parser.py
Original file line number Diff line number Diff line change
@@ -1,93 +1,93 @@
from dataclasses import asdict, dataclass
from datetime import datetime
from time import sleep
from typing import Generator

import pytz

from src.constants import EMPTY_FIELD, GOOGLE_MAX_CELL_LEN, TIMEDELTA, TIMEZONE
from src.interface_wrapper import IRepositoryAPI, Repository
from src.utils import logger


@dataclass(kw_only=True, frozen=True)
class CommitData:
repository_name: str = ''
author_name: str = ''
author_login: str = ''
author_email: str = ''
date_and_time: str = ''
changed_files: str = ''
commit_id: str = ''
branch: str = ''
additions: str = ''
deletions: str = ''


def log_repository_commits(
client: IRepositoryAPI, repository: Repository, csv_name, start, finish, branch
):
branches = []
match branch:
case 'all':
for branch in client.get_branches(repository):
branches.append(branch.name)
case None:
branches.append(repository.default_branch.name)
case _:
branches.append(branch)

for branch in branches:
print(f'Processing branch {branch}')
commits = client.get_commits(repository)
for commit in commits:
if (
commit.date.astimezone(pytz.timezone(TIMEZONE)) < start
or commit.date.astimezone(pytz.timezone(TIMEZONE)) > finish
):
continue

changed_files = '; '.join([file for file in commit.files])
changed_files = changed_files[:GOOGLE_MAX_CELL_LEN]
commit_data = CommitData(
repository_name=repository.name,
author_name=commit.author.username if commit.author else EMPTY_FIELD,
author_login=commit.author.login if commit.author else EMPTY_FIELD,
author_email=commit.author.email if commit.author else EMPTY_FIELD,
date_and_time=commit.date.astimezone(pytz.timezone(TIMEZONE)).isoformat(),
changed_files=changed_files,
commit_id=commit._id,
branch=branch,
additions=commit.additions,
deletions=commit.deletions,
)
info = asdict(commit_data)

logger.log_to_csv(csv_name, list(info.keys()), info)
logger.log_to_stdout(info)

sleep(TIMEDELTA)


def log_commits(
binded_repos: Generator[tuple[IRepositoryAPI, Repository, str], None, None],
csv_name: str,
start: datetime,
finish: datetime,
branch: str,
fork_flag: bool,
):
info = asdict(CommitData())
logger.log_to_csv(csv_name, list(info.keys()))

for client, repo, token in binded_repos:
logger.log_title(repo.name)
log_repository_commits(client, repo, csv_name, start, finish, branch)
if fork_flag:
for forked_repo in client.get_forks(repo):
logger.log_title(f"FORKED: {forked_repo.name}")
log_repository_commits(
client, forked_repo, csv_name, start, finish, branch
)
sleep(TIMEDELTA)
sleep(TIMEDELTA)
from dataclasses import asdict, dataclass
from datetime import datetime
from time import sleep
from typing import Generator
import pytz
from src.constants import EMPTY_FIELD, GOOGLE_MAX_CELL_LEN, TIMEDELTA, TIMEZONE
from src.interface_wrapper import IRepositoryAPI, Repository
from src.utils import logger
@dataclass(kw_only=True, frozen=True)
class CommitData:
repository_name: str = ''
author_name: str = ''
author_login: str = ''
author_email: str = ''
date_and_time: str = ''
changed_files: str = ''
commit_id: str = ''
branch: str = ''
additions: str = ''
deletions: str = ''
def log_repository_commits(
client: IRepositoryAPI, repository: Repository, csv_name, start, finish, branch
):
branches = []
match branch:
case 'all':
for branch in client.get_branches(repository):
branches.append(branch.name)
case None:
branches.append(repository.default_branch.name)
case _:
branches.append(branch)
for branch in branches:
print(f'Processing branch {branch}')
commits = client.get_commits(repository)
for commit in commits:
if (
commit.date.astimezone(pytz.timezone(TIMEZONE)) < start
or commit.date.astimezone(pytz.timezone(TIMEZONE)) > finish
):
continue
changed_files = '; '.join([file for file in commit.files])
changed_files = changed_files[:GOOGLE_MAX_CELL_LEN]
commit_data = CommitData(
repository_name=repository.name,
author_name=commit.author.username if commit.author else EMPTY_FIELD,
author_login=commit.author.login if commit.author else EMPTY_FIELD,
author_email=commit.author.email if commit.author else EMPTY_FIELD,
date_and_time=commit.date.astimezone(pytz.timezone(TIMEZONE)).isoformat(),
changed_files=changed_files,
commit_id=commit._id,
branch=branch,
additions=commit.additions,
deletions=commit.deletions,
)
info = asdict(commit_data)
logger.log_to_csv(csv_name, list(info.keys()), info)
logger.log_to_stdout(info)
sleep(TIMEDELTA)
def log_commits(
binded_repos: Generator[tuple[IRepositoryAPI, Repository, str], None, None],
csv_name: str,
start: datetime,
finish: datetime,
branch: str,
fork_flag: bool = False,
):
info = asdict(CommitData())
logger.log_to_csv(csv_name, list(info.keys()))
for client, repo, token in binded_repos:
logger.log_title(repo.name)
log_repository_commits(client, repo, csv_name, start, finish, branch)
if fork_flag:
for forked_repo in client.get_forks(repo):
logger.log_title(f"FORKED: {forked_repo.name}")
log_repository_commits(
client, forked_repo, csv_name, start, finish, branch
)
sleep(TIMEDELTA)
sleep(TIMEDELTA)
4 changes: 3 additions & 1 deletion src/interface_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,9 @@ class PullRequest:
files: list[str]
issue_url: str
labels: list[str]
milestone: str
milestone: str
comments: int = 0
review_comments: int = 0


@dataclass
Expand Down
Loading
Loading