Search code examples
pythonazure-devopsazure-pipelinesazure-devops-rest-api

Get All Pipeline Parameters Through Azure DevOps REST API


I am creating a desktop application thorough Python to execute pipelines in Azure DevOps. I've managed to execute pipelines with fields such as Org Name, Project Name, Pipeline Id, PAT and with two buttons, Trigger Pipeline and View Past Runs.

Now, I want to add a button with Fetch Parameters. This button will fetch all the available parameters of the pipeline and populate the form with radio buttons.

Till now, I am able to execute pipeline with Azure DevOps REST API. Now, I need to fetch all the parameters of given pipeline id and populate them accordingly. I've tried to look all the REST APIs Azure DevOps offering, none can help.

I know this is kinda reinventing the wheel thing. But, I want to learn about this if anyone can help.


Solution

  • Currently, there isn't a built-in Azure DevOps REST API that directly retrieves the parameters of a YAML pipeline. However, you can work around this by leveraging API requests extracted from the Azure DevOps web UI.

    Here is a sample Python script for your reference.

    import base64
    import json
    import requests
    
    # Define your Azure DevOps organization, project, repo, pull request id and personal access token
    organization = "MyADOOrgName"
    project = "TheProjectName"
    pipeline_definition_id = "123"
    personal_access_token = 'xxxxxx'
    
    # Encode the personal access token in base64
    base64_pat = base64.b64encode(f':{personal_access_token}'.encode('utf-8')).decode('utf-8')
    headers = {
        "Authorization": f"Basic {base64_pat}",
        "Content-Type": "application/json"
    }
    
    # Define the URL for a new thread of the PR
    para_url = f"https://dev.azure.com/{organization}/_apis/Contribution/HierarchyQuery/project/{project}?api-version=7.1-preview.1"
    
    para_body = {
        "contributionIds": [
            "ms.vss-build-web.pipeline-run-parameters-data-provider"
        ],
        "dataProviderContext": {
            "properties": {
                "pipelineId": f"{pipeline_definition_id}",
                "sourceBranch": "refs/heads/master",
                "onlyFetchTemplateParameters": True,
                "sourcePage": {
    
                    "routeId": "ms.vss-build-web.pipeline-details-route",
                    "routeValues": {
                        "project": f"{project}"
                    }
                }
            }
        }
    }
    
    # Make the API request
    para_response = requests.post(para_url, headers=headers, json=para_body).json()
    
    # Print the response
    print(json.dumps(para_response, indent=4))
    
    template_parameters = para_response.get("dataProviders", {}).get(
        "ms.vss-build-web.pipeline-run-parameters-data-provider", {}
    ).get("templateParameters", [])
    
    # Print the extracted template parameters
    print(json.dumps(template_parameters, indent=4))
    

    Image