如何使用 Azure DevOps REST API 更新文件?
我正在尝试更新 Azure DevOps Repo 上托管的 test.json
文件。我正在使用逻辑应用程序。无法从文档中识别操作顺序。
我认为我需要...
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” } ] } 使用响应中的
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
}
问题:
- 我的操作顺序正确吗?
- 我该如何克服这个问题?
修复:(谢谢@Leo_Liu-MSFT)
向
https://dev.azure.com/myOrg/myProject/_apis/git/repositories/repoID/commits 发出请求? searchCriteria.$top=1&searchCriteria.itemVersion.version=main&api-version=6.0
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...
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" } ] }
Use the
objectId
in the response to issue a POST HTTP request to thePushes
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:
- Am I correct on the order of operations?
- How do I overcome this issue?
FIX: (Thank you @Leo_Liu-MSFT)
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
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
请求正文中的
oldObjectId
不是objectId
的值。它应该是分支
main
的最新提交 SHA。并且在用于创建新分支时,该值应为 create a newbranch
0000000000000000000000000000000000000000
。The
oldObjectId
in the request body is not the value of theobjectId
.It should be the the latest commit SHA for the branch
main
.And the value should be create a new branch
0000000000000000000000000000000000000000
when used to create a new branch.