如何在 Uploading/pushing 使用 Pushes Create Api 和 Python 将文件发送到 Azure Devops Repo 时查找 oldobjectid

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

我试图自动执行将一些文件推送到存储库中的各个文件夹的任务。我尝试使用 azure 提供的 Rest API。当使用 Pushes Create API 时,从文档中这是请求正文中的内容

请求正文快照:

这是我不知道如何进入 python 脚本的具体内容 “oldObjectId”:“8b67126d2500e28c771f82c9ddc292679978197c”

我知道我必须使用 python 请求库发送一个包含与上面类似的数据的 JSON 变量。但是我不知道如何在 python 脚本中获取 oldobjectid。我试着到处寻找(文档、Whosebug ....)但看不到我们如何找到 oldobjectid。

link 用于文档 api

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

oldobjectid 是 Azure Devops Repo 当前分支上的最新提交 ID

要获取 oldObjectId(最新提交 ID),我们可以使用带有 URI 参数 top=1searchCriteria.refName=refs/heads/master 的 REST API Pushes - List

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

现在,我们得到 pushId,我们可以使用 REST API Pushes - Get 得到 commitId:

然后,我们可以使用 REST API 推送 - 使用请求正文创建:

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

测试结果: