Skip to content

Commit

Permalink
Add support for IssueProperty resource (#1439)
Browse files Browse the repository at this point in the history
  • Loading branch information
turran authored Aug 14, 2022
1 parent 89bbd90 commit c141213
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 0 deletions.
44 changes: 44 additions & 0 deletions jira/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
Issue,
IssueLink,
IssueLinkType,
IssueProperty,
IssueSecurityLevelScheme,
IssueType,
IssueTypeScheme,
Expand Down Expand Up @@ -2401,6 +2402,49 @@ def add_worklog(

return Worklog(self._options, self._session, json_loads(r))

# Issue properties

@translate_resource_args
def issue_properties(self, issue: str) -> List[IssueProperty]:
"""Get a list of issue property Resource from the server for an issue.
Args:
issue (str): ID or key of the issue to get properties from
Returns:
List[IssueProperty]
"""
r_json = self._get_json(f"issue/{issue}/properties")
properties = [self.issue_property(issue, key["key"]) for key in r_json["keys"]]
return properties

@translate_resource_args
def issue_property(self, issue: str, key: str) -> IssueProperty:
"""Get a specific issue property Resource from the server.
Args:
issue (str): ID or key of the issue to get the property from
key (str): Key of the property to get
Returns:
IssueProperty
"""
return self._find_for_resource(IssueProperty, (issue, key))

@translate_resource_args
def add_issue_property(self, issue: str, key: str, data) -> Response:
"""Add or update a specific issue property Resource.
Args:
issue (str): ID or key of the issue to set the property to
key (str): Key of the property to set
data: The data to set for the property
Returns:
Response
"""

url = self._get_url(f"issue/{issue}/properties/{key}")
return self._session.put(url, data=json.dumps(data))

# Issue links

@translate_resource_args
Expand Down
41 changes: 41 additions & 0 deletions jira/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class AnyLike:
"Worklog",
"IssueLink",
"IssueLinkType",
"IssueProperty",
"IssueSecurityLevelScheme",
"IssueType",
"IssueTypeScheme",
Expand Down Expand Up @@ -290,6 +291,21 @@ def find(
else:
path = self._resource.format(id)
url = self._get_url(path)
self._find_by_url(url, params)

def _find_by_url(
self,
url: str,
params: Optional[Dict[str, str]] = None,
):
"""Finds a resource on the specified url. The resource is loaded
with the JSON data returned by doing a request on the specified
url.
Args:
url (str): url
params (Optional[Dict[str, str]]): params
"""
self._load(url, params=params)

def _get_url(self, path: str) -> str:
Expand Down Expand Up @@ -996,6 +1012,30 @@ def delete( # type: ignore[override]
super().delete(params)


class IssueProperty(Resource):
"""Custom data against an issue."""

def __init__(
self,
options: Dict[str, str],
session: ResilientSession,
raw: Dict[str, Any] = None,
):
Resource.__init__(self, "issue/{0}/properties/{1}", options, session)
if raw:
self._parse_raw(raw)
self.raw: Dict[str, Any] = cast(Dict[str, Any], self.raw)

def _find_by_url(
self,
url: str,
params: Optional[Dict[str, str]] = None,
):
super()._find_by_url(url, params)
# An IssueProperty never returns "self" identifier, set it
self.self = url


class IssueLink(Resource):
"""Link between two issues."""

Expand Down Expand Up @@ -1485,6 +1525,7 @@ def dict2resource(
r"issue/[^/]+/votes$": Votes,
r"issue/[^/]+/watchers$": Watchers,
r"issue/[^/]+/worklog/[^/]+$": Worklog,
r"issue/[^/]+/properties/[^/]+$": IssueProperty,
r"issueLink/[^/]+$": IssueLink,
r"issueLinkType/[^/]+$": IssueLinkType,
r"issuetype/[^/]+$": IssueType,
Expand Down
21 changes: 21 additions & 0 deletions tests/resources/test_issue_property.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from tests.conftest import JiraTestCase


class IssuePropertyTests(JiraTestCase):
def setUp(self):
JiraTestCase.setUp(self)
self.issue_1 = self.test_manager.project_b_issue1

def test_issue_property(self):
self.jira.add_issue_property(
self.issue_1, "custom-property", "Testing a property value"
)
properties = self.jira.issue_properties(self.issue_1)
self.assertEqual(len(properties), 1)

prop = self.jira.issue_property(self.issue_1, "custom-property")
self.assertEqual(prop.key, "custom-property")
self.assertEqual(prop.value, "Testing a property value")
prop.delete()
properties = self.jira.issue_properties(self.issue_1)
self.assertEqual(len(properties), 0)

0 comments on commit c141213

Please sign in to comment.