我正在通过 Python 创建一个桌面应用程序来在 Azure DevOps 中执行管道。我已经成功地使用组织名称、项目名称、管道 ID、PAT 等字段以及两个按钮(触发管道和查看过去的运行)来执行管道。
现在,我想添加一个带有“获取参数”的按钮。此按钮将获取管道的所有可用参数并使用单选按钮填充表单。
到目前为止,我可以使用 Azure DevOps REST API 执行管道。现在,我需要获取给定管道 ID 的所有参数并相应地填充它们。我尝试查看 Azure DevOps 提供的所有 REST API,但没有一个能提供帮助。
我知道这有点重新发明轮子。但是,如果有人可以提供帮助,我想了解这一点。
目前,没有内置的 Azure DevOps REST API 可以直接检索 YAML 管道的参数。但是,您可以通过利用从 Azure DevOps Web UI 提取的 API 请求来解决此问题。
这里有一个示例 Python 脚本供您参考。
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 to create a thread with comment
para_response = requests.post(para_url, headers=headers, json=para_body).json()
# Print the response of the new thread and the id
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))