Skip to content

Commit

Permalink
Add method to get linked items #124
Browse files Browse the repository at this point in the history
  • Loading branch information
jesper-raemaekers committed Jul 29, 2023
1 parent 096a2ed commit 5611b91
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 1 deletion.
7 changes: 7 additions & 0 deletions docs/workitem.rst
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,13 @@ Workitems can be linked together using :func:`~Workitem.addLinkedItem`. In this
new_workitem_2.addLinkedItem(new_workitem_1, 'relates_to')
Links can be retrieved either with or without link roles:
.. code:: python
print(workitem_1.getLinkedItem()) # [PYTH-540: None]
print(workitem_1.getLinkedItemWithRoles()) # [('follow_up', PYTH-540: None)]
Custom fields
^^^^^^^^^^^^^
Expand Down
25 changes: 25 additions & 0 deletions polarion/workitem.py
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,31 @@ def removeLinkedItem(self, workitem, role=None):
self._reloadFromPolarion()
workitem._reloadFromPolarion()

def getLinkedItemWithRoles(self):
"""
Get linked workitems both linked and back linked item will show up. Will include link roles.
@return: Array of tuple ('link type', Workitem)
"""
linked_items = []
service = self._polarion.getService('Tracker')
if self.linkedWorkItems is not None:
for linked_item in self.linkedWorkItems.LinkedWorkItem:
linked_items.append((linked_item.role.id, Workitem(self._polarion, self._project, uri=linked_item.workItemURI)))
if self.linkedWorkItemsDerived is not None:
for linked_item in self.linkedWorkItemsDerived.LinkedWorkItem:
linked_items.append((linked_item.role.id, Workitem(self._polarion, self._project, uri=linked_item.workItemURI)))
return linked_items

def getLinkedItem(self):
"""
Get linked workitems both linked and back linked item will show up.
@return: Array of Workitem
@return:
"""
return [item[1] for item in self.getLinkedItemWithRoles()]

def hasAttachment(self):
"""
Checks if the workitem has attachments
Expand Down
33 changes: 32 additions & 1 deletion tests/test_polarion_workitem.py
Original file line number Diff line number Diff line change
Expand Up @@ -645,4 +645,35 @@ def test_testcase_update_steps(self):
for i in range(3):
self.assertEqual(f'{10 + i}', executed_workitem_1.getTestSteps()[i]['step'], msg='Value in first column did not change')
self.assertEqual(f'new {i + 20}', executed_workitem_1.getTestSteps()[i]['description'], msg='Value in second column did not change')
self.assertEqual(f'last {i + 20}', executed_workitem_1.getTestSteps()[i]['expectedResult'], msg='Value in third column did not change')
self.assertEqual(f'last {i + 20}', executed_workitem_1.getTestSteps()[i]['expectedResult'], msg='Value in third column did not change')

def test_get_linked_items(self):
executed_workitem_1 = self.executing_project.createWorkitem('task')
executed_workitem_2 = self.executing_project.createWorkitem('task')

# check empty linked items
self.assertEqual(0, len(executed_workitem_1.getLinkedItemWithRoles()), msg='Linked workitem not 0 in length')
self.assertEqual(0, len(executed_workitem_1.getLinkedItem()), msg='Linked workitem not 0 in length')

# add link
executed_workitem_1.addLinkedItem(executed_workitem_2, 'follow_up')

# check non empty linked items
self.assertEqual(1, len(executed_workitem_1.getLinkedItemWithRoles()), msg='Linked workitem not 1 in length')
self.assertEqual(1, len(executed_workitem_1.getLinkedItem()), msg='Linked workitem not 1 in length')
self.assertEqual(1, len(executed_workitem_2.getLinkedItemWithRoles()), msg='Linked workitem not 1 in length')
self.assertEqual(1, len(executed_workitem_2.getLinkedItem()), msg='Linked workitem not 1 in length')

self.assertEqual(executed_workitem_2, executed_workitem_1.getLinkedItemWithRoles()[0][1],
msg='Check workitem')
self.assertEqual('follow_up', executed_workitem_1.getLinkedItemWithRoles()[0][0],
msg='Check link type')
self.assertEqual(executed_workitem_2, executed_workitem_1.getLinkedItem()[0],
msg='Check workitem')

self.assertEqual(executed_workitem_1, executed_workitem_2.getLinkedItemWithRoles()[0][1],
msg='Check workitem')
self.assertEqual('follow_up', executed_workitem_2.getLinkedItemWithRoles()[0][0],
msg='Check link type')
self.assertEqual(executed_workitem_1, executed_workitem_2.getLinkedItem()[0],
msg='Check workitem')

0 comments on commit 5611b91

Please sign in to comment.