使用像_apis/git/repositories/<Project>/pushes?api-version=6.0
这样的Devops推送端点,我们可以重命名或编辑文件。
这没问题。但是,我想在一次提交中重命名和编辑一个文件。我尝试在一个请求中传递两个更改,比如:
{
"changes": [
{
"changeType": "rename",
"item": {
"path": "/path/new-filename.txt"
},
"sourceServerItem": "/path/old-filename.txt"
},
{
"changeType": "edit",
"item": {
"path": "/path/new-filename.txt"
},
"newContent": {
"content": "...new content...",
"contentType": "rawtext"
}
}
]
}
这给出了错误"Multiple operations were attempted on file 'path/new-filename.txt' within the same request. Only a single operation may be applied to each file within the same commit. Parameter name: newPush"
所以我试着将它们与"所有"的变化类型结合起来
{
"changeType": "all",
"item": {
"path": "/path/new-filename.txt",
},
"sourceServerItem": "/path/old-filename.txt",
"newContent": {
"content": "...new content...",
"contentType": "rawText"
}
}
仍然没有乐趣:"The parameters supplied are not valid. Parameter name: newPush"
这可能吗?或者我必须在两次提交中分开更改吗?
编辑:
甚至不能在一个请求中进行多次提交😭.我的意思是,当您无论如何都必须有一个提交时,将提交作为数组有什么意义?
The parameters are incorrect. A posted push must contain exactly one commit and one refUpdate.
Parameter name: newPush
使用Rest API进行测试,我可以重现相同的情况。
这个问题来自Rest API本身的限制。当我们使用Rest API执行推送操作时,它限制了您只能进行一次提交,并且不能同时对同一文件进行更改。
您需要将更改分为两次提交。
为了满足您的要求,您可以使用PowerShell脚本运行Rest API两次以提交更改。
例如:
$token = "PAT"
$url="https://dev.azure.com/{Org}/{Project}/_apis/git/repositories/{Repo}/pushes?api-version=6.0"
$token = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($token)"))
$JSON = @'
{
"refUpdates": [
{
"name": "refs/heads/master",
"oldObjectId": "commitid"
}
],
"commits": [
{
"comment": "Renaming tasks.md to xxx",
"changes": [
{
"changeType": "rename",
"sourceServerItem": "/path/old-filename.txt",
"item": {
"path": "/path/new-filename.txt"
}
}
]
}
]
}
'@
$response = Invoke-RestMethod -Uri $url -Headers @{Authorization = "Basic $token"} -Method Post -Body $JSON -ContentType application/json
$newobjectid = $response.commits.commitid
echo $newobjectid
$JSON1 = "
{
`"refUpdates`": [
{
`"name`": `"refs/heads/master`",
`"oldObjectId`": `"$newobjectid`"
}
],
`"commits`": [
{
`"comment`": `"Renaming tasks.md to xxx`",
`"changes`": [
{
`"changeType`": `"edit`",
`"item`": {
`"path`": `"/path/new-filename.txt`"
},
`"newContent`": {
`"content`": `"...new content...`",
`"contentType`": `"rawtext`"
}
}
]
}
]
}
"
$response1 = Invoke-RestMethod -Uri $url -Headers @{Authorization = "Basic $token"} -Method Post -Body $JSON1 -ContentType application/json