简体   繁体   中英

azure devops python get all links (child / parent) for work item

i have a work item created in azure devops

from azure.devops.connection import Connection
from msrest.authentication import BasicAuthentication
from azure.devops.v6_0.py_pi_api import JsonPatchOperation
import pprint

# Fill in with your personal access token and org URL
personal_access_token = '<pat>'
organization_url = 'https://dev.azure.com/<org>'

# Create a connection to the org
credentials = BasicAuthentication('', personal_access_token)
connection = Connection(base_url=organization_url, creds=credentials)

# Get a client (the "core" client provides access to projects, teams, etc)
wi_client = connection.clients.get_work_item_tracking_client()

ads_id= 2129

parent_work_item = wi_client.get_work_item(ads_id);

# how to get child/parent work items?

if i run the get_work_item function, it returns data, but has 'relations': None as if there is no parent/child links, even though i can see those links online. is there someway I can get the parent/child links with a function call?

I know the reason for your issue, your must expand the results . I guess the difficulty of this issue is you can't find a ready-made python example(You may don't know how to expand the results in python). Then I will write you one now:

from azure.devops.connection import Connection
from msrest.authentication import BasicAuthentication
import pprint

#get all the work items linked to a work item
def get_work_items_parents_childs(wi_id):
    #get a connection to Azure DevOps
    organization_url = 'https://dev.azure.com/xxx'
    personal_access_token = 'xxx'
    credentials = BasicAuthentication('', personal_access_token)
    connection = Connection(base_url=organization_url, creds=credentials)
    work_item_tracking_client = connection.clients.get_work_item_tracking_client()
    work_item = work_item_tracking_client.get_work_item(wi_id, expand="relations")
    #create a list
    work_items_parents_childs = []
    #get the work item links
    for item in work_item.relations:
        #get parent and child work items
        if item.attributes['name'] == 'Parent' or item.attributes['name'] == 'Child':
            #get the work item id
            work_item_id = item.url.split('/')[-1]
            #get the work item
            linked_work_item = work_item_tracking_client.get_work_item(work_item_id)
            #add the work item to the list
            work_items_parents_childs.append(linked_work_item)
    return work_items_parents_childs

items = get_work_items_parents_childs(120)

for item in items:
    print(item.fields['System.Title'])

I can successfully get them:

在此处输入图像描述

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM