Skip to content

Commit 79257db

Browse files
mynk8betulependule
authored andcommitted
Implement Forgejo event handling in Packit Service
1 parent 26445c7 commit 79257db

10 files changed

Lines changed: 636 additions & 0 deletions

File tree

packit_service/events/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
copr,
88
enums,
99
event,
10+
forgejo,
1011
github,
1112
gitlab,
1213
koji,
@@ -22,6 +23,7 @@
2223
anitya.__name__,
2324
github.__name__,
2425
gitlab.__name__,
26+
forgejo.__name__,
2527
koji.__name__,
2628
openscanhub.__name__,
2729
pagure.__name__,
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Copyright Contributors to the Packit project.
2+
# SPDX-License-Identifier: MIT
3+
4+
from . import abstract, issue, pr, push
5+
6+
__all__ = [abstract.__name__, push.__name__, issue.__name__, pr.__name__]
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Copyright Contributors to the Packit project.
2+
# SPDX-License-Identifier: MIT
3+
4+
from typing import Optional
5+
6+
from ..abstract.base import ForgeIndependent
7+
8+
9+
class ForgejoEvent(ForgeIndependent):
10+
def __init__(self, project_url: str, pr_id: Optional[int] = None, **kwargs):
11+
super().__init__(pr_id=pr_id)
12+
self.project_url: str = project_url
13+
# git ref that can be 'git checkout'-ed
14+
self.git_ref: Optional[str] = None
15+
self.identifier: Optional[str] = (
16+
None # will be shown to users -- e.g. in logs or in the copr-project name
17+
)
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
# Copyright Contributors to the Packit project.
2+
# SPDX-License-Identifier: MIT
3+
4+
# SPDX-License-Identifier: MIT
5+
6+
from typing import Optional
7+
8+
from ogr.abstract import Comment as OgrComment
9+
10+
from ..abstract.comment import Issue as AbstractIssueCommentEvent
11+
from ..enums import IssueCommentAction
12+
from .abstract import ForgejoEvent
13+
14+
15+
class Comment(AbstractIssueCommentEvent, ForgejoEvent):
16+
def __init__(
17+
self,
18+
action: IssueCommentAction,
19+
issue_id: int,
20+
repo_namespace: str,
21+
repo_name: str,
22+
target_repo: str,
23+
project_url: str,
24+
actor: str,
25+
comment: str,
26+
comment_id: int,
27+
tag_name: str = "",
28+
base_ref: Optional[str] = "main",
29+
comment_object: Optional[OgrComment] = None,
30+
dist_git_project_url=None,
31+
) -> None:
32+
super().__init__(
33+
issue_id=issue_id,
34+
repo_namespace=repo_namespace,
35+
repo_name=repo_name,
36+
project_url=project_url,
37+
comment=comment,
38+
comment_id=comment_id,
39+
tag_name=tag_name,
40+
comment_object=comment_object,
41+
dist_git_project_url=dist_git_project_url,
42+
)
43+
self.action = action
44+
self.actor = actor
45+
self.base_ref = base_ref
46+
self.target_repo = target_repo
47+
self.identifier = str(issue_id)
48+
49+
@classmethod
50+
def event_type(cls) -> str:
51+
return "forgejo.issue.Comment"
52+
53+
@property
54+
def tag_name(self):
55+
"""
56+
For Forgejo issue comments, return the tag_name passed in constructor
57+
without making API calls to avoid authentication issues.
58+
"""
59+
return self._tag_name
60+
61+
@tag_name.setter
62+
def tag_name(self, value: str) -> None:
63+
self._tag_name = value
64+
65+
@property
66+
def commit_sha(self) -> Optional[str]:
67+
"""
68+
For Forgejo issue comments, return the commit_sha passed in constructor
69+
without making API calls to avoid authentication issues.
70+
"""
71+
return self._commit_sha
72+
73+
@commit_sha.setter
74+
def commit_sha(self, value: Optional[str]) -> None:
75+
self._commit_sha = value
76+
77+
def get_dict(self, default_dict: Optional[dict] = None) -> dict:
78+
"""
79+
Override get_dict to avoid accessing properties that make API calls.
80+
"""
81+
# Get the basic dict from CommentEvent, not from Issue to avoid tag_name access
82+
from ..abstract.comment import CommentEvent
83+
84+
result = CommentEvent.get_dict(self, default_dict=default_dict)
85+
86+
# Add the specific fields we need without triggering API calls
87+
result["action"] = self.action.value
88+
result["issue_id"] = self.issue_id
89+
result["tag_name"] = self._tag_name # Use the private attribute directly
90+
result["commit_sha"] = self._commit_sha # Use the private attribute directly
91+
92+
return result
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
# Copyright Contributors to the Packit project.
2+
# SPDX-License-Identifier: MIT
3+
4+
from typing import Optional
5+
6+
from ogr.abstract import Comment as OgrComment
7+
from ogr.abstract import GitProject
8+
9+
from packit_service.service.db_project_events import AddPullRequestEventToDb
10+
11+
from ..abstract.comment import PullRequest as AbstractPRCommentEvent
12+
from ..enums import PullRequestAction, PullRequestCommentAction
13+
from .abstract import ForgejoEvent
14+
15+
16+
class Action(AddPullRequestEventToDb, ForgejoEvent):
17+
def __init__(
18+
self,
19+
action: PullRequestAction,
20+
pr_id: int,
21+
base_repo_namespace: str,
22+
base_repo_name: str,
23+
base_ref: str,
24+
target_repo_namespace: str,
25+
target_repo_name: str,
26+
project_url: str,
27+
commit_sha: str,
28+
commit_sha_before: str,
29+
actor: str,
30+
body: str,
31+
):
32+
super().__init__(project_url=project_url, pr_id=pr_id)
33+
self.action = action
34+
self.base_repo_namespace = base_repo_namespace
35+
self.base_repo_name = base_repo_name
36+
self.base_ref = base_ref
37+
self.target_repo_namespace = target_repo_namespace
38+
self.target_repo_name = target_repo_name
39+
self.commit_sha = commit_sha
40+
self.commit_sha_before = commit_sha_before
41+
self.actor = actor
42+
self.identifier = str(pr_id)
43+
self._pr_id = pr_id
44+
self.git_ref = None # use pr_id for checkout
45+
self.body = body
46+
47+
def get_dict(self, default_dict: Optional[dict] = None) -> dict:
48+
result = super().get_dict()
49+
result["action"] = result["action"].value
50+
return result
51+
52+
@classmethod
53+
def event_type(cls) -> str:
54+
return "forgejo.pr.Action"
55+
56+
def get_base_project(self) -> GitProject:
57+
return self.project.service.get_project(
58+
namespace=self.target_repo_namespace,
59+
repo=self.target_repo_name,
60+
)
61+
62+
63+
class Comment(AbstractPRCommentEvent, ForgejoEvent):
64+
def __init__(
65+
self,
66+
action: PullRequestCommentAction,
67+
pr_id: int,
68+
base_repo_namespace: str,
69+
base_repo_name: Optional[str],
70+
base_ref: Optional[str],
71+
target_repo_namespace: str,
72+
target_repo_name: str,
73+
project_url: str,
74+
actor: str,
75+
comment: str,
76+
comment_id: int,
77+
commit_sha: Optional[str] = None,
78+
comment_object: Optional[OgrComment] = None,
79+
) -> None:
80+
super().__init__(
81+
pr_id=pr_id,
82+
project_url=project_url,
83+
comment=comment,
84+
comment_id=comment_id,
85+
commit_sha=commit_sha,
86+
comment_object=comment_object,
87+
)
88+
self.action = action
89+
self.base_repo_namespace = base_repo_namespace
90+
self.base_repo_name = base_repo_name
91+
self.base_ref = base_ref
92+
self.target_repo_namespace = target_repo_namespace
93+
self.target_repo_name = target_repo_name
94+
self.actor = actor
95+
self.identifier = str(pr_id)
96+
self.git_ref = None
97+
self.pr_id = pr_id
98+
99+
@classmethod
100+
def event_type(cls) -> str:
101+
return "forgejo.pr.Comment"
102+
103+
def get_dict(self, default_dict: Optional[dict] = None) -> dict:
104+
"""
105+
Override get_dict to avoid accessing properties that make API calls.
106+
Use private attributes directly, similar to forgejo/issue.py.
107+
"""
108+
from ..abstract.comment import CommentEvent
109+
110+
result = CommentEvent.get_dict(self, default_dict=default_dict)
111+
result.pop("_comment_object")
112+
result["action"] = self.action.value
113+
result["pr_id"] = self.pr_id
114+
result["commit_sha"] = self._commit_sha
115+
return result
116+
117+
def get_base_project(self) -> GitProject:
118+
return self.project.service.get_project(
119+
namespace=self.target_repo_namespace,
120+
repo=self.target_repo_name,
121+
)
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Copyright Contributors to the Packit project.
2+
# SPDX-License-Identifier: MIT
3+
4+
from packit_service.service.db_project_events import AddBranchPushEventToDb
5+
6+
from .abstract import ForgejoEvent
7+
8+
9+
class Commit(AddBranchPushEventToDb, ForgejoEvent):
10+
def __init__(
11+
self,
12+
repo_namespace: str,
13+
repo_name: str,
14+
git_ref: str,
15+
project_url: str,
16+
commit_sha: str,
17+
commit_sha_before: str,
18+
):
19+
super().__init__(project_url=project_url)
20+
self.repo_namespace = repo_namespace
21+
self.repo_name = repo_name
22+
self.git_ref = git_ref
23+
self.commit_sha = commit_sha
24+
self.commit_sha_before = commit_sha_before
25+
self.identifier = git_ref
26+
27+
@classmethod
28+
def event_type(cls) -> str:
29+
return "forgejo.push.Commit"

0 commit comments

Comments
 (0)