pythonpython-3.xazureazure-devopsazure-devops-rest-api

Azure Dev/Ops - Ingestion of Analytics View data using Python


I would like to access Analytics View data from Azure DevOps to have access to registered project activities.

Would anyone have any examples of how they would do it using the azure-devops python library?

I found no example involving extracting data from Analytics View. Basically, I need a Python script that shows all the Analytics View fields of my projects.


Solution

  • After some research, I managed to partially solve my problem because this solution does not bring everything from Analytics View, in addition there is a limitation of 20k records in the query result:

    from azure.devops.connection import Connection
    from msrest.authentication import BasicAuthentication
    from azure.devops.v5_1.work_item_tracking.models import Wiql
    
    
    token = 'xxx'
    team_instance = 'https://dev.azure.com/xxx'
    
    
    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)
    
    get_TC_from_query(
        """\
    SELECT
            [System.Id],
            [System.WorkItemType],
            [System.Title],
            [System.State],
            [System.AreaPath],
            [System.IterationPath]
    FROM workitems
    WHERE
            [System.TeamProject] = 'Project'
            and [System.WorkItemType] = 'Product Backlog Item'
            and [System.State] = 'Done'
    ORDER BY [System.ChangedDate] DESC
    """
    )