Search code examples
pythonselenium-webdriverazure-devopsazure-pipelinestfs-workitem

Automating export Azure Devops pipeline Work Items Revisions using Python Selenium Webdriver


Currently I am using Azure Devops Pipeline OData Service to export the data using the following link:

 https://<CompanyName>.analytics.visualstudio.com/<ProjectName>/_odata/v2.0/WorkItemRevisions?$apply=filter(CreatedDateSK%20eq%20<Date>%20and%20WorkItemType%20eq%20%27<WorkItemType%27)

and I am using extraction tool (Talend DI) to automatically change in these parameters, and I am running python code (Selenium Library) to webscrape the above mentioned link using basic authentication.

Is there any other alternative solution other than selenium to do this task. As selenium web driver gives time out error when waiting for large size oage to be loaded.

try:
        content_element=expected_conditions.visibility_of_element_located((By.XPATH,contentElementID))
        WebDriverWait(driver,15).until(content_element)
        break
    except TimeoutException:
        if(i==14):
                driver.quit()
                raise TimeoutException

Solution

  • You can use Requests HTTP library to call the OData Service. Please check below codes example:

    import requests
    import json
    import base64
    
    if __name__ == "__main__":
    
        pat = 'Personal access token'
        authorization = str(base64.b64encode(bytes(':'+pat, 'ascii')), 'ascii')
    
        headers = {
        'Content-Type': 'application/json',
        'Authorization': 'Basic '+authorization
        }       
    
        url = "https://<CompanyName>.analytics.visualstudio.com/<ProjectName>/_odata/v2.0/WorkItemRevisions?$apply=filter(CreatedDateSK%20eq%20<Date>%20and%20WorkItemType%20eq%20%27<WorkItemType%27)"
    
        #you can also use below domain dev.azure.com, which is the new domain of azure devops service
        #url = "https://analytics.dev.azure.com/{org}/{proj}/_odata/v2.0//WorkItems?`$select=WorkItemId,Title,State&`$expand=Parent(`$select=WorkItemId,Title,State)&`$filter=WorkItemId eq 12"
    
        response = requests.get(url, headers=headers)
    
        print(response.content)
    

    Please check here to get a Person access token with the right permission scope.