1
votes

I am trying to change source from azure repos git to GitHub in azure DevOps build using rest api.

This is the response I get for azure repos using azure Devops build definitions rest api - GET https://dev.azure.com/{org_name}/{project_name}/_apis/build/definitions/{Build_Id}?api-version=6.0?

"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
},

If manually I change source from azure repos to GitHub this is the json response I get for GitHub repo -

  "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

I tried to change azure repo to github using postman by copying GitHub json response body and adding in postman and tried to call put -https://dev.azure.com/{org_name}/{project_name}/_apis/build/definitions/{Build_Id}?api-version=6.0?

But this does not work

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

enter image description here

enter image description here

1

1 Answers

1
votes

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

You could copy the content of the Get Build Definition API.

Here is my example:

URL:

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

Request Body sample:

{
    "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"
}

In the Reuqest Body, there are the following key points:

  1. The Process field is required. You could copy the content from the Get Build Definition Rest API.

  2. The "id": {DefinitionID} is required.

  3. "revision": {revisionID} You need to input the valid revision. This is very important.

To get the correct revision, you need to Navigate to Azure Pipelines -> Target Build Definition -> History.

enter image description here

You need to count how many Update records. The correct revision is the total number + 1.

For example: In my screenshot, the correct revision is 10 (9+1 =10).