Create Azure Devops Branch from a specific branch using Azure Devops Rest API

Viewed 136

I have some files which needs to be changed programmatically and if there is the change detected. Then a branch needs to be created and then a PR for the manual approval.

I got stuck when I am trying to create the branch from the master branch. Even though if I just want to use the Initial commit (https://docs.microsoft.com/en-us/rest/api/azure/devops/git/pushes/create?view=azure-devops-rest-5.1&tabs=HTTP#initial-commit-(create-a-new-branch)) and want to add the json file like this.

    $params = '{
    "refUpdates": [
      {
        "name": "refs/heads/feature/testBranchNish",
        "oldObjectId": "0000000000000000000000000000000000000000"
      }
    ],
    "commits": [
      {
        "comment": "Initial commit.",
        "changes": [
          {
            "changeType": "add",
            "item": {
              "path": "/setting.json"
            },
            "newContent": {
              "content": "{"version":1}",
              "contentType": "application/json"
            }
          }
        ]
      }
    ]
  }'

In the above Json, Here I need either one of the below two solutions but at this moment they both are not working.

First option: Because I need to update the json file So I should have to mention the contentType as "application/json" but it is giving the Newtonsoft.Json.JsonReaderException as mention below. Invoke-RestMethod : {"$id":"1","innerException":null,"message":"TF400898: An Internal Error Occurred. Activity Id: 65512b93-8924-4e40-8bd8-ba975418d8bd.","typeName":"Newtonsoft.Json.JsonReaderException, Newtonsoft.Json" ,"typeKey":"JsonReaderException","errorCode":0,"eventId":0} At line:27 char:1

+ CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand

Second Option: I don't want to provide the new content rather I would rather provide the file path something like this.

$params = '{
    "refUpdates": [
      {
        "name": "refs/heads/feature/testBranchNish",
        "oldObjectId": "0000000000000000000000000000000000000000"
      }
    ],
    "commits": [
      {
        "comment": "Initial commit.",
        "changes": [
          {
            "changeType": "add",
            "item": {
              "path": "/setting.json"
            },
            "newContent": {
              "path": "C:/Users/abc/setting.json",
              "contentType": "application/json"
            }
          }
        ]
      }
    ]
  }'

But I don't find this option anywhere in the Microsoft documentation. So the first option is more relevant to me while seeing the MS documentation.

Please answer if you guys already have a solution of this problem.

Thanks in advance

1 Answers

For your first option, it should be a request body issue. Please note that we cannot specify "contentType": "application/json", that's not supported. The "contentType" can be only set to "base64Encoded" or "rawText". See ItemContentType, enter image description here

I don't want to provide the new content rather I would rather provide the file path

I don't think we can achieve that by only provide the file path as you did in the second option. Basically, for Git we have to add the file to local repository first, then commit the change, after that push the changes to remote repository. So, we cannot add the file from an external path (out of the local repository) directly like upload a file from windows explorer.

However, you can try to get the contents from the json file and convert it to string, then use it in the request body.

Something like this : (However I cannot find a correct way to convert the contents to the valid string, you can do further research based on this sample)

$content = (Get-Content "C:/Users/abc/setting.json" -Raw)

function CreateJsonBody
{

    $value = @"
    {
        "refUpdates": [
          {
            "name": "refs/heads/feature/testBranchNish",
            "oldObjectId": "0000000000000000000000000000000000000000"
          }
        ],
        "commits": [
          {
            "comment": "Initial commit.",
            "changes": [
              {
                "changeType": "add",
                "item": {
                  "path": "/setting.json"
                },
                "newContent": {
                  "content": "$content",
                  "contentType": "rawText"
                }
              }
            ]
          }
        ]
    }
"@

 return $value
}

$json = CreateJsonBody
Related