git - Azure CLI 从任务创建新分支

标签 git azure powershell azure-devops azure-cli

我正在尝试在 Azure 板上自动创建任务和分支,但在创建任务后,我很难以编程方式创建分支。 我在 PowerShell 中使用 Azure/Azure Devops 扩展,但在文档中看不到任何可以复制下图所示的“创建分支”链接的内容:

create branch pic

感谢任何帮助

最佳答案

要以编程方式创建分支,有两种方法:

Az CLI:运行

az repos ref create --name refs/heads/{branch name} --object-id {Id of the object to create the reference from} --organization https://dev.azure.com/{org name} --project {project name} --repository {repos name}

休息API:

  1. 在项目设置 >> 您的存储库中获取存储库 ID,或使用 REST API Repositories - List 并使用 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
    
  2. 使用 Initial commit (Create a new branch) 从具有以下请求正文的特定分支创建分支。

{
  "refUpdates": [
    {
      "name": "refs/heads/{DefineNewBranchName}",
      "oldObjectId": "{oldObjectId}"
    }
  ],
  "commits": [
    {
      "comment": "Initial commit.",
      "changes": [
        {
          "changeType": "add",
          "item": {
            "path": "/readme.md"
          },
          "newContent": {
            "content": "My first file!",
            "contentType": "rawtext"
          }
        }
      ]
    }
  ]
}

接下来,您可以使用 Rest API 将工作项与现有分支链接:Work Items - Update

PATCH https://dev.azure.com/{organization}/{project}/_apis/wit/workitems/{id}?api-version=5.1

请求正文:

[
  {
    "op": "add",
    "path": "/relations/-",
    "value": {
      "rel": "ArtifactLink",
      "url": "vstfs:///Git/Ref/{ProjectID}/{RepoId}/GB{BranchName}",
      "attributes": {
        "name": "Branch",
        "comment": "test link branch"
      }
    }
  }
]

这是关于您的问题的类似 ticket

关于git - Azure CLI 从任务创建新分支,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72832142/

相关文章:

ios - 提交时层次结构中出现未知文件名

linux - Gobblin Git 克隆错误

powershell - 添加-AzureAccount : user_realm_discovery_failed: User realm discovery failed: Unable to connect to the remote

powershell - 移动具有相同名称但扩展名不同的文件。电源外壳

git - 权限被拒绝(公钥)从远程机器复制到本地

git - 如何让 git 忽略后/预提交 Hook 上的未暂存更改

azure - 将文件从 URL 复制到 Azure BLOB

Azure ML : finding the very similar client/payer

Powershell Invoke-Command 导致不同的结果

powershell - 访问被拒绝将域用户添加到本地管理员组