How to extract WorkItems from query in VSTS (Azure DevOps) with Python REST API?

后端 未结 1 695
走了就别回头了
走了就别回头了 2020-12-20 09:07

I\'m using the official Python REST API of Azure DevOps: https://github.com/Microsoft/azure-devops-python-api

Thanks to the samples I have been able to retrieve inf

1条回答
  •  时光说笑
    2020-12-20 09:25

    After posting a message (i.e. issue leading to a pull request) on the Github repository for VSTS examples, I got a hint to solve my issue.

    The solution is to use:

    • a wiql query object with Wiql class
    • query_by_wiql function
    • transforming the results of the query (references with WorkItem id) to WorkItem with get_work_item function (or get_work_items to process several WorkItems in a single pass)

    Here is my solution to this problem:

    [UPDATED FOR VERSION 5.1 and azure-devops module replacing VSTS]

    from azure.devops.connection import Connection
    from msrest.authentication import BasicAuthentication
    from azure.devops.v5_1.work_item_tracking.models import Wiql
    
    
    token = "hcykwckuhe6vbnigsjs7r3ai2jefsdlkfjslkfj5mxizbtfu6k53j4ia"
    team_instance = "https://tfstest.toto.com:8443/tfs/Development/"
    
    credentials = BasicAuthentication("", token)
    connection = Connection(base_url=team_instance, creds=credentials)
    
    
    def print_work_items(work_items):
        for work_item in work_items:
            print(
                "{0} {1}: {2}".format(
                    work_item.fields["System.WorkItemType"],
                    work_item.id,
                    work_item.fields["System.Title"],
                )
            )
    
    
    wit_client = connection.clients.get_work_item_tracking_client()
    
    
    def get_TC_from_query(query):
        query_wiql = Wiql(query=query)
        results = wit_client.query_by_wiql(query_wiql).work_items
        # WIQL query gives a WorkItemReference => we get the corresponding WorkItem from id
        work_items = (wit_client.get_work_item(int(result.id)) for result in results)
        print_work_items(work_items)
    

    0 讨论(0)
提交回复
热议问题