From b9163c410e45f1ab33d2c9e13e1888e22b1eb58b Mon Sep 17 00:00:00 2001 From: "m.prestel" Date: Mon, 26 Aug 2024 19:27:14 +0200 Subject: [PATCH 1/4] Add pin/unpin & pinned_comments --- jira/client.py | 34 +++++++++++++++++++++++++ jira/resources.py | 14 +++++++++++ tests/resources/test_pinned_comment.py | 35 ++++++++++++++++++++++++++ 3 files changed, 83 insertions(+) create mode 100644 tests/resources/test_pinned_comment.py diff --git a/jira/client.py b/jira/client.py index 851b7f5d8..09621cdbd 100644 --- a/jira/client.py +++ b/jira/client.py @@ -74,6 +74,7 @@ IssueTypeScheme, NotificationScheme, PermissionScheme, + PinnedComment, Priority, PriorityScheme, Project, @@ -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()) \ No newline at end of file diff --git a/jira/resources.py b/jira/resources.py index e72b7f456..b85bffc4b 100644 --- a/jira/resources.py +++ b/jira/resources.py @@ -954,6 +954,19 @@ 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, + ): + 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.""" @@ -1659,6 +1672,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, diff --git a/tests/resources/test_pinned_comment.py b/tests/resources/test_pinned_comment.py new file mode 100644 index 000000000..4c5a5fd7b --- /dev/null +++ b/tests/resources/test_pinned_comment.py @@ -0,0 +1,35 @@ +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 + From b318accc1f3a74b3c989efcb23cf608ca9c48deb Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 26 Aug 2024 17:27:49 +0000 Subject: [PATCH 2/4] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- jira/client.py | 8 ++++---- jira/resources.py | 2 ++ tests/resources/test_pinned_comment.py | 1 - 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/jira/client.py b/jira/client.py index 09621cdbd..ec4327acd 100644 --- a/jira/client.py +++ b/jira/client.py @@ -5653,9 +5653,9 @@ def pinned_comments(self, issue: int | str) -> list[PinnedComment]: 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: + def pin_comment(self, issue: int | str, comment: int | str, pin: bool) -> Response: """Pin/Unpin a comment on the issue. Args: @@ -5666,5 +5666,5 @@ def pin_comment(self, issue: int | str , comment: int | str, pin: bool) -> Respo Returns: Response """ - url = self._get_url("issue/" + str(issue) + "/comment/"+ str(comment) + "/pin") - return self._session.put(url, data=str(pin).lower()) \ No newline at end of file + url = self._get_url("issue/" + str(issue) + "/comment/" + str(comment) + "/pin") + return self._session.put(url, data=str(pin).lower()) diff --git a/jira/resources.py b/jira/resources.py index b85bffc4b..5ceb861ac 100644 --- a/jira/resources.py +++ b/jira/resources.py @@ -954,6 +954,7 @@ def update( # type: ignore[override] super().update(async_=async_, jira=jira, notify=notify, fields=data) + class PinnedComment(Resource): """Pinned comment on an issue.""" @@ -968,6 +969,7 @@ def __init__( 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.""" diff --git a/tests/resources/test_pinned_comment.py b/tests/resources/test_pinned_comment.py index 4c5a5fd7b..5170d89f6 100644 --- a/tests/resources/test_pinned_comment.py +++ b/tests/resources/test_pinned_comment.py @@ -32,4 +32,3 @@ def test_pincomments(self): self.jira.pin_comment(comment2.id, False) pinned_comments = self.jira.pinned_comments(issue) assert len(pinned_comments) == 0 - From eff3bb61bfa6ee3d441ee390f7e62ea30e5c4453 Mon Sep 17 00:00:00 2001 From: "m.prestel" Date: Mon, 26 Aug 2024 19:36:53 +0200 Subject: [PATCH 3/4] Fix Lint & Try to fix docs --- jira/resources.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/jira/resources.py b/jira/resources.py index 5ceb861ac..d66d235c5 100644 --- a/jira/resources.py +++ b/jira/resources.py @@ -69,6 +69,7 @@ class AnyLike: "ServiceDesk", "RequestType", "resource_class_map", + "PinnedComment" ) logging.getLogger("jira").addHandler(logging.NullHandler()) @@ -962,7 +963,7 @@ def __init__( self, options: dict[str, str], session: ResilientSession, - raw: dict[str, Any] = None, + raw: dict[str, Any] | None = None, ): Resource.__init__(self, "issue/{0}/pinned-comments", options, session) if raw: From f0cceef7980be3e204383fb9dd22c276d8727ea5 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 26 Aug 2024 17:37:27 +0000 Subject: [PATCH 4/4] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- jira/resources.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/jira/resources.py b/jira/resources.py index d66d235c5..29c86e604 100644 --- a/jira/resources.py +++ b/jira/resources.py @@ -69,7 +69,7 @@ class AnyLike: "ServiceDesk", "RequestType", "resource_class_map", - "PinnedComment" + "PinnedComment", ) logging.getLogger("jira").addHandler(logging.NullHandler())