Skip to content

Commit 11334cd

Browse files
authored
Allow setting remote related WI relations (#1239)
* Initial Commit * run style fix * Error message changed
1 parent 568f568 commit 11334cd

File tree

2 files changed

+29
-16
lines changed

2 files changed

+29
-16
lines changed

azure-devops/azext_devops/dev/boards/arguments.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ def load_work_arguments(self, _):
3535
context.argument('relation_type', help='Relation type to create. Example: parent, child ')
3636
context.argument('target_id', help='ID(s) of work-items to create relation with. \
3737
Multiple values can be passed comma separated. Example: 1,2 ')
38+
context.argument('target_url', help='URL(s) of work-items to create relation with. \
39+
Multiple values can be passed comma separated.')
3840

3941
with self.argument_context('boards work-item relation remove') as context:
4042
context.argument('relation_type', help='Relation type to remove. Example: parent, child ')

azure-devops/azext_devops/dev/boards/relations.py

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,36 +21,47 @@ def get_relation_types_show(organization=None, detect=None):
2121
return client.get_relation_types()
2222

2323

24-
def add_relation(id, relation_type, target_id, organization=None, detect=None): # pylint: disable=redefined-builtin
24+
def add_relation(id, relation_type, target_id=None, target_url=None, organization=None, detect=None): # pylint: disable=redefined-builtin
2525
""" Add relation(s) to work item.
2626
"""
27+
28+
if target_id is None and target_url is None:
29+
raise CLIError('--target-id or --target-url must be provided')
30+
2731
organization = resolve_instance(detect=detect, organization=organization)
2832
patch_document = []
2933
client = get_work_item_tracking_client(organization)
3034

3135
relation_types_from_service = client.get_relation_types()
3236
relation_type_system_name = get_system_relation_name(relation_types_from_service, relation_type)
3337

34-
target_work_item_ids = target_id.split(',')
35-
work_item_query_clause = []
36-
for target_work_item_id in target_work_item_ids:
37-
work_item_query_clause.append('[System.Id] = {}'.format(target_work_item_id))
38+
patch_document = []
39+
if target_id is not None:
40+
target_work_item_ids = target_id.split(',')
41+
work_item_query_clause = []
42+
for target_work_item_id in target_work_item_ids:
43+
work_item_query_clause.append('[System.Id] = {}'.format(target_work_item_id))
3844

39-
wiql_query_format = 'SELECT [System.Id] FROM WorkItems WHERE ({})'
40-
wiql_query_to_get_target_work_items = wiql_query_format.format(' OR '.join(work_item_query_clause))
45+
wiql_query_format = 'SELECT [System.Id] FROM WorkItems WHERE ({})'
46+
wiql_query_to_get_target_work_items = wiql_query_format.format(' OR '.join(work_item_query_clause))
4147

42-
wiql_object = Wiql()
43-
wiql_object.query = wiql_query_to_get_target_work_items
44-
target_work_items = client.query_by_wiql(wiql=wiql_object).work_items
48+
wiql_object = Wiql()
49+
wiql_object.query = wiql_query_to_get_target_work_items
50+
target_work_items = client.query_by_wiql(wiql=wiql_object).work_items
4551

46-
if len(target_work_items) != len(target_work_item_ids):
47-
raise CLIError('Id(s) supplied in --target-id is not valid')
52+
if len(target_work_items) != len(target_work_item_ids):
53+
raise CLIError('Id(s) supplied in --target-id is not valid')
4854

49-
patch_document = []
55+
for target_work_item in target_work_items:
56+
op = _create_patch_operation('add', '/relations/-', relation_type_system_name, target_work_item.url)
57+
patch_document.append(op)
58+
59+
if target_url is not None:
60+
target_urls = target_url.split(',')
5061

51-
for target_work_item in target_work_items:
52-
op = _create_patch_operation('add', '/relations/-', relation_type_system_name, target_work_item.url)
53-
patch_document.append(op)
62+
for url in target_urls:
63+
op = _create_patch_operation('add', '/relations/-', relation_type_system_name, url)
64+
patch_document.append(op)
5465

5566
client.update_work_item(document=patch_document, id=id)
5667
work_item = client.get_work_item(id, expand='All')

0 commit comments

Comments
 (0)