-
Notifications
You must be signed in to change notification settings - Fork 55
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
Implement get_commit_comment method and refactor CommitComment #865
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,11 +4,12 @@ | |
import datetime | ||
from typing import Union | ||
|
||
from github.CommitComment import CommitComment as _GithubCommitComment | ||
from github.IssueComment import IssueComment as _GithubIssueComment | ||
from github.PullRequestComment import PullRequestComment as _GithubPullRequestComment | ||
from github.Reaction import Reaction as _Reaction | ||
|
||
from ogr.abstract import Comment, IssueComment, PRComment, Reaction | ||
from ogr.abstract import Comment, CommitComment, IssueComment, PRComment, Reaction | ||
|
||
|
||
class GithubReaction(Reaction): | ||
|
@@ -24,7 +25,11 @@ def delete(self) -> None: | |
class GithubComment(Comment): | ||
def _from_raw_comment( | ||
self, | ||
raw_comment: Union[_GithubIssueComment, _GithubPullRequestComment], | ||
raw_comment: Union[ | ||
_GithubIssueComment, | ||
_GithubPullRequestComment, | ||
_GithubCommitComment, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. so the properties will work as they should? 👀 nice! |
||
], | ||
) -> None: | ||
self._raw_comment = raw_comment | ||
self._id = raw_comment.id | ||
|
@@ -60,3 +65,8 @@ def __str__(self) -> str: | |
class GithubPRComment(GithubComment, PRComment): | ||
def __str__(self) -> str: | ||
return "Github" + super().__str__() | ||
|
||
|
||
class GithubCommitComment(GithubComment, CommitComment): | ||
def __str__(self) -> str: | ||
return "Github" + super().__str__() |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,20 +7,25 @@ | |
|
||
import gitlab.exceptions | ||
from gitlab.v4.objects import ( | ||
ProjectCommitComment, | ||
ProjectCommitDiscussionNote, | ||
ProjectIssueNote, | ||
ProjectIssueNoteAwardEmoji, | ||
ProjectMergeRequestAwardEmoji, | ||
ProjectMergeRequestNote, | ||
) | ||
|
||
from ogr.abstract import Comment, IssueComment, PRComment, Reaction | ||
from ogr.exceptions import GitlabAPIException | ||
from ogr.abstract import Comment, CommitComment, IssueComment, PRComment, Reaction | ||
from ogr.exceptions import GitlabAPIException, OperationNotSupported | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class GitlabReaction(Reaction): | ||
_raw_reaction: Union[ProjectIssueNoteAwardEmoji, ProjectMergeRequestAwardEmoji] | ||
_raw_reaction: Union[ | ||
ProjectIssueNoteAwardEmoji, | ||
ProjectMergeRequestAwardEmoji, | ||
] | ||
|
||
def __str__(self) -> str: | ||
return "Gitlab" + super().__str__() | ||
|
@@ -32,7 +37,12 @@ def delete(self) -> None: | |
class GitlabComment(Comment): | ||
def _from_raw_comment( | ||
self, | ||
raw_comment: Union[ProjectIssueNote, ProjectMergeRequestNote], | ||
raw_comment: Union[ | ||
ProjectIssueNote, | ||
ProjectMergeRequestNote, | ||
ProjectCommitDiscussionNote, | ||
ProjectCommitComment, | ||
], | ||
) -> None: | ||
self._raw_comment = raw_comment | ||
self._id = raw_comment.get_id() | ||
|
@@ -94,3 +104,36 @@ def __str__(self) -> str: | |
class GitlabPRComment(GitlabComment, PRComment): | ||
def __str__(self) -> str: | ||
return "Gitlab" + super().__str__() | ||
|
||
|
||
class GitlabCommitComment(GitlabComment, CommitComment): | ||
def __str__(self) -> str: | ||
return "Gitlab" + super().__str__() | ||
|
||
@property | ||
def body(self) -> str: | ||
# TODO: ideally, the raw comment should be of the same type for both | ||
# individual and all comments retrievals, this comes from the | ||
# Gitlab API inconsistency (see get_commit_comment vs get_commit_comments) | ||
if isinstance(self._raw_comment, ProjectCommitComment): | ||
return self._raw_comment.note | ||
|
||
return self._raw_comment.body | ||
|
||
@body.setter | ||
def body(self, new_body: str) -> None: | ||
if isinstance(self._raw_comment, ProjectCommitComment): | ||
self._raw_comment.note = new_body | ||
else: | ||
self._raw_comment.body = new_body | ||
self._raw_comment.save() | ||
|
||
def get_reactions(self) -> list[Reaction]: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. oh shoot |
||
raise OperationNotSupported( | ||
"Interacting with award emojis on commit comments is not supported via API.", | ||
) | ||
|
||
def add_reaction(self, reaction: str) -> GitlabReaction: | ||
raise OperationNotSupported( | ||
"Interacting with award emojis on commit comments is not supported via API.", | ||
) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,6 +25,7 @@ | |
from ogr.exceptions import GitlabAPIException, OperationNotSupported | ||
from ogr.services import gitlab as ogr_gitlab | ||
from ogr.services.base import BaseGitProject | ||
from ogr.services.gitlab.comments import GitlabCommitComment | ||
from ogr.services.gitlab.flag import GitlabCommitFlag | ||
from ogr.services.gitlab.issue import GitlabIssue | ||
from ogr.services.gitlab.pull_request import GitlabPullRequest | ||
|
@@ -294,11 +295,10 @@ def commit_comment( | |
return self._commit_comment_from_gitlab_object(raw_comment, commit) | ||
|
||
@staticmethod | ||
def _commit_comment_from_gitlab_object(raw_comment, commit) -> CommitComment: | ||
return CommitComment( | ||
def _commit_comment_from_gitlab_object(raw_comment, commit: str) -> CommitComment: | ||
return GitlabCommitComment( | ||
raw_comment=raw_comment, | ||
sha=commit, | ||
body=raw_comment.note, | ||
author=raw_comment.author["username"], | ||
) | ||
|
||
def get_commit_comments(self, commit: str) -> list[CommitComment]: | ||
|
@@ -313,6 +313,38 @@ def get_commit_comments(self, commit: str) -> list[CommitComment]: | |
for comment in commit_object.comments.list() | ||
] | ||
|
||
def get_commit_comment(self, commit_sha: str, comment_id: int) -> CommitComment: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure what's the library for, if we need to do all the dirty work anyways… |
||
try: | ||
commit_object: ProjectCommit = self.gitlab_repo.commits.get(commit_sha) | ||
except gitlab.exceptions.GitlabGetError as ex: | ||
logger.error(f"Commit with SHA {commit_sha} was not found: {ex}") | ||
raise GitlabAPIException( | ||
f"Commit with SHA {commit_sha} was not found.", | ||
) from ex | ||
|
||
try: | ||
discussions = commit_object.discussions.list(all=True) | ||
comment = None | ||
|
||
for discussion in discussions: | ||
note_ids = [note["id"] for note in discussion.attributes["notes"]] | ||
if comment_id in note_ids: | ||
comment = discussion.notes.get(comment_id) | ||
break | ||
|
||
if comment is None: | ||
raise GitlabAPIException( | ||
f"Comment with ID {comment_id} not found in commit {commit_sha}.", | ||
) | ||
|
||
except gitlab.exceptions.GitlabGetError as ex: | ||
logger.error(f"Failed to retrieve comment with ID {comment_id}: {ex}") | ||
raise GitlabAPIException( | ||
f"Failed to retrieve comment with ID {comment_id}.", | ||
) from ex | ||
|
||
return self._commit_comment_from_gitlab_object(comment, commit_sha) | ||
|
||
@indirect(GitlabCommitFlag.set) | ||
def set_commit_status( | ||
self, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -482,6 +482,9 @@ def commit_comment( | |
def get_commit_comments(self, commit: str) -> list[CommitComment]: | ||
raise OperationNotSupported("Commit comments are not supported on Pagure.") | ||
|
||
def get_commit_comment(self, commit_sha: str, comment_id: int) -> CommitComment: | ||
raise OperationNotSupported("Commit comments are not supported on Pagure.") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. … |
||
|
||
@if_readonly(return_function=GitProjectReadOnly.set_commit_status) | ||
@indirect(PagureCommitFlag.set) | ||
def set_commit_status( | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ya… this should be an abstract class, it would've been much easier to see unimplemented methods and missing functionality (e.g., on Pagure' side), but that sounds more like a Friday thing than including this in here…