-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add: Add async GitHub API to search for repositories
Implement the GitHub REST search repositories API.
- Loading branch information
1 parent
2fd4271
commit a356d32
Showing
6 changed files
with
676 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
# Copyright (C) 2023 Greenbone Networks GmbH | ||
# | ||
# SPDX-License-Identifier: GPL-3.0-or-later | ||
# | ||
# This program is free software: you can redistribute it and/or modify | ||
# it under the terms of the GNU General Public License as published by | ||
# the Free Software Foundation, either version 3 of the License, or | ||
# (at your option) any later version. | ||
# | ||
# This program is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU General Public License | ||
# along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
from typing import AsyncIterator, Iterable, Union | ||
|
||
from pontos.github.api.client import GitHubAsyncREST | ||
from pontos.github.models.organization import Repository | ||
from pontos.github.models.search import Qualifier, RepositorySort, SortOrder | ||
from pontos.helper import enum_or_value | ||
|
||
|
||
class GitHubAsyncRESTSearch(GitHubAsyncREST): | ||
async def repositories( | ||
self, | ||
*, | ||
keywords: Iterable[str], | ||
qualifiers: Iterable[Qualifier], | ||
order: Union[str, SortOrder] = SortOrder.DESC, | ||
sort: Union[str, RepositorySort, None] = None, | ||
) -> AsyncIterator[Repository]: | ||
""" | ||
Search for repositories | ||
https://docs.github.com/en/rest/search#search-repositories | ||
https://docs.github.com/en/search-github/searching-on-github/searching-for-repositories | ||
Args: | ||
keywords: List of keywords to search for. | ||
qualifiers: List of qualifiers. | ||
order: Sort order either 'asc' or 'desc'. Default is 'desc'. | ||
sort: Sort the found repositories by this criteria. | ||
Raises: | ||
`httpx.HTTPStatusError` if there was an error in the request | ||
""" | ||
api = "/search/repositories" | ||
params = { | ||
"per_page": "100", | ||
} | ||
if order: | ||
params["order"] = enum_or_value(order) | ||
if sort: | ||
params["sort"] = enum_or_value(sort) | ||
|
||
query = ( | ||
f"{' '.join(keywords)} " | ||
f"{' '.join([str(qualifier) for qualifier in qualifiers])}" | ||
) | ||
params["q"] = query | ||
|
||
async for response in self._client.get_all(api, params=params): | ||
response.raise_for_status() | ||
data = response.json() | ||
for repo in data["items"]: | ||
yield Repository.from_dict(repo) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
# Copyright (C) 2023 Greenbone Networks GmbH | ||
# | ||
# SPDX-License-Identifier: GPL-3.0-or-later | ||
# | ||
# This program is free software: you can redistribute it and/or modify | ||
# it under the terms of the GNU General Public License as published by | ||
# the Free Software Foundation, either version 3 of the License, or | ||
# (at your option) any later version. | ||
# | ||
# This program is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU General Public License | ||
# along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
__all__ = ( | ||
"InDescriptionQualifier", | ||
"InNameQualifier", | ||
"InReadmeQualifier", | ||
"InTopicsQualifier", | ||
"IsPrivateQualifier", | ||
"IsPublicQualifier", | ||
"NotQualifier", | ||
"OrganizationQualifier", | ||
"Qualifier", | ||
"RepositoryQualifier", | ||
"RepositorySort", | ||
"SortOrder", | ||
"UserQualifier", | ||
) | ||
|
||
from abc import ABC | ||
from enum import Enum | ||
|
||
|
||
class SortOrder(Enum): | ||
ASC = "asc" | ||
DESC = "desc" | ||
|
||
|
||
class RepositorySort(Enum): | ||
STARS = "stars" | ||
FORKS = "forks" | ||
HELP_WANTED_ISSUES = "help-wanted-issues" | ||
UPDATED = "updated" | ||
|
||
|
||
class Qualifier(ABC): | ||
operator: str | ||
term: str | ||
|
||
def __str__(self) -> str: | ||
""" """ | ||
return f"{self.operator}:{self.term}" | ||
|
||
|
||
class NotQualifier(Qualifier): | ||
def __init__(self, qualifier: Qualifier) -> None: | ||
self.qualifier = qualifier | ||
|
||
def __str__(self) -> str: | ||
return f"-{str(self.qualifier)}" | ||
|
||
|
||
class InQualifier(Qualifier): | ||
operator = "in" | ||
|
||
|
||
class InNameQualifier(InQualifier): | ||
term = "name" | ||
|
||
|
||
class InDescriptionQualifier(InQualifier): | ||
term = "description" | ||
|
||
|
||
class InTopicsQualifier(InQualifier): | ||
term = "topics" | ||
|
||
|
||
class InReadmeQualifier(InQualifier): | ||
term = "readme" | ||
|
||
|
||
class RepositoryQualifier(Qualifier): | ||
operator = "repo" | ||
|
||
def __init__(self, repository: str) -> None: | ||
""" | ||
Search within a repository | ||
Args: | ||
repository: owner/repo | ||
""" | ||
self.term = repository | ||
|
||
|
||
class OrganizationQualifier(Qualifier): | ||
operator = "org" | ||
|
||
def __init__(self, organization: str) -> None: | ||
""" | ||
Search within an organization | ||
Args: | ||
organization: Name of the organization to search within | ||
""" | ||
self.term = organization | ||
|
||
|
||
class UserQualifier(Qualifier): | ||
operator = "user" | ||
|
||
def __init__(self, user: str) -> None: | ||
""" | ||
Search within an user space | ||
Args: | ||
user: Name of the user | ||
""" | ||
self.term = user | ||
|
||
|
||
class IsPublicQualifier(Qualifier): | ||
operator = "is" | ||
term = "public" | ||
|
||
|
||
class IsPrivateQualifier(Qualifier): | ||
operator = "is" | ||
term = "private" |
Oops, something went wrong.