Azure DevOps的Rest API:从特定分支创建分支

10
我正在寻找一个Azure DevOps Rest API,用于从现有分支创建新分支。
5个回答

13
使用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 仓库 - 列表 来获取 repositoryId

GET https://dev.azure.com/{organization}/{project}/_apis/git/repositories?api-version=4.1

然后,使用 REST API Refs - Listfilter=<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"
          }
        }
      ]
    }
  ]
}

Postman的结果:

enter image description here

这是我的测试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
如果我只想要一个新的分支怎么办?不是添加或更改文件,只是一个新的分支?我可以简单地省略请求JSON中的“提交”部分吗? - Tom Padilla
1
@TomPadilla,我也需要这样做。如果你只想从另一个分支简单地创建一个新分支,请执行Leo提供的所有步骤,但在最后一步中进行POST时,请按照示例from here中的请求正文操作。还要记得在链接示例中使用第二步获取的repositoryId作为newObjectId。即newObjectId代表你从第二步获取的源分支引用。 - Eric Chhun
谢谢您,这正是我需要了解的。 - Tom Padilla
如果我想从另一个分支创建一个分支,但不是最新提交,而是特定的先前提交,该怎么办? - Alejandro Galera

11

我捕获了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

很棒的解决方案!我在使用时有一些小提示。要将基础分支指定为主分支,只需写“master”即可,无需完整名称“refs/heads/master”(似乎是在幕后使用的)。关于PAT令牌,您需要具有读取和编写代码权限才能允许创建分支。 - Fhyarnir
你好,我们有没有办法在从主分支创建分支时添加提交日期?我需要使用特定日期的最后一次提交从主分支创建一个分支。 目前,我正在使用此语法来获取日期。但不知道如何基于该日期创建分支$url="https://dev.azure.com/$organization/$project/_apis/git/repositories/$repository/commits?searchCriteria.toDate=$toDate&api-version=6.0"提前感谢 - Kart
1
我不确定我是否正确理解了您的要求。但是,如果您想在指定的提交上创建一个新分支,您可以使用“提交”端点获取提交 ID 并在创建新分支时使用它。像这样:$url = "https://dev.azure.com/$organization/$project/_apis/git/repositories/$repository/commits?searchCriteria.fromDate=07/15/2021&api-version=5.1"$commitsResponse = Invoke-RestMethod -Uri $url -ContentType "application/json" -headers $headers -Method GET然后,newObjectId = $commitsResponse.value[0].commitId - Miroslav Ježík
我需要从指定日期的最后提交创建一个分支。为此,我已经根据日期收集了提交ID。以下是我的命令。$url = "https://dev.azure.com/$organization/$project/_apis/git/repositories/$repository/commits?searchCriteria.toDate=$toDate&api-version=6.0" $response = Invoke-RestMethod -Uri $url -Method GET -headers $headers $output = $response.value | Select -First 1 $commitid = $output.commitid到目前为止,我获取了该指定日期的提交ID。现在,我被困在从该提交ID创建分支上。你能帮我一下吗? - Kart
1
最终,我已经从提交ID创建了分支$url = "https://dev.azure.com/$organization/$project/_apis/git/repositories/$repository/refs?api-version=6.0" $body = ConvertTo-Json @( @{ name = "refs/heads/$newBranch" newObjectId = $output.commitid oldObjectId = "0000000000000000000000000000000000000000" }) $response = Invoke-RestMethod -Uri $url -Method POST感谢Miroslav Ježík的回复。 - Kart

1

怎么做到的?天哪,你是如何理解那个混乱的东西的?整个页面上没有“创建”或“添加”(除了标题)。他们给出了一个例子,但是它是什么……?同时展示所有三个动作的随机示例?老实说,我应该如何阅读那个页面,并从中获得Leo Liu-MSFT发布的代码呢? - Tom Padilla
点赞,因为它报告了链接到微软官方文档。遗憾的是,那里的文档没有更好的记录。 - Andry


0

Sparrow插件可以满足您的需求(使用Rest API):

s6 --plg-run ado-git-branch-create@project=Backends,repo=Catalog,branch_from=dev,branch=feature

请注意,它不会为此创建任何虚拟文件,它只通过其内部对象ID引用现有分支。
随意窃取代码 ;-))。

PS 披露。我是这个工具的作者,更多细节可以在这里找到 - https://github.com/melezhik/Spazure


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接