Search code examples
azure-devopswiql

How can we run WIQL query in Azure devops pipeline task? Any examples


I have a pipeline in which I need to query work items for which I am using rest api but I am not sure how to use this with Azure pipeline task. Mine is a linux agent hence, is it possible to run it in shell instead of powershell?


Solution

  • You can run WIQL query in shell script or something like that in pipeline task.

    Create a Json without newlines like:

    {
      "query": "Select [System.Id], [System.Title], [System.State] From WorkItems Where [System.WorkItemType] = 'Task' AND [State] <> 'Closed' AND [State] <> 'Removed' order by [Microsoft.VSTS.Common.Priority] asc, [System.CreatedDate] desc"
    }
    

    And send it with the following:

    #!/bin/bash
    source ~/bin/variables.sh
    url="https://dev.azure.com/$(organizationName)/$(project)/_apis/wit/queries/{query}?api-version=7.0"    
    response=$(curl -s -u $(presenter):$(PAT) -X POST -H "Content-Type: application/json" -d "${json}" "${url}")
    

    organizationName is the organization name.

    project is the project name.

    presenter is the user.name (from az account show)

    PAT is the personal access token.

    For running query in PowerShell script with Azure pipeline, you can refer to this ticket Running query in Pipeline in Azure-DevOps.

    For running Azure DevOps Service REST API with Linux Shell script, you can refer to this ticket Create Pull Request by Azure DevOps Service REST API with Linux Shell script.

    Hope that would help you.