diff --git a/jira/resources.py b/jira/resources.py index faf43b3f0..25778ad6e 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, field_name: str) -> Any: + """Obtain the (parsed) value from the Issue's field. + + Args: + 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 ``_`` + + Returns: + Any: Returns the parsed data stored in the field. For example, "project" would be of class :py:class:`Project` + """ + + if field_name.startswith("_"): + raise AttributeError( + f"An issue field_name cannot start with underscore (_): {field_name}", + field_name, + ) + else: + 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. diff --git a/tests/resources/test_issue.py b/tests/resources/test_issue.py index ca7bcbcd0..4b744102f 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(field_name="description") + ) + + with self.assertRaisesRegex(AttributeError, ": _something"): + issue.get_field("_something") + + 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") self.assertEqual(issue.fields.summary, "issue 2 from %s" % self.project_b)