Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Azure DevOps REST API to Update File?

I'm trying to Update a test.json file hosted on a Azure DevOps Repo. I'm using a Logic App. Having trouble identifying the order of operations from the documentation.

I think I need to...

  1. Issue a GET HTTP request to the Items endpoint:

    • https://dev.azure.com/myOrg/myProject/_apis/git/repositories/myRepoID/items?scopePath=/data/test.json&$format=json&api-version=6.0
    • Response:
    {
      "count": 1,
      "value": [
        {
          "objectId": "<longGUID>",
          "gitObjectType": "blob",
          "commitId": "<longGUID>",
          "path": "/data/test.json",
          "url": "https://dev.azure.com/myOrg/longGUID/_apis/git/repositories/myRepoID/items?path=%2Fdata%2Ftest.json&versionType=Branch&versionOptions=None"
        }
      ]
    }
    
  2. Use the objectId in the response to issue a POST HTTP request to the Pushes endpoint

    • Body:
{
  "refUpdates": [
    {
      "name": "refs/heads/main",
      "oldObjectId": "<longGuid from previous response>"
    }
  ],
  "commits": [
    {
      "changes": [
        {
          "changeType": "edit",
          "item": {
            "path": "/data/test.json"
          },
          "newContent": {
            "content": "CHECK CHECK!",
            "contentType": "rawtext"
          }
        }
      ],
      "comment": "My commit message"
    }
  ]
}

Error:

  • Status 409 Conflict
{
  "$id": "1",
  "innerException": null,
  "message": "TF401028: The reference 'refs/heads/main' has already been updated by another client, so you cannot update it. Please try again.",
  "typeName": "Microsoft.TeamFoundation.Git.Server.GitReferenceStaleException, Microsoft.TeamFoundation.Git.Server",
  "typeKey": "GitReferenceStaleException",
  "errorCode": 0,
  "eventId": 3000
}

Questions:

  1. Am I correct on the order of operations?
  2. How do I overcome this issue?

FIX: (Thank you @Leo_Liu-MSFT)

  1. GET request to https://dev.azure.com/myOrg/myProject/_apis/git/repositories/repoID/commits?searchCriteria.$top=1&searchCriteria.itemVersion.version=main&api-version=6.0

  2. POST request to https://dev.azure.com/myOrg/myProject/_apis/git/repositories/repoID/pushes

  • Body:
{
  "commits": [
    {
      "changes": [
        {
          "changeType": "edit",
          "item": {
            "path": "<Your File To Update>"
          },
          "newContent": {
            "content": "CHECK CHECK!",
            "contentType": "rawtext"
          }
        }
      ],
      "comment": "<YOUR COMMIT MSG>"
    }
  ],
  "refUpdates": [
    {
      "name": "refs/heads/main",
      "oldObjectId": "<commitId from previous response>"
    }
  ]
}
like image 234
SeaDude Avatar asked Sep 12 '25 00:09

SeaDude


1 Answers

How to use Azure DevOps REST API to Update File?

The oldObjectId in the request body is not the value of the objectId.

It should be the the latest commit SHA for the branch main.

  • Go to the code page > Files
  • Choose a repository and branch
  • Select the root level (repository name) > History
  • Click … of the first commit > Copy full SHA

And the value should be create a new branch 0000000000000000000000000000000000000000 when used to create a new branch.

enter image description here

like image 179
Leo Liu-MSFT Avatar answered Sep 14 '25 19:09

Leo Liu-MSFT