如何使用 Azure DevOps REST API 更新文件?
How to use Azure DevOps REST API to Update File?
我正在尝试更新托管在 Azure DevOps Repo 上的 test.json
文件。我正在使用逻辑应用程序。无法从文档中识别操作顺序。
我想我需要...
- 发出 GET HTTP 请求
https://dev.azure.com/myOrg/myProject/_apis/git/repositories/myRepoID/items?scopePath=/data/test.json&$format=json&api-version=6.0
- 回复:
{
"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"
}
]
}
在响应中使用 objectId
向 Pushes
endpoint
发出 POST HTTP 请求
- 正文:
{
"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
}
问题:
- 我的操作顺序正确吗?
- 我该如何解决这个问题?
FIX:(谢谢@Leo_Liu-MSFT)
GET 请求 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>"
}
]
}
How to use Azure DevOps REST API to Update File?
请求体中的oldObjectId
不是objectId
的值。
它应该是 分支 main
的最新提交 SHA。
- 转到代码页 > 文件
- 选择存储库和分支
- Select 根级别(存储库名称)> 历史记录
- 单击第一个提交的 … > 复制完整的 SHA
用于创建新分支时,该值应为创建新分支0000000000000000000000000000000000000000
。
我正在尝试更新托管在 Azure DevOps Repo 上的 test.json
文件。我正在使用逻辑应用程序。无法从文档中识别操作顺序。
我想我需要...
- 发出 GET HTTP 请求
https://dev.azure.com/myOrg/myProject/_apis/git/repositories/myRepoID/items?scopePath=/data/test.json&$format=json&api-version=6.0
- 回复:
{ "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" } ] }
在响应中使用
发出 POST HTTP 请求objectId
向Pushes
endpoint- 正文:
{
"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
}
问题:
- 我的操作顺序正确吗?
- 我该如何解决这个问题?
FIX:(谢谢@Leo_Liu-MSFT)
GET 请求
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>"
}
]
}
How to use Azure DevOps REST API to Update File?
请求体中的oldObjectId
不是objectId
的值。
它应该是 分支 main
的最新提交 SHA。
- 转到代码页 > 文件
- 选择存储库和分支
- Select 根级别(存储库名称)> 历史记录
- 单击第一个提交的 … > 复制完整的 SHA
用于创建新分支时,该值应为创建新分支0000000000000000000000000000000000000000
。