-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4a3d53f
commit 0ecd191
Showing
16 changed files
with
1,534 additions
and
6 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
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,14 @@ | ||
# Copyright Contributors to the Packit project. | ||
# SPDX-License-Identifier: MIT | ||
|
||
from ogr.services.forgejo.issue import ForgejoIssue | ||
from ogr.services.forgejo.project import ForgejoProject | ||
from ogr.services.forgejo.pull_request import ForgejoPullRequest | ||
from ogr.services.forgejo.service import ForgejoService | ||
|
||
__all__ = [ | ||
ForgejoPullRequest.__name__, | ||
ForgejoIssue.__name__, | ||
ForgejoProject.__name__, | ||
ForgejoService.__name__, | ||
] |
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,10 @@ | ||
# Copyright Contributors to the Packit project. | ||
# SPDX-License-Identifier: MIT | ||
|
||
from ogr.services import forgejo | ||
from ogr.services.base import BaseIssue | ||
|
||
|
||
class ForgejoIssue(BaseIssue): | ||
def __init__(self, raw_issue, project: "forgejo.ForgejoProject"): | ||
super().__init__(raw_issue, project) |
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,35 @@ | ||
# Copyright Contributors to the Packit project. | ||
# SPDX-License-Identifier: MIT | ||
|
||
|
||
from ogr.services import forgejo | ||
from ogr.services.base import BaseGitProject | ||
|
||
|
||
class ForgejoProject(BaseGitProject): | ||
service: "forgejo.ForgejoService" | ||
|
||
def __init__( | ||
self, | ||
repo: str, | ||
service: "forgejo.ForgejoService", | ||
namespace: str, | ||
**kwargs, | ||
): | ||
super().__init__(repo, service, namespace) | ||
self._forgejo_repo = None | ||
|
||
@property | ||
def forgejo_repo(self): | ||
if not self._forgejo_repo: | ||
if self.namespace: | ||
self._forgejo_repo = self.service.api.repository.repo_get( | ||
owner=self.namespace, | ||
repo=self.repo, | ||
) | ||
else: | ||
self._forgejo_repo = self.service.api.repository.repo_get( | ||
owner=self.service.user.get_username(), | ||
repo=self.repo, | ||
) | ||
return self._forgejo_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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# Copyright Contributors to the Packit project. | ||
# SPDX-License-Identifier: MIT | ||
|
||
from typing import Any | ||
|
||
from ogr.services import forgejo | ||
from ogr.services.base import BasePullRequest | ||
|
||
|
||
class ForgejoPullRequest(BasePullRequest): | ||
def __init__( | ||
self, | ||
raw_pr: Any, | ||
project: "forgejo.ForgejoProject", | ||
): | ||
super().__init__(raw_pr, project) |
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,89 @@ | ||
# Copyright Contributors to the Packit project. | ||
# SPDX-License-Identifier: MIT | ||
|
||
from typing import Optional | ||
from urllib.parse import urlparse | ||
|
||
from pyforgejo import PyforgejoApi | ||
|
||
from ogr.abstract import GitUser | ||
from ogr.exceptions import OgrException | ||
from ogr.factory import use_for_service | ||
from ogr.services.base import BaseGitService | ||
from ogr.services.forgejo.project import ForgejoProject | ||
from ogr.services.forgejo.user import ForgejoUser | ||
|
||
|
||
@use_for_service("forgejo") | ||
class ForgejoService(BaseGitService): | ||
version = "/api/v1" | ||
|
||
def __init__( | ||
self, | ||
instance_url: str = "https://forgejo.org", | ||
api_key: Optional[str] = None, | ||
**kwargs, | ||
): | ||
super().__init__() | ||
self.instance_url = instance_url + self.version | ||
self._token = f"token {api_key}" | ||
self._api = None | ||
|
||
@property | ||
def api(self): | ||
if not self._api: | ||
self._api = PyforgejoApi(base_url=self.instance_url, api_key=self._token) | ||
return self._api | ||
|
||
def get_project( # type: ignore[override] | ||
self, | ||
repo: str, | ||
namespace: str, | ||
**kwargs, | ||
) -> "ForgejoProject": | ||
return ForgejoProject( | ||
repo=repo, | ||
namespace=namespace, | ||
service=self, | ||
**kwargs, | ||
) | ||
|
||
@property | ||
def user(self) -> GitUser: | ||
return ForgejoUser(self) | ||
|
||
def project_create( | ||
self, | ||
repo: str, | ||
namespace: Optional[str] = None, | ||
description: Optional[str] = None, | ||
) -> "ForgejoProject": | ||
if namespace: | ||
new_repo = self.api.organization.create_org_repo( | ||
org=namespace, | ||
name=repo, | ||
description=description, | ||
) | ||
else: | ||
new_repo = self.api.repository.create_current_user_repo( | ||
name=repo, | ||
description=description, | ||
) | ||
return ForgejoProject( | ||
repo=repo, | ||
namespace=namespace, | ||
service=self, | ||
github_repo=new_repo, | ||
) | ||
|
||
def get_project_from_url(self, url: str) -> "ForgejoProject": | ||
parsed_url = urlparse(url) | ||
path_parts = parsed_url.path.strip("/").split("/") | ||
|
||
if len(path_parts) < 2: | ||
raise OgrException(f"Invalid Forgejo URL: {url}") | ||
|
||
namespace = path_parts[0] | ||
repo = path_parts[1] | ||
|
||
return self.get_project(repo=repo, namespace=namespace) |
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,26 @@ | ||
# Copyright Contributors to the Packit project. | ||
# SPDX-License-Identifier: MIT | ||
|
||
|
||
from ogr.services import forgejo | ||
from ogr.services.base import BaseGitUser | ||
|
||
|
||
class ForgejoUser(BaseGitUser): | ||
service: "forgejo.ForgejoService" | ||
|
||
def __init__(self, service: "forgejo.ForgejoService") -> None: | ||
super().__init__(service=service) | ||
self._forgejo_user = None | ||
|
||
def __str__(self) -> str: | ||
return f'ForgejoUser(username="{self.get_username()}")' | ||
|
||
@property | ||
def forgejo_user(self): | ||
if not self._forgejo_user: | ||
self._forgejo_user = self.service.api.user.get_current() | ||
return self._forgejo_user | ||
|
||
def get_username(self) -> str: | ||
return self.forgejo_user.login |
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 |
---|---|---|
|
@@ -43,6 +43,7 @@ dependencies = [ | |
"PyYAML", | ||
"requests", | ||
"urllib3", | ||
"pyforgejo", | ||
] | ||
|
||
[project.urls] | ||
|
4 changes: 4 additions & 0 deletions
4
tests/integration/factory/test_data/test_factory/FactoryTests.test_get_project_forgejo.yaml
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,4 @@ | ||
_requre: | ||
DataTypes: 1 | ||
key_strategy: StorageKeysInspectSimple | ||
version_storage_file: 3 |
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,2 @@ | ||
# Copyright Contributors to the Packit project. | ||
# SPDX-License-Identifier: MIT |
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,17 @@ | ||
# Copyright Contributors to the Packit project. | ||
# SPDX-License-Identifier: MIT | ||
|
||
import os | ||
|
||
import pytest | ||
|
||
from ogr.services.forgejo import ForgejoService | ||
|
||
|
||
@pytest.fixture | ||
def service(): | ||
api_key = os.environ.get("FORGEJO_TOKEN") | ||
return ForgejoService( | ||
instance_url="https://v10.next.forgejo.org", | ||
api_key=api_key, | ||
) |
Oops, something went wrong.