Skip to content

Commit c141213

Browse files
authored
Add support for IssueProperty resource (#1439)
1 parent 89bbd90 commit c141213

File tree

3 files changed

+106
-0
lines changed

3 files changed

+106
-0
lines changed

jira/client.py

+44
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
Issue,
6868
IssueLink,
6969
IssueLinkType,
70+
IssueProperty,
7071
IssueSecurityLevelScheme,
7172
IssueType,
7273
IssueTypeScheme,
@@ -2401,6 +2402,49 @@ def add_worklog(
24012402

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

2405+
# Issue properties
2406+
2407+
@translate_resource_args
2408+
def issue_properties(self, issue: str) -> List[IssueProperty]:
2409+
"""Get a list of issue property Resource from the server for an issue.
2410+
2411+
Args:
2412+
issue (str): ID or key of the issue to get properties from
2413+
2414+
Returns:
2415+
List[IssueProperty]
2416+
"""
2417+
r_json = self._get_json(f"issue/{issue}/properties")
2418+
properties = [self.issue_property(issue, key["key"]) for key in r_json["keys"]]
2419+
return properties
2420+
2421+
@translate_resource_args
2422+
def issue_property(self, issue: str, key: str) -> IssueProperty:
2423+
"""Get a specific issue property Resource from the server.
2424+
2425+
Args:
2426+
issue (str): ID or key of the issue to get the property from
2427+
key (str): Key of the property to get
2428+
Returns:
2429+
IssueProperty
2430+
"""
2431+
return self._find_for_resource(IssueProperty, (issue, key))
2432+
2433+
@translate_resource_args
2434+
def add_issue_property(self, issue: str, key: str, data) -> Response:
2435+
"""Add or update a specific issue property Resource.
2436+
2437+
Args:
2438+
issue (str): ID or key of the issue to set the property to
2439+
key (str): Key of the property to set
2440+
data: The data to set for the property
2441+
Returns:
2442+
Response
2443+
"""
2444+
2445+
url = self._get_url(f"issue/{issue}/properties/{key}")
2446+
return self._session.put(url, data=json.dumps(data))
2447+
24042448
# Issue links
24052449

24062450
@translate_resource_args

jira/resources.py

+41
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ class AnyLike:
4242
"Worklog",
4343
"IssueLink",
4444
"IssueLinkType",
45+
"IssueProperty",
4546
"IssueSecurityLevelScheme",
4647
"IssueType",
4748
"IssueTypeScheme",
@@ -290,6 +291,21 @@ def find(
290291
else:
291292
path = self._resource.format(id)
292293
url = self._get_url(path)
294+
self._find_by_url(url, params)
295+
296+
def _find_by_url(
297+
self,
298+
url: str,
299+
params: Optional[Dict[str, str]] = None,
300+
):
301+
"""Finds a resource on the specified url. The resource is loaded
302+
with the JSON data returned by doing a request on the specified
303+
url.
304+
305+
Args:
306+
url (str): url
307+
params (Optional[Dict[str, str]]): params
308+
"""
293309
self._load(url, params=params)
294310

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

9981014

1015+
class IssueProperty(Resource):
1016+
"""Custom data against an issue."""
1017+
1018+
def __init__(
1019+
self,
1020+
options: Dict[str, str],
1021+
session: ResilientSession,
1022+
raw: Dict[str, Any] = None,
1023+
):
1024+
Resource.__init__(self, "issue/{0}/properties/{1}", options, session)
1025+
if raw:
1026+
self._parse_raw(raw)
1027+
self.raw: Dict[str, Any] = cast(Dict[str, Any], self.raw)
1028+
1029+
def _find_by_url(
1030+
self,
1031+
url: str,
1032+
params: Optional[Dict[str, str]] = None,
1033+
):
1034+
super()._find_by_url(url, params)
1035+
# An IssueProperty never returns "self" identifier, set it
1036+
self.self = url
1037+
1038+
9991039
class IssueLink(Resource):
10001040
"""Link between two issues."""
10011041

@@ -1485,6 +1525,7 @@ def dict2resource(
14851525
r"issue/[^/]+/votes$": Votes,
14861526
r"issue/[^/]+/watchers$": Watchers,
14871527
r"issue/[^/]+/worklog/[^/]+$": Worklog,
1528+
r"issue/[^/]+/properties/[^/]+$": IssueProperty,
14881529
r"issueLink/[^/]+$": IssueLink,
14891530
r"issueLinkType/[^/]+$": IssueLinkType,
14901531
r"issuetype/[^/]+$": IssueType,
+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
from tests.conftest import JiraTestCase
2+
3+
4+
class IssuePropertyTests(JiraTestCase):
5+
def setUp(self):
6+
JiraTestCase.setUp(self)
7+
self.issue_1 = self.test_manager.project_b_issue1
8+
9+
def test_issue_property(self):
10+
self.jira.add_issue_property(
11+
self.issue_1, "custom-property", "Testing a property value"
12+
)
13+
properties = self.jira.issue_properties(self.issue_1)
14+
self.assertEqual(len(properties), 1)
15+
16+
prop = self.jira.issue_property(self.issue_1, "custom-property")
17+
self.assertEqual(prop.key, "custom-property")
18+
self.assertEqual(prop.value, "Testing a property value")
19+
prop.delete()
20+
properties = self.jira.issue_properties(self.issue_1)
21+
self.assertEqual(len(properties), 0)

0 commit comments

Comments
 (0)