From 2ada66b521b95582e69c50fa75257fac5697e11d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dennis=20V=C3=A6rum?= <6872940+dvaerum@users.noreply.github.com> Date: Thu, 15 Jul 2021 09:02:48 +0200 Subject: [PATCH 1/8] Add subscriptable support for PropertyHolder MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This would be a simple, nice to have feature for dealing with custom fields 😃 Alternatively, I will just keep patching it in ```python from jira import resources as jira_resources def _jira_issue_property_holder_get_item(cls, item): if item.startswith("_"): raise KeyError(item) else: return getattr(cls, item) jira_resources.PropertyHolder._getitem__ = _jira_issue_property_holder_get_item ``` --- jira/resources.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/jira/resources.py b/jira/resources.py index faf43b3f0..b00d40909 100644 --- a/jira/resources.py +++ b/jira/resources.py @@ -1431,3 +1431,9 @@ def cls_for_resource(resource_literal: str) -> Type[Resource]: class PropertyHolder(object): def __init__(self, raw): __bases__ = raw # noqa + + def __getitem__(self, item) + if item.startswith("_"): + raise KeyError(item) + else: + return getattr(self, item) From 2eaca23d9079d15733096b796e0529366b1ae371 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dennis=20Vestergaard=20V=C3=A6rum?= Date: Mon, 26 Jul 2021 13:17:59 +0200 Subject: [PATCH 2/8] Implemented get_field method for Issue class --- jira/resources.py | 27 +++++++++++++++++++++------ tests/resources/test_issue.py | 12 ++++++++++++ 2 files changed, 33 insertions(+), 6 deletions(-) diff --git a/jira/resources.py b/jira/resources.py index b00d40909..405706715 100644 --- a/jira/resources.py +++ b/jira/resources.py @@ -692,6 +692,27 @@ def update( # type: ignore[override] # incompatible supertype ignored super(Issue, self).update(async_=async_, jira=jira, notify=notify, fields=data) + def get_field(self, fieldName: str) -> Any: + """Obtain the (parsed) value from the Issue's field. + + Args: + fieldName (str): The name of the field to get + + Raises: + AttributeError: If the field does not exist or if the field starts with an ``_`` + + Returns: + Any: Returns the parsed data stored in the field. For example, "project" would be of class :py:class:`Project` + """ + + if fieldName.startswith("_"): + raise AttributeError( + f"A issue field_name cannot start with underscore (_): {fieldName}", + fieldName, + ) + else: + return getattr(self.fields, fieldName) + def add_field_value(self, field: str, value: str): """Add a value to a field that supports multiple values, without resetting the existing values. @@ -1431,9 +1452,3 @@ def cls_for_resource(resource_literal: str) -> Type[Resource]: class PropertyHolder(object): def __init__(self, raw): __bases__ = raw # noqa - - def __getitem__(self, item) - if item.startswith("_"): - raise KeyError(item) - else: - return getattr(self, item) diff --git a/tests/resources/test_issue.py b/tests/resources/test_issue.py index ca7bcbcd0..8370b4d4c 100644 --- a/tests/resources/test_issue.py +++ b/tests/resources/test_issue.py @@ -26,6 +26,18 @@ def test_issue(self): self.assertEqual(issue.key, self.issue_1) self.assertEqual(issue.fields.summary, "issue 1 from %s" % self.project_b) + def test_issue_get_field(self): + issue = self.jira.issue(self.issue_1) + self.assertEqual(issue.fields.description, issue.get_field(fieldName="description")) + + self.assertRaisesRegex( + AttributeError, ": _something", issue.get_field, "_something" + ) + + self.assertRaisesRegex( + AttributeError, "customfield_1234", issue.get_field, "customfield_1234" + ) + def test_issue_field_limiting(self): issue = self.jira.issue(self.issue_2, fields="summary,comment") self.assertEqual(issue.fields.summary, "issue 2 from %s" % self.project_b) From 4ae1c4c71e13bda551391fe1f154094cbdf5b703 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 3 Mar 2022 16:57:23 +0000 Subject: [PATCH 3/8] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- tests/resources/test_issue.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/resources/test_issue.py b/tests/resources/test_issue.py index 8370b4d4c..ce40eb15b 100644 --- a/tests/resources/test_issue.py +++ b/tests/resources/test_issue.py @@ -28,7 +28,9 @@ def test_issue(self): def test_issue_get_field(self): issue = self.jira.issue(self.issue_1) - self.assertEqual(issue.fields.description, issue.get_field(fieldName="description")) + self.assertEqual( + issue.fields.description, issue.get_field(fieldName="description") + ) self.assertRaisesRegex( AttributeError, ": _something", issue.get_field, "_something" From 0aabc13b3c883c9f105480b541b506e420400a4e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dennis=20V=C3=A6rum?= <6872940+dvaerum@users.noreply.github.com> Date: Mon, 7 Mar 2022 14:28:54 +0100 Subject: [PATCH 4/8] Update jira/resources.py Added fix for name convention Co-authored-by: Adel Haddad <26027314+adehad@users.noreply.github.com> --- jira/resources.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/jira/resources.py b/jira/resources.py index 405706715..402a5f867 100644 --- a/jira/resources.py +++ b/jira/resources.py @@ -692,7 +692,7 @@ def update( # type: ignore[override] # incompatible supertype ignored super(Issue, self).update(async_=async_, jira=jira, notify=notify, fields=data) - def get_field(self, fieldName: str) -> Any: + def get_field(self, field_name: str) -> Any: """Obtain the (parsed) value from the Issue's field. Args: From aafd239bd09f07e95d418470d09852981d04c322 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dennis=20V=C3=A6rum?= <6872940+dvaerum@users.noreply.github.com> Date: Mon, 7 Mar 2022 14:29:49 +0100 Subject: [PATCH 5/8] Update tests/resources/test_issue.py Change unittest to "context manager style" Co-authored-by: Adel Haddad <26027314+adehad@users.noreply.github.com> --- tests/resources/test_issue.py | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/tests/resources/test_issue.py b/tests/resources/test_issue.py index ce40eb15b..f373dc253 100644 --- a/tests/resources/test_issue.py +++ b/tests/resources/test_issue.py @@ -32,13 +32,11 @@ def test_issue_get_field(self): issue.fields.description, issue.get_field(fieldName="description") ) - self.assertRaisesRegex( - AttributeError, ": _something", issue.get_field, "_something" - ) + with self.assertRaisesRegex(AttributeError, ": _something"): + issue.get_field("_something") - self.assertRaisesRegex( - AttributeError, "customfield_1234", issue.get_field, "customfield_1234" - ) + with self.assertRaisesRegex(AttributeError, "customfield_1234"): + issue.get_field("customfield_1234") def test_issue_field_limiting(self): issue = self.jira.issue(self.issue_2, fields="summary,comment") From 5a41be8c3cfb2ed6f090852d28873939641c834f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dennis=20V=C3=A6rum?= <6872940+dvaerum@users.noreply.github.com> Date: Mon, 7 Mar 2022 14:30:05 +0100 Subject: [PATCH 6/8] Update jira/resources.py Fixed spelling mistage Co-authored-by: Adel Haddad <26027314+adehad@users.noreply.github.com> --- jira/resources.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/jira/resources.py b/jira/resources.py index 402a5f867..30270f3ce 100644 --- a/jira/resources.py +++ b/jira/resources.py @@ -707,7 +707,7 @@ def get_field(self, field_name: str) -> Any: if fieldName.startswith("_"): raise AttributeError( - f"A issue field_name cannot start with underscore (_): {fieldName}", + f"An issue field_name cannot start with underscore (_): {fieldName}", fieldName, ) else: From 1c0d53591fd5930cccb8cd9713056e8d30e6a6e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dennis=20V=C3=A6rum?= <6872940+dvaerum@users.noreply.github.com> Date: Mon, 7 Mar 2022 23:46:51 +0100 Subject: [PATCH 7/8] Update jira/resources.py Changed the rest of the variable names from `fieldName` to `field_name` Co-authored-by: Adel Haddad <26027314+adehad@users.noreply.github.com> --- jira/resources.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/jira/resources.py b/jira/resources.py index 30270f3ce..25778ad6e 100644 --- a/jira/resources.py +++ b/jira/resources.py @@ -696,7 +696,7 @@ def get_field(self, field_name: str) -> Any: """Obtain the (parsed) value from the Issue's field. Args: - fieldName (str): The name of the field to get + field_name (str): The name of the field to get Raises: AttributeError: If the field does not exist or if the field starts with an ``_`` @@ -705,13 +705,13 @@ def get_field(self, field_name: str) -> Any: Any: Returns the parsed data stored in the field. For example, "project" would be of class :py:class:`Project` """ - if fieldName.startswith("_"): + if field_name.startswith("_"): raise AttributeError( - f"An issue field_name cannot start with underscore (_): {fieldName}", - fieldName, + f"An issue field_name cannot start with underscore (_): {field_name}", + field_name, ) else: - return getattr(self.fields, fieldName) + return getattr(self.fields, field_name) def add_field_value(self, field: str, value: str): """Add a value to a field that supports multiple values, without resetting the existing values. From 059fcfedf44a707ccd85c7ec0885e5a4e5540546 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dennis=20V=C3=A6rum?= <6872940+dvaerum@users.noreply.github.com> Date: Mon, 7 Mar 2022 23:51:06 +0100 Subject: [PATCH 8/8] Found one more `fieldName` I forgot to change to `field_name` --- tests/resources/test_issue.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/resources/test_issue.py b/tests/resources/test_issue.py index f373dc253..4b744102f 100644 --- a/tests/resources/test_issue.py +++ b/tests/resources/test_issue.py @@ -29,7 +29,7 @@ def test_issue(self): def test_issue_get_field(self): issue = self.jira.issue(self.issue_1) self.assertEqual( - issue.fields.description, issue.get_field(fieldName="description") + issue.fields.description, issue.get_field(field_name="description") ) with self.assertRaisesRegex(AttributeError, ": _something"):