|
| 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 | + ) |
0 commit comments