如何使用 rest api 将 azure DevOps 构建定义源从 azure repos 更新到 GitHub

How to update azure DevOps build definition source from azure repos to GitHub using rest api

我正在尝试使用 rest api.

在 azure DevOps 构建中将源代码从 azure repos git 更改为 GitHub

这是我使用 azure Devops 构建定义 rest api - GET https://dev.azure.com/{org_name}/{project_name}/_apis/build/definitions/{Build_Id}?api-version=6.0?

得到的 azure repos 的响应
"repository": {
    "properties": {
        "cleanOptions": "0",
        "labelSources": "0",
        "labelSourcesFormat": "$(build.buildNumber)",
        "reportBuildStatus": "true",
        "gitLfsSupport": "false",
        "skipSyncSource": "false",
        "checkoutNestedSubmodules": "false",
        "fetchDepth": "0"
    },
    "id": "xxxx",
    "type": "TfsGit",
    "name": "{repo_name}",
    "url": "https://dev.azure.com/{org_name}/{project_name}/_git/{repo_name}",
    "defaultBranch": "refs/heads/master",
    "clean": "false",
    "checkoutSubmodules": false
},

如果我手动将源从 azure repos 更改为 GitHub,这是我对 GitHub repo -

得到的 json 响应
  "repository": {
    "properties": {
        "apiUrl": "https://api.github.com/repos/{github_id}/{repo_name}",
        "branchesUrl": "https://api.github.com/repos/{github_id}/{repo_name}/branches",
        "cloneUrl": "https://github.com/{github_id}/{repo_name}.git",
        "connectedServiceId": "xxxxxxx",
        "defaultBranch": "master",
        "fullName": "{github_id}/{repo_name}",
        "hasAdminPermissions": "True",
        "isFork": "False",
        "isPrivate": "False",
        "lastUpdated": "10/16/2019 17:28:29",
        "manageUrl": "https://github.com/{github_id}/{repo_name}",
        "nodeId": "xxxxxx",
        "ownerId": "xxxxx",
        "orgName": "{github_id}",
        "refsUrl": "https://api.github.com/repos/{github_id}/pyapp/git/refs",
        "safeRepository": "{github_id}/pyapp",
        "shortName": "{repo_name}",
        "ownerAvatarUrl": "https://avatars2.githubusercontent.com/u/xxxxx?v=4",
        "archived": "False",
        "externalId": "xxxxxx",
        "ownerIsAUser": "True",
        "checkoutNestedSubmodules": "false",
        "cleanOptions": "0",
        "fetchDepth": "0",
        "gitLfsSupport": "false",
        "reportBuildStatus": "true",
        "skipSyncSource": "false",
        "labelSourcesFormat": "$(build.buildNumber)",
        "labelSources": "0"
    },
 "id": "{github_id}/{repo_name}",
        "type": "GitHub",
        "name": "{github_id}/{repo_name}",
        "url": "https://github.com/{github_id}/{repo_name}.git",
        "defaultBranch": "master",
        "clean": "false",
       "checkoutSubmodules": false

我尝试通过复制 GitHub json 响应正文并添加邮递员来使用邮递员将 azure repo 更改为 github 并尝试调用 put -https://dev.azure.com/{org_name}/{project_name}/_apis/build/definitions/{Build_Id}?api-version= 6.0?

但这不起作用

如何使用脚本或邮递员实现此目的?我在这里错过了什么?

How can I achieve this using script or postman ? what am I missing here ?

您可以复制 Get Build Definition API 的内容。

这是我的例子:

URL:

PUT https://dev.azure.com/{OrganizationName}/{ProjectName}/_apis/build/definitions/{DefinitionID}?api-version=5.0-preview.6

请求正文样本:

{
    "process": {
        "phases": [
            {
                "steps": [

                ],
                "name": "Phase 1",
                "refName": "Phase_1",
                "condition": "succeeded()",
                "target": {
                    "executionOptions": {
                        "type": 0
                    },
                    "allowScriptsAuthAccessOption": false,
                    "type": 1
                },
                "jobAuthorizationScope": "projectCollection",
                "jobCancelTimeoutInMinutes": 1
            }
        ],
        "type": 1
    },
    "repository": {
        "properties": {
            "cleanOptions": "0",
            "labelSources": "0",
            "labelSourcesFormat": "$(build.buildNumber)",
            "reportBuildStatus": "true",
            "gitLfsSupport": "false",
            "skipSyncSource": "false",
            "checkoutNestedSubmodules": "false",
            "fetchDepth": "0"
        },
         "id": "{github_id}/{repo_name}",
        "type": "GitHub",
        "name": "{github_id}/{repo_name}",
        "url": "https://github.com/{github_id}/{repo_name}.git",
        "defaultBranch": "master",
        "clean": "false",
        "checkoutSubmodules": false
    },
    "id": {DefinitionID},
    "revision": {revisionID},
    "name": "definitionCreatedByRESTAPI",
    "type": "build",
    "queueStatus": "enabled"
}

在Reuqest Body中,有以下几个要点:

  1. 进程 字段是必需的。您可以从 Get Build Definition Rest API.

    复制内容
  2. 需要"id": {DefinitionID}

  3. "revision": {revisionID} 您需要输入有效的修订版。这非常重要

要获得正确的修订版,您需要导航至 Azure Pipelines -> Target Build Definition -> History

您需要统计有多少更新记录。正确的修订是 total number + 1.

例如:在我的截图中,正确的版本是10(9+1 =10)。