Skip to content
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

Add pin/unpin & pinned_comments #1888

Merged
merged 4 commits into from
Sep 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions jira/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
IssueTypeScheme,
NotificationScheme,
PermissionScheme,
PinnedComment,
Priority,
PriorityScheme,
Project,
Expand Down Expand Up @@ -5634,3 +5635,36 @@ def move_to_backlog(self, issue_keys: list[str]) -> Response:
url = self._get_url("backlog/issue", base=self.AGILE_BASE_URL)
payload = {"issues": issue_keys} # TODO: should be list of issues
return self._session.post(url, data=json.dumps(payload))

@translate_resource_args
def pinned_comments(self, issue: int | str) -> list[PinnedComment]:
"""Get a list of pinned comment Resources of the issue provided.

Args:
issue (Union[int, str]): the issue ID or key to get the comments from

Returns:
List[PinnedComment]
"""
r_json = self._get_json(f"issue/{issue}/pinned-comments", params={})

pinned_comments = [
PinnedComment(self._options, self._session, raw_comment_json)
for raw_comment_json in r_json
]
return pinned_comments

@translate_resource_args
def pin_comment(self, issue: int | str, comment: int | str, pin: bool) -> Response:
"""Pin/Unpin a comment on the issue.

Args:
issue (Union[int, str]): the issue ID or key to get the comments from
comment (Union[int, str]): the comment ID
pin (bool): Pin (True) or Unpin (False)

Returns:
Response
"""
url = self._get_url("issue/" + str(issue) + "/comment/" + str(comment) + "/pin")
return self._session.put(url, data=str(pin).lower())
17 changes: 17 additions & 0 deletions jira/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ class AnyLike:
"ServiceDesk",
"RequestType",
"resource_class_map",
"PinnedComment",
)

logging.getLogger("jira").addHandler(logging.NullHandler())
Expand Down Expand Up @@ -955,6 +956,21 @@ def update( # type: ignore[override]
super().update(async_=async_, jira=jira, notify=notify, fields=data)


class PinnedComment(Resource):
"""Pinned comment on an issue."""

def __init__(
self,
options: dict[str, str],
session: ResilientSession,
raw: dict[str, Any] | None = None,
):
Resource.__init__(self, "issue/{0}/pinned-comments", options, session)
if raw:
self._parse_raw(raw)
self.raw: dict[str, Any] = cast(dict[str, Any], self.raw)


class RemoteLink(Resource):
"""A link to a remote application from an issue."""

Expand Down Expand Up @@ -1659,6 +1675,7 @@ def dict2resource(
r"filter/[^/]$": Filter,
r"issue/[^/]+$": Issue,
r"issue/[^/]+/comment/[^/]+$": Comment,
r"issue/[^/]+/pinned-comments$": PinnedComment,
r"issue/[^/]+/votes$": Votes,
r"issue/[^/]+/watchers$": Watchers,
r"issue/[^/]+/worklog/[^/]+$": Worklog,
Expand Down
34 changes: 34 additions & 0 deletions tests/resources/test_pinned_comment.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
from __future__ import annotations

from tests.conftest import JiraTestCase


class PinnedCommentTests(JiraTestCase):
def setUp(self):
JiraTestCase.setUp(self)
self.issue_1_key = self.test_manager.project_b_issue1
self.issue_2_key = self.test_manager.project_b_issue2
self.issue_3_key = self.test_manager.project_b_issue3

def tearDown(self) -> None:
for issue in [self.issue_1_key, self.issue_2_key, self.issue_3_key]:
for comment in self.jira.comments(issue):
comment.delete()

def test_pincomments(self):
for issue in [self.issue_1_key, self.jira.issue(self.issue_2_key)]:
self.jira.issue(issue)
comment1 = self.jira.add_comment(issue, "First comment")
self.jira.pin_comment(comment1.id, True)
comment2 = self.jira.add_comment(issue, "Second comment")
self.jira.pin_comment(comment2.id, True)
pinned_comments = self.jira.pinned_comments(issue)
assert pinned_comments[0].comment.body == "First comment"
assert pinned_comments[1].comment.body == "Second comment"
self.jira.pin_comment(comment1.id, False)
pinned_comments = self.jira.pinned_comments(issue)
assert len(pinned_comments) == 1
assert pinned_comments[0].comment.body == "Second comment"
self.jira.pin_comment(comment2.id, False)
pinned_comments = self.jira.pinned_comments(issue)
assert len(pinned_comments) == 0
Loading