Skip to content

Commit

Permalink
Allow setting remote related WI relations (#1239)
Browse files Browse the repository at this point in the history
* Initial Commit

* run style fix

* Error message changed
  • Loading branch information
roshan-sy committed Dec 28, 2021
1 parent 568f568 commit 11334cd
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 16 deletions.
2 changes: 2 additions & 0 deletions azure-devops/azext_devops/dev/boards/arguments.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ def load_work_arguments(self, _):
context.argument('relation_type', help='Relation type to create. Example: parent, child ')
context.argument('target_id', help='ID(s) of work-items to create relation with. \
Multiple values can be passed comma separated. Example: 1,2 ')
context.argument('target_url', help='URL(s) of work-items to create relation with. \
Multiple values can be passed comma separated.')

with self.argument_context('boards work-item relation remove') as context:
context.argument('relation_type', help='Relation type to remove. Example: parent, child ')
Expand Down
43 changes: 27 additions & 16 deletions azure-devops/azext_devops/dev/boards/relations.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,36 +21,47 @@ def get_relation_types_show(organization=None, detect=None):
return client.get_relation_types()


def add_relation(id, relation_type, target_id, organization=None, detect=None): # pylint: disable=redefined-builtin
def add_relation(id, relation_type, target_id=None, target_url=None, organization=None, detect=None): # pylint: disable=redefined-builtin
""" Add relation(s) to work item.
"""

if target_id is None and target_url is None:
raise CLIError('--target-id or --target-url must be provided')

organization = resolve_instance(detect=detect, organization=organization)
patch_document = []
client = get_work_item_tracking_client(organization)

relation_types_from_service = client.get_relation_types()
relation_type_system_name = get_system_relation_name(relation_types_from_service, relation_type)

target_work_item_ids = target_id.split(',')
work_item_query_clause = []
for target_work_item_id in target_work_item_ids:
work_item_query_clause.append('[System.Id] = {}'.format(target_work_item_id))
patch_document = []
if target_id is not None:
target_work_item_ids = target_id.split(',')
work_item_query_clause = []
for target_work_item_id in target_work_item_ids:
work_item_query_clause.append('[System.Id] = {}'.format(target_work_item_id))

wiql_query_format = 'SELECT [System.Id] FROM WorkItems WHERE ({})'
wiql_query_to_get_target_work_items = wiql_query_format.format(' OR '.join(work_item_query_clause))
wiql_query_format = 'SELECT [System.Id] FROM WorkItems WHERE ({})'
wiql_query_to_get_target_work_items = wiql_query_format.format(' OR '.join(work_item_query_clause))

wiql_object = Wiql()
wiql_object.query = wiql_query_to_get_target_work_items
target_work_items = client.query_by_wiql(wiql=wiql_object).work_items
wiql_object = Wiql()
wiql_object.query = wiql_query_to_get_target_work_items
target_work_items = client.query_by_wiql(wiql=wiql_object).work_items

if len(target_work_items) != len(target_work_item_ids):
raise CLIError('Id(s) supplied in --target-id is not valid')
if len(target_work_items) != len(target_work_item_ids):
raise CLIError('Id(s) supplied in --target-id is not valid')

patch_document = []
for target_work_item in target_work_items:
op = _create_patch_operation('add', '/relations/-', relation_type_system_name, target_work_item.url)
patch_document.append(op)

if target_url is not None:
target_urls = target_url.split(',')

for target_work_item in target_work_items:
op = _create_patch_operation('add', '/relations/-', relation_type_system_name, target_work_item.url)
patch_document.append(op)
for url in target_urls:
op = _create_patch_operation('add', '/relations/-', relation_type_system_name, url)
patch_document.append(op)

client.update_work_item(document=patch_document, id=id)
work_item = client.get_work_item(id, expand='All')
Expand Down

0 comments on commit 11334cd

Please sign in to comment.