我正在寻找一个Azure DevOps Rest API从现有分支创建新分支。
答案 0 :(得分:2)
Azure DevOps Rest API从特定分支创建分支
Konteks指出了正确的REST API。
我们可以使用Initial commit (Create a new branch)创建分支,但是如果您要从特定分支创建分支,则需要修改请求正文。
POST https://dev.azure.com/fabrikam/_apis/git/repositories/{repositoryId}/pushes?api-version=5.1
首先,我们需要使用REST API Repositories - List来获取repositoryId
。
GET https://dev.azure.com/{organization}/{project}/_apis/git/repositories?api-version=4.1
然后,将REST API Refs - List与filter=<BranchName>
结合使用,以获取特定分支的oldObjectId
:
GET https://dev.azure.com/{organization}/{project}/_apis/git/repositories/{repositoryId}/refs?filter=heads/master&api-version=5.1
现在,我们可以将REST API Repositories - List
与以下请求正文一起使用,以从特定分支创建一个新分支 :
{
"refUpdates": [
{
"name": "refs/heads/<DefineYourNewBranchName>",
"oldObjectId": "<oldObjectId we get from above REST API>"
}
],
"commits": [
{
"comment": "Initial commit.",
"changes": [
{
"changeType": "add",
"item": {
"path": "/readme111.md"
},
"newContent": {
"content": "My first file!",
"contentType": "rawtext"
}
}
]
}
]
}
邮递员的结果:
这是我测试的Powershell脚本:
$connectionToken="<PAT Here>"
$base64AuthInfo= [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($connectionToken)"))
$headers = @{ Authorization = "Bearer $env:SYSTEM_ACCESSTOKEN" }
$url= "https://dev.azure.com/<Organizationname>/<ProjectName>/_apis/git/repositories/<repositoryId>/pushes?api-version=5.1"
$body =@"
{
"refUpdates": [
{
"name": "refs/heads/Test228",
"oldObjectId": "a57f0c34f8ec7330bdc37e7631f7be3cc3ea3956"
}
],
"commits": [
{
"comment": "Initial commit.",
"changes": [
{
"changeType": "add",
"item": {
"path": "/readme111.md"
},
"newContent": {
"content": "My first file!",
"contentType": "rawtext"
}
}
]
}
]
}
"@
Write-Host "$url"
$response= Invoke-RestMethod -Uri $url -ContentType "application/json-patch+json" -Body $body -headers @{authorization = "Basic $base64AuthInfo"} -Method POST
希望这会有所帮助。
答案 1 :(得分:2)
我捕获了AzureDevOps Web UI创建的请求,发现对于REST api,从特定分支创建分支是非常简单的任务。
$repository = "repositoryName"
$newBranch = "newBranchName"
$baseBranch = "baseBranchName"
$organization = "organizationName"
$project = "projectName"
$pat = "personalAccessToken"
$base64AuthInfo = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$pat"))
$headers = @{ Authorization = "Basic $base64AuthInfo" }
# Get ID of the base branch
$url = "https://dev.azure.com/$organization/$project/_apis/git/repositories/$repository/refs?filter=heads/$baseBranch&api-version=5.1"
$baseBranchResponse = Invoke-RestMethod -Uri $url -ContentType "application/json" -headers $headers -Method GET
# Create a new branch
$url = "https://dev.azure.com/$organization/$project/_apis/git/repositories/$repository/refs?api-version=5.1"
$body = ConvertTo-Json @(
@{
name = "refs/heads/$newBranch"
newObjectId = $baseBranchResponse.value.objectId
oldObjectId = "0000000000000000000000000000000000000000"
})
$response = Invoke-RestMethod -Uri $url -ContentType "application/json" -Body $body -headers $headers -Method POST
答案 2 :(得分:0)
答案 3 :(得分:0)
这对我有用:
要获取objectId: https://docs.microsoft.com/en-us/rest/api/azure/devops/git/refs/list?view=azure-devops-rest-5.0#refs
答案 4 :(得分:0)
Sparrow插件可以满足您的需求(使用Rest API):
s6 --plg-run ado-git-branch-create@project=Backends,repo=Catalog,branch_from=dev,branch=feature
请注意,它不会为此创建任何伪文件,它仅通过其内部对象ID引用现有分支。
随时保持code;-))。
PS披露。我是工具的作者,更多详细信息可以在这里找到-https://github.com/melezhik/Spazure