How to find oldobjectid when Uploading/pushing a file to Azure Devops Repo using Pushes Create Api with Python

Viewed 565

I was trying to automate the task of pushing some files to various folder in a repo. I tried using Rest API provided by azure. When using Pushes Create API for the same, from the docs this is the content in the request body

Snapshot of request body:

This is the specific thing i can't figure out how to get in python script "oldObjectId": "8b67126d2500e28c771f82c9ddc292679978197c"

I know I have to send a JSON variable containing the data similar to above using python requests library. But I don't know how to get the oldobjectid in the python script. I tried looking everywhere(documentation,stackoverflow....) but can't see to find how we can find the oldobjectid.

The link for the documentation of the api

1 Answers

How to find oldobjectid when Uploading/pushing a file to Azure Devops Repo using Pushes Create Api with Python

The oldobjectid is the latest commit ID on the current branch for the Azure Devops Repo.

To get the oldObjectId (latest commit id), we could use the REST API Pushes - List with URI Parameters top=1 and searchCriteria.refName=refs/heads/master:

GET https://dev.azure.com/{organization}/{project}/_apis/git/repositories/{repositoryId}/pushes?&$top=1&searchCriteria.refName=refs/heads/master&api-version=6.0

enter image description here

Now, we get the pushId, and we could use the REST API Pushes - Get to get the commitId:

enter image description here

Then, we could use the REST API Pushes - Create with request body:

{
  "refUpdates": [
    {
      "name": "refs/heads/master",
      "oldObjectId": "e71544e80870e83cfd3eb3a797eda9c6227c66a7"
    }
  ],
  "commits": [
    {
      "comment": "Added task markdown file.",
      "changes": [
        {
          "changeType": "add",
          "item": {
            "path": "/tasks.md"
          },
          "newContent": {
            "content": "# Tasks\n\n* Item 1\n* Item 2",
            "contentType": "rawtext"
          }
        }
      ]
    }
  ]
}

The test result:

enter image description here

Related