Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/forgejo/projects #893

Draft
wants to merge 13 commits into
base: main
Choose a base branch
from
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ repos:
# to verify the rebasing
stages: [manual, pre-push]
- repo: https://github.com/packit/requre
rev: 0.8.4
rev: 0.9.0
hooks:
- id: requre-purge
# Do not run in pre-commit.ci as it requires too much time
Expand Down
89 changes: 61 additions & 28 deletions ogr/abstract.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import datetime
import functools
from collections.abc import Sequence
from collections.abc import Iterable, Sequence
from enum import Enum, IntEnum
from re import Match
from typing import (
Expand Down Expand Up @@ -245,7 +245,7 @@ def edited(self) -> datetime.datetime:
"""Datetime of last edit of the comment."""
return self._edited

def get_reactions(self) -> list[Reaction]:
def get_reactions(self) -> Union[list[Reaction], Iterable[Reaction]]:
"""Returns list of reactions."""
raise NotImplementedError()

Expand Down Expand Up @@ -343,7 +343,7 @@ def created(self) -> datetime.datetime:
raise NotImplementedError()

@property
def labels(self) -> list["IssueLabel"]:
def labels(self) -> Union[list["IssueLabel"], Iterable["IssueLabel"]]:
"""Labels of the issue."""
raise NotImplementedError()

Expand Down Expand Up @@ -417,7 +417,7 @@ def get_list(
author: Optional[str] = None,
assignee: Optional[str] = None,
labels: Optional[list[str]] = None,
) -> list["Issue"]:
) -> Union[list["Issue"], Iterable["Issue"]]:
"""
List of issues.

Expand All @@ -442,10 +442,19 @@ def get_list(
"""
raise NotImplementedError()

def _get_all_comments(self) -> list[IssueComment]:
def _get_all_comments(
self,
reverse: bool = False,
) -> Union[list[IssueComment], Iterable[IssueComment]]:
"""
Get list of all issue comments.

Args:
reverse: Defines whether the comments should be listed in a reversed
order.

Defaults to `False`.

Returns:
List of all comments on the issue.
"""
Expand All @@ -456,7 +465,7 @@ def get_comments(
filter_regex: Optional[str] = None,
reverse: bool = False,
author: Optional[str] = None,
) -> list[IssueComment]:
) -> Union[list[IssueComment], Iterable[IssueComment]]:
"""
Get list of issue comments.

Expand Down Expand Up @@ -635,7 +644,7 @@ def created(self) -> datetime.datetime:
raise NotImplementedError()

@property
def labels(self) -> list["PRLabel"]:
def labels(self) -> Union[list["PRLabel"], Iterable["PRLabel"]]:
"""Labels of the pull request."""
raise NotImplementedError()

Expand Down Expand Up @@ -752,7 +761,10 @@ def get(project: Any, id: int) -> "PullRequest":
raise NotImplementedError()

@staticmethod
def get_list(project: Any, status: PRStatus = PRStatus.open) -> list["PullRequest"]:
def get_list(
project: Any,
status: PRStatus = PRStatus.open,
) -> Union[list["PullRequest"], Iterable["PullRequest"]]:
"""
List of pull requests.

Expand Down Expand Up @@ -788,10 +800,19 @@ def update_info(
"""
raise NotImplementedError()

def _get_all_comments(self) -> list[PRComment]:
def _get_all_comments(
self,
reverse: bool = False,
) -> Union[list[PRComment], Iterable[PRComment]]:
"""
Get list of all pull request comments.

Args:
reverse: Defines whether the comments should be listed in a reversed
order.

Defaults to `False`.

Returns:
List of all comments on the pull request.
"""
Expand All @@ -802,7 +823,7 @@ def get_comments(
filter_regex: Optional[str] = None,
reverse: bool = False,
author: Optional[str] = None,
) -> list["PRComment"]:
) -> Union[list["PRComment"], Iterable["PRComment"]]:
"""
Get list of pull request comments.

Expand All @@ -823,7 +844,7 @@ def get_comments(
"""
raise NotImplementedError()

def get_all_commits(self) -> list[str]:
def get_all_commits(self) -> Union[list[str], Iterable[str]]:
"""
Returns:
List of commit hashes of commits in pull request.
Expand Down Expand Up @@ -907,7 +928,7 @@ def add_label(self, *labels: str) -> None:
"""
raise NotImplementedError()

def get_statuses(self) -> list["CommitFlag"]:
def get_statuses(self) -> Union[list["CommitFlag"], Iterable["CommitFlag"]]:
"""
Returns statuses for latest commit on pull request.

Expand Down Expand Up @@ -996,7 +1017,10 @@ def _from_raw_commit_flag(self) -> None:
raise NotImplementedError()

@staticmethod
def get(project: Any, commit: str) -> list["CommitFlag"]:
def get(
project: Any,
commit: str,
) -> Union[list["CommitFlag"], Iterable["CommitFlag"]]:
"""
Acquire commit statuses for given commit in the project.

Expand Down Expand Up @@ -1208,7 +1232,7 @@ def get_latest(project: Any) -> Optional["Release"]:
raise NotImplementedError()

@staticmethod
def get_list(project: Any) -> list["Release"]:
def get_list(project: Any) -> Union[list["Release"], Iterable["Release"]]:
"""
Returns:
List of the objects that represent releases.
Expand Down Expand Up @@ -1370,7 +1394,7 @@ def list_projects(
user: Optional[str] = None,
search_pattern: Optional[str] = None,
language: Optional[str] = None,
) -> list["GitProject"]:
) -> Union[list["GitProject"], Iterable["GitProject"]]:
"""
List projects for given criteria.

Expand Down Expand Up @@ -1478,7 +1502,7 @@ def has_issues(self) -> bool:
"""`True` if issues are enabled on the project."""
raise NotImplementedError()

def get_branches(self) -> list[str]:
def get_branches(self) -> Union[list[str], Iterable[str]]:
"""
Returns:
List with names of branches in the project.
Expand All @@ -1490,7 +1514,7 @@ def default_branch(self) -> str:
"""Default branch (usually `main`, `master` or `trunk`)."""
raise NotImplementedError()

def get_commits(self, ref: Optional[str] = None) -> list[str]:
def get_commits(self, ref: Optional[str] = None) -> Union[list[str], Iterable[str]]:
"""
Get list of commits for the project.

Expand Down Expand Up @@ -1522,7 +1546,7 @@ def get_fork(self, create: bool = True) -> Optional["GitProject"]:
"""
raise NotImplementedError()

def get_owners(self) -> list[str]:
def get_owners(self) -> Union[list[str], Iterable[str]]:
"""
Returns:
List of usernames of project owners.
Expand Down Expand Up @@ -1621,7 +1645,7 @@ def get_issue_list(
author: Optional[str] = None,
assignee: Optional[str] = None,
labels: Optional[list[str]] = None,
) -> list["Issue"]:
) -> Union[list["Issue"], Iterable["Issue"]]:
"""
List of issues.

Expand Down Expand Up @@ -1704,7 +1728,10 @@ def create_issue(
"""
raise NotImplementedError()

def get_pr_list(self, status: PRStatus = PRStatus.open) -> list["PullRequest"]:
def get_pr_list(
self,
status: PRStatus = PRStatus.open,
) -> Union[list["PullRequest"], Iterable["PullRequest"]]:
"""
List of pull requests.

Expand Down Expand Up @@ -1747,7 +1774,7 @@ def get_pr_files_diff(
"""
raise NotImplementedError()

def get_tags(self) -> list["GitTag"]:
def get_tags(self) -> Union[list["GitTag"], Iterable["GitTag"]]:
"""
Returns:
List of objects that represent tags.
Expand Down Expand Up @@ -1796,7 +1823,7 @@ def get_latest_release(self) -> Optional[Release]:
"""
raise NotImplementedError()

def get_releases(self) -> list[Release]:
def get_releases(self) -> Union[list[Release], Iterable[Release]]:
"""
Returns:
List of the objects that represent releases.
Expand Down Expand Up @@ -1877,7 +1904,10 @@ def commit_comment(
"""
raise NotImplementedError()

def get_commit_comments(self, commit: str) -> list[CommitComment]:
def get_commit_comments(
self,
commit: str,
) -> Union[list[CommitComment], Iterable[CommitComment]]:
"""
Get comments for a commit.

Expand Down Expand Up @@ -1929,7 +1959,10 @@ def set_commit_status(
"""
raise NotImplementedError()

def get_commit_statuses(self, commit: str) -> list[CommitFlag]:
def get_commit_statuses(
self,
commit: str,
) -> Union[list[CommitFlag], Iterable[CommitFlag]]:
"""
Get statuses of the commit.

Expand Down Expand Up @@ -2000,7 +2033,7 @@ def get_files(
ref: Optional[str] = None,
filter_regex: Optional[str] = None,
recursive: bool = False,
) -> list[str]:
) -> Union[list[str], Iterable[str]]:
"""
Get a list of file paths of the repo.

Expand All @@ -2021,7 +2054,7 @@ def get_files(
"""
raise NotImplementedError

def get_forks(self) -> Sequence["GitProject"]:
def get_forks(self) -> Union[Sequence["GitProject"], Iterable["GitProject"]]:
"""
Returns:
All forks of the project.
Expand Down Expand Up @@ -2088,14 +2121,14 @@ def get_email(self) -> str:
"""
raise NotImplementedError()

def get_projects(self) -> Sequence["GitProject"]:
def get_projects(self) -> Union[Sequence["GitProject"], Iterable["GitProject"]]:
"""
Returns:
Sequence of projects in user's namespace.
"""
raise NotImplementedError()

def get_forks(self) -> Sequence["GitProject"]:
def get_forks(self) -> Union[Sequence["GitProject"], Iterable["GitProject"]]:
"""
Returns:
Sequence of forks in user's namespace.
Expand Down
44 changes: 24 additions & 20 deletions ogr/services/base.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
# Copyright Contributors to the Packit project.
# SPDX-License-Identifier: MIT

from typing import Any, Optional
import re
from collections.abc import Iterable
from typing import Optional, Union
from urllib.request import urlopen

from ogr.abstract import (
Expand All @@ -12,6 +14,7 @@
GitUser,
Issue,
IssueComment,
PRComment,
PullRequest,
Release,
)
Expand Down Expand Up @@ -57,28 +60,29 @@ def get_comments(
filter_regex: Optional[str] = None,
reverse: bool = False,
author: Optional[str] = None,
):
all_comments = self._get_all_comments()
return filter_comments(all_comments, filter_regex, reverse, author)
) -> Union[list[PRComment], Iterable[PRComment]]:
all_comments = self._get_all_comments(reverse=reverse)
return filter_comments(all_comments, filter_regex, author)

def search(
self,
filter_regex: str,
reverse: bool = False,
description: bool = True,
):
all_comments: list[Any] = self.get_comments(reverse=reverse)
if description:
description_content = self.description
if reverse:
all_comments.append(description_content)
else:
all_comments.insert(0, description_content)

return search_in_comments(comments=all_comments, filter_regex=filter_regex)

def get_statuses(self) -> list[CommitFlag]:
commit = self.get_all_commits()[-1]
) -> Optional[re.Match[str]]:
if description and (found_match := re.search(filter_regex, self.description)):
return found_match

return search_in_comments(
comments=self.get_comments(reverse=reverse),
filter_regex=filter_regex,
)

def get_statuses(self) -> Union[list[CommitFlag], Iterable[CommitFlag]]:
# [NOTE] Is there any reason we fetch all commits, instead of using the
# head commit on the PR?
# commit = self.get_all_commits()[-1]
commit = self.head_commit
return self.target_project.get_commit_statuses(commit)


Expand All @@ -92,9 +96,9 @@ def get_comments(
filter_regex: Optional[str] = None,
reverse: bool = False,
author: Optional[str] = None,
) -> list[IssueComment]:
all_comments: list[IssueComment] = self._get_all_comments()
return filter_comments(all_comments, filter_regex, reverse, author)
) -> Union[list[IssueComment], Iterable[IssueComment]]:
all_comments = self._get_all_comments(reverse=reverse)
return filter_comments(all_comments, filter_regex, author)

def can_close(self, username: str) -> bool:
return username == self.author or username in self.project.who_can_close_issue()
Expand Down
Loading
Loading