Skip to content

Commit

Permalink
Merge pull request #63 from jesper-raemaekers/workitem-required-fields
Browse files Browse the repository at this point in the history
Workitem required fields
  • Loading branch information
jesper-raemaekers authored Apr 11, 2022
2 parents 0e6dc78 + 8f31f69 commit 3ddb051
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 3 deletions.
3 changes: 3 additions & 0 deletions docs/workitem.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ A new workitem can be created using :func:`~Project.createWorkitem`.
new_task = project.createWorkitem('changerequest')
# Add fields to the creation. Needed if there are required fields upon creation.
new_task = project..createWorkitem('task', new_workitem_fields={'title': 'New title'})
Updating a field
^^^^^^^^^^^^^^^^

Expand Down
4 changes: 2 additions & 2 deletions polarion/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,14 +115,14 @@ def searchPlanFullItem(self, query='', order='Created', limit=-1):
return return_list


def createWorkitem(self, workitem_type: str):
def createWorkitem(self, workitem_type: str, new_workitem_fields=None):
"""
Create a workitem based on the workitem type.
:param workitem_type: The new workitem type
:return: A new workitem
:rtype: Workitem
"""
return Workitem(self.polarion, self, new_workitem_type=workitem_type)
return Workitem(self.polarion, self, new_workitem_type=workitem_type, new_workitem_fields=new_workitem_fields)

def searchWorkitem(self, query='', order='Created', field_list=None, limit=-1):
"""Query for available workitems. This will only query for the items.
Expand Down
20 changes: 19 additions & 1 deletion polarion/workitem.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class HyperlinkRoles(Enum):
INTERNAL_REF = 'internal reference'
EXTERNAL_REF = 'external reference'

def __init__(self, polarion, project, id=None, uri=None, new_workitem_type=None, polarion_workitem=None):
def __init__(self, polarion, project, id=None, uri=None, new_workitem_type=None, new_workitem_fields=None, polarion_workitem=None):
super().__init__(polarion, project, id, uri)
self._polarion = polarion
self._project = project
Expand All @@ -54,9 +54,27 @@ def __init__(self, polarion, project, id=None, uri=None, new_workitem_type=None,
raise Exception(
f'Cannot find workitem {self._id} in project {self._project.id}')
elif new_workitem_type is not None:
# construct empty workitem
self._polarion_item = self._polarion.WorkItemType(
type=self._polarion.EnumOptionIdType(id=new_workitem_type))
self._polarion_item.project = self._project.polarion_data

# get the required field for a new item
required_features = service.getInitialWorkflowActionForProjectAndType(self._project.id, self._polarion.EnumOptionIdType(id=new_workitem_type))
if required_features.requiredFeatures is not None:
# if there are any, go and check if they are all supplied
if new_workitem_fields is None or not set(required_features.requiredFeatures.item) <= new_workitem_fields.keys():
# let the user know with a better error
raise Exception(f'New workitem required field: {required_features.requiredFeatures.item} to be filled in using new_workitem_fields')

if new_workitem_fields is not None:
for new_field in new_workitem_fields:
if new_field in self._polarion_item:
self._polarion_item[new_field] = new_workitem_fields[new_field]
else:
raise Exception(f'{new_field} in new_workitem_fields is not recognised as a workitem field')

# and create it
new_uri = service.createWorkItem(self._polarion_item)
# reload from polarion
self._polarion_item = service.getWorkItemByUri(new_uri)
Expand Down
12 changes: 12 additions & 0 deletions tests/test_polarion_workitem.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,18 @@ def test_create_workitem(self):
self.assertEqual(executed_workitem, checking_workitem,
msg='Workitems not identical')

# Check that fields can be supplied with the call
executed_workitem = self.executing_project.createWorkitem('task', new_workitem_fields={'title': 'Test title!'})
checking_workitem = self.checking_project.getWorkitem(
executed_workitem.id)

self.assertEqual(executed_workitem, checking_workitem,
msg='Workitems not identical when created with supplied fields')

# Check that fields are checks for validity
with self.assertRaises(Exception):
executed_workitem = self.executing_project.createWorkitem('task', new_workitem_fields={'wrong-field': 'what happens?'})

def test_title_change(self):
executed_workitem = self.global_workitem
checking_workitem = self.checking_project.getWorkitem(
Expand Down

0 comments on commit 3ddb051

Please sign in to comment.