如何使用 Azure DevOps REST API 更新文件?

发布于 2025-01-10 21:00:31 字数 3300 浏览 6 评论 0原文

我正在尝试更新 Azure DevOps Repo 上托管的 test.json 文件。我正在使用逻辑应用程序。无法从文档中识别操作顺序。

我认为我需要...

  1. Items 端点:

    • https://dev.azure.com/myOrg/myProject/_apis/git/repositories/myRepoID/items?scopePath=/data/test.json&$format=json&api-version=6.0< /代码>
    • 回复:
    <代码>{
      “计数”:1,
      “价值”: [
        {
          "objectId": "",
          “gitObjectType”:“blob”,
          "commitId": "",
          “路径”:“/data/test.json”,
          “url”:“https://dev.azure.com/myOrg/longGUID/_apis/git/repositories/myRepoID/items?path=%2Fdata%2Ftest.json&versionType=Branch&versionOptions=None”
        }
      ]
    }
    
  2. 使用响应中的objectId推送端点

    • 正文:
{
  "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"
    }
  ]
}

错误:

  • 状态 409 冲突
{
  "$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
}

问题:

  1. 我的操作顺序正确吗?
  2. 我该如何克服这个问题?

修复:(谢谢@Leo_Liu-MSFT)

  1. https://dev.azure.com/myOrg/myProject/_apis/git/repositories/repoID/commits 发出请求? searchCriteria.$top=1&searchCriteria.itemVersion.version=main&api-version=6.0

  2. POST 请求至https://dev.azure.com/myOrg/myProject/_apis/git/repositories/repoID/pushes

  • 正文:
{
  "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>"
    }
  ]
}

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

遗忘曾经 2025-01-17 21:00:31

如何使用 Azure DevOps REST API 更新文件?

请求正文中的 oldObjectId 不是 objectId 的值。

它应该是分支main最新提交 SHA。

  • 转到代码页>文件
  • 选择存储库和分支
  • 选择根级别(存储库名称)> 历史记录
  • 第一次提交的 点击…>复制完整 SHA

并且在用于创建新分支时,该值应为 create a newbranch 0000000000000000000000000000000000000000

输入图片此处描述

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文