Skip to content

Commit

Permalink
Merge pull request #636 from nikromen/+1-to-the-comment-command
Browse files Browse the repository at this point in the history
Add Comment.react_to_the_comment, Comment.get_reactions, Reaction.delete

Support for reacting to comments, getting list of reactions and deleting reactions.
Also compatibility.md file - updated.

Fixes #630


Ogr now supports reacting to the comment with a given reaction, getting them in list and deleting them (only when reaction is added by using ogr API).

Reviewed-by: Matej Focko <None>
Reviewed-by: None <None>
  • Loading branch information
softwarefactory-project-zuul[bot] authored Sep 14, 2021
2 parents 1e50bf0 + 12a04ad commit 8dbbc23
Show file tree
Hide file tree
Showing 13 changed files with 6,184 additions and 6 deletions.
10 changes: 10 additions & 0 deletions COMPATIBILITY.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,16 @@ In case you find any error, please [create a new issue](https://github.com/packi
| | GitHub | GitLab | Pagure |
| ---------------- | :----: | :----: | :----: |
| `body` (get/set) | ✔/✔ | ✔/✔ | ✔/✘ |
| `add_reaction` ||||
| `get_reactions` ||||

### `PRComment`

| | GitHub | GitLab | Pagure |
| ---------------- | :----: | :----: | :----: |
| `body` (get/set) | ✔/✔ | ✔/✔ | ✔/✘ |
| `add_reaction` ||||
| `get_reactions` ||||

## Issue

Expand Down Expand Up @@ -60,3 +64,9 @@ In case you find any error, please [create a new issue](https://github.com/packi
| `get_projects` ||||
| `get_forks` ||||
| `get_email` ||||

## Reaction

| | GitHub | GitLab | Pagure |
| -------- | :----: | :----: | :----: |
| `delete` ||||
30 changes: 30 additions & 0 deletions ogr/abstract.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,18 @@ def __repr__(self) -> str:
return f"<{str(self)}>"


class Reaction(OgrAbstractClass):
def __init__(self, raw_reaction: Any) -> None:
self._raw_reaction = raw_reaction

def __str__(self):
return f"Reaction(raw_reaction={self._raw_reaction})"

def delete(self) -> None:
"""Delete a reaction."""
raise NotImplementedError()


class Comment(OgrAbstractClass):
def __init__(
self,
Expand Down Expand Up @@ -111,6 +123,24 @@ def created(self) -> datetime.datetime:
def edited(self) -> datetime.datetime:
return self._edited

def get_reactions(self) -> List[Reaction]:
"""Returns list of reactions."""
raise NotImplementedError()

def add_reaction(self, reaction: str) -> Reaction:
"""
Reacts to a comment.
Colons in between reaction are not needed: e.g. comment.add_reaction("+1")
Args:
reaction: String representing specific reaction to be added.
Returns:
Object representing newly added reaction.
"""
raise NotImplementedError()


class IssueComment(Comment):
@property
Expand Down
23 changes: 21 additions & 2 deletions ogr/services/github/comments.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,23 @@
# SOFTWARE.

import datetime
from typing import Union
from typing import List, Union

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 IssueComment, PRComment
from ogr.abstract import IssueComment, PRComment, Reaction


class GithubReaction(Reaction):
_raw_reaction: _Reaction

def __str__(self) -> str:
return "Github" + super().__str__()

def delete(self) -> None:
self._raw_reaction.delete()


class GithubComment:
Expand All @@ -49,6 +60,14 @@ def body(self, new_body: str) -> None:
def edited(self) -> datetime.datetime:
return self._raw_comment.updated_at

def get_reactions(self) -> List[Reaction]:
return [
GithubReaction(reaction) for reaction in self._raw_comment.get_reactions()
]

def add_reaction(self, reaction: str) -> GithubReaction:
return GithubReaction(self._raw_comment.create_reaction(reaction))


class GithubIssueComment(GithubComment, IssueComment):
def __str__(self) -> str:
Expand Down
53 changes: 50 additions & 3 deletions ogr/services/gitlab/comments.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,31 @@
# SOFTWARE.

import datetime
from typing import Union
import logging
from typing import List, Union

from gitlab.v4.objects import ProjectIssueNote, ProjectMergeRequestNote
import gitlab.exceptions
from gitlab.v4.objects import (
ProjectIssueNote,
ProjectMergeRequestNote,
ProjectIssueNoteAwardEmoji,
ProjectMergeRequestAwardEmoji,
)

from ogr.abstract import IssueComment, PRComment
from ogr.abstract import IssueComment, PRComment, Reaction
from ogr.exceptions import GitlabAPIException

logger = logging.getLogger(__name__)


class GitlabReaction(Reaction):
_raw_reaction: Union[ProjectIssueNoteAwardEmoji, ProjectMergeRequestAwardEmoji]

def __str__(self) -> str:
return "Gitlab" + super().__str__()

def delete(self) -> None:
self._raw_reaction.delete()


class GitlabComment:
Expand All @@ -49,6 +69,33 @@ def body(self, new_body: str) -> None:
def edited(self) -> datetime.datetime:
return self._raw_comment.updated_at

def get_reactions(self) -> List[Reaction]:
return [
GitlabReaction(reaction)
for reaction in self._raw_comment.awardemojis.list()
]

def add_reaction(self, reaction: str) -> GitlabReaction:
try:
reaction_obj = self._raw_comment.awardemojis.create({"name": reaction})
except gitlab.exceptions.GitlabCreateError as ex:
if "404 Award Emoji Name has already been taken" not in str(ex):
raise GitlabAPIException(ex)

# this happens only when the reaction was already added
logger.info(f"The emoji {reaction} has already been taken.")
(reaction_obj,) = filter(
(
# we want to return that already given reaction
lambda item: item.attributes["name"] == reaction
and item.attributes["user"]["name"]
== item.awardemojis.gitlab.user.name
),
self._raw_comment.awardemojis.list(),
)

return GitlabReaction(reaction_obj)


class GitlabIssueComment(GitlabComment, IssueComment):
def __str__(self) -> str:
Expand Down
2 changes: 1 addition & 1 deletion ogr/services/pagure/comments.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
# SOFTWARE.

import datetime
from typing import Optional, Dict, Any
from typing import Any, Dict, Optional

from ogr.abstract import Comment, IssueComment, PRComment
from ogr.exceptions import OperationNotSupported
Expand Down
43 changes: 43 additions & 0 deletions tests/integration/github/test_comments.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from datetime import datetime

from requre.online_replacing import record_requests_for_all_methods

from tests.integration.github.base import GithubTests
Expand Down Expand Up @@ -121,3 +123,44 @@ def test_pr_comments_updates(self):

comments[0].body = before_comment
assert comments[0].body == before_comment

def test_pr_react_to_comment_and_delete(self):
pr = self.service.get_project(repo="playground", namespace="nikromen").get_pr(4)
pr_comment = pr.comment(datetime.now().strftime("%m/%d/%Y"))

reaction = pr_comment.add_reaction("+1")
assert len(pr_comment.get_reactions()) == 1

reaction.delete()
assert len(pr_comment.get_reactions()) == 0

def test_issue_react_to_comment_and_delete(self):
issue = self.service.get_project(
repo="playground", namespace="nikromen"
).get_issue(5)
issue_comment = issue.comment(datetime.now().strftime("%m/%d/%Y"))

reaction = issue_comment.add_reaction("confused")
assert len(issue_comment.get_reactions()) == 1

reaction.delete()
assert len(issue_comment.get_reactions()) == 0

def test_get_reactions(self):
pr = self.service.get_project(repo="playground", namespace="nikromen").get_pr(4)
pr_comment = pr.comment(datetime.now().strftime("%m/%d/%Y"))

pr_comment.add_reaction("+1")
pr_comment.add_reaction("-1")
pr_comment.add_reaction("confused")
assert len(pr_comment.get_reactions()) == 3

issue = self.service.get_project(
repo="playground", namespace="nikromen"
).get_issue(5)
issue_comment = issue.comment(datetime.now().strftime("%m/%d/%Y"))

issue_comment.add_reaction("+1")
issue_comment.add_reaction("-1")
issue_comment.add_reaction("confused")
assert len(pr_comment.get_reactions()) == 3
Loading

0 comments on commit 8dbbc23

Please sign in to comment.