Skip to content

Commit afbced5

Browse files
marci4m.prestel
and
m.prestel
authored
Add pin/unpin & pinned_comments (#1888)
--------- Co-authored-by: m.prestel <[email protected]>
1 parent 91b5c27 commit afbced5

File tree

3 files changed

+85
-0
lines changed

3 files changed

+85
-0
lines changed

jira/client.py

+34
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@
7474
IssueTypeScheme,
7575
NotificationScheme,
7676
PermissionScheme,
77+
PinnedComment,
7778
Priority,
7879
PriorityScheme,
7980
Project,
@@ -5637,3 +5638,36 @@ def move_to_backlog(self, issue_keys: list[str]) -> Response:
56375638
url = self._get_url("backlog/issue", base=self.AGILE_BASE_URL)
56385639
payload = {"issues": issue_keys} # TODO: should be list of issues
56395640
return self._session.post(url, data=json.dumps(payload))
5641+
5642+
@translate_resource_args
5643+
def pinned_comments(self, issue: int | str) -> list[PinnedComment]:
5644+
"""Get a list of pinned comment Resources of the issue provided.
5645+
5646+
Args:
5647+
issue (Union[int, str]): the issue ID or key to get the comments from
5648+
5649+
Returns:
5650+
List[PinnedComment]
5651+
"""
5652+
r_json = self._get_json(f"issue/{issue}/pinned-comments", params={})
5653+
5654+
pinned_comments = [
5655+
PinnedComment(self._options, self._session, raw_comment_json)
5656+
for raw_comment_json in r_json
5657+
]
5658+
return pinned_comments
5659+
5660+
@translate_resource_args
5661+
def pin_comment(self, issue: int | str, comment: int | str, pin: bool) -> Response:
5662+
"""Pin/Unpin a comment on the issue.
5663+
5664+
Args:
5665+
issue (Union[int, str]): the issue ID or key to get the comments from
5666+
comment (Union[int, str]): the comment ID
5667+
pin (bool): Pin (True) or Unpin (False)
5668+
5669+
Returns:
5670+
Response
5671+
"""
5672+
url = self._get_url("issue/" + str(issue) + "/comment/" + str(comment) + "/pin")
5673+
return self._session.put(url, data=str(pin).lower())

jira/resources.py

+17
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ class AnyLike:
6969
"ServiceDesk",
7070
"RequestType",
7171
"resource_class_map",
72+
"PinnedComment",
7273
)
7374

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

957958

959+
class PinnedComment(Resource):
960+
"""Pinned comment on an issue."""
961+
962+
def __init__(
963+
self,
964+
options: dict[str, str],
965+
session: ResilientSession,
966+
raw: dict[str, Any] | None = None,
967+
):
968+
Resource.__init__(self, "issue/{0}/pinned-comments", options, session)
969+
if raw:
970+
self._parse_raw(raw)
971+
self.raw: dict[str, Any] = cast(dict[str, Any], self.raw)
972+
973+
958974
class RemoteLink(Resource):
959975
"""A link to a remote application from an issue."""
960976

@@ -1659,6 +1675,7 @@ def dict2resource(
16591675
r"filter/[^/]$": Filter,
16601676
r"issue/[^/]+$": Issue,
16611677
r"issue/[^/]+/comment/[^/]+$": Comment,
1678+
r"issue/[^/]+/pinned-comments$": PinnedComment,
16621679
r"issue/[^/]+/votes$": Votes,
16631680
r"issue/[^/]+/watchers$": Watchers,
16641681
r"issue/[^/]+/worklog/[^/]+$": Worklog,
+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
from __future__ import annotations
2+
3+
from tests.conftest import JiraTestCase
4+
5+
6+
class PinnedCommentTests(JiraTestCase):
7+
def setUp(self):
8+
JiraTestCase.setUp(self)
9+
self.issue_1_key = self.test_manager.project_b_issue1
10+
self.issue_2_key = self.test_manager.project_b_issue2
11+
self.issue_3_key = self.test_manager.project_b_issue3
12+
13+
def tearDown(self) -> None:
14+
for issue in [self.issue_1_key, self.issue_2_key, self.issue_3_key]:
15+
for comment in self.jira.comments(issue):
16+
comment.delete()
17+
18+
def test_pincomments(self):
19+
for issue in [self.issue_1_key, self.jira.issue(self.issue_2_key)]:
20+
self.jira.issue(issue)
21+
comment1 = self.jira.add_comment(issue, "First comment")
22+
self.jira.pin_comment(comment1.id, True)
23+
comment2 = self.jira.add_comment(issue, "Second comment")
24+
self.jira.pin_comment(comment2.id, True)
25+
pinned_comments = self.jira.pinned_comments(issue)
26+
assert pinned_comments[0].comment.body == "First comment"
27+
assert pinned_comments[1].comment.body == "Second comment"
28+
self.jira.pin_comment(comment1.id, False)
29+
pinned_comments = self.jira.pinned_comments(issue)
30+
assert len(pinned_comments) == 1
31+
assert pinned_comments[0].comment.body == "Second comment"
32+
self.jira.pin_comment(comment2.id, False)
33+
pinned_comments = self.jira.pinned_comments(issue)
34+
assert len(pinned_comments) == 0

0 commit comments

Comments
 (0)