You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何向Azure DevOps Cherry Pick API传入多个提交并创建PR?

批量Cherry Pick提交并创建Pull Request的PowerShell脚本示例

直接在commitList数组中添加多个包含commitId的哈希表对象,就能实现批量挑选提交进行Cherry Pick,完整脚本示例如下:

# 配置参数
$RepoName = "你的仓库名称"
$tempBranch = "refs/heads/cherry-pick-temp-20240520" # 临时分支名,建议带时间戳避免冲突
$targetBranch = "refs/heads/main" # 目标分支

# 构造Cherry Pick请求体,包含多个提交
$requestbody= @{
    "generatedRefName" = $tempBranch;
    "ontoRefName" = $targetBranch;
    "repository" = @{
        "name"= $RepoName
    };
    "source" = @{
        "commitList"= @(
            @{ "commitId" = "405d4e27a4ac33fcfc489076ae3b890571619483" },
            @{ "commitId" = "a1b2c3d4e5f6g7h8i9j0k1l2m3n4o5p6q7r8s9t0" },
            @{ "commitId" = "1234567890abcdef1234567890abcdef12345678" }
        )
    }
}

# 调用Cherry Pick API(以Azure DevOps为例,根据你的平台调整)
$apiUrl = "https://dev.azure.com/你的组织/_apis/git/repositories/$RepoName/cherryPicks?api-version=7.1-preview.1"
# 认证方式按需调整,这里用系统AccessToken示例
$headers = @{ Authorization = "Bearer $env:SYSTEM_ACCESSTOKEN" }
$response = Invoke-RestMethod -Uri $apiUrl -Method Post -Body ($requestbody | ConvertTo-Json -Depth 10) -ContentType "application/json" -Headers $headers

# 等待Cherry Pick任务完成(可选,根据API返回的状态判断)
Start-Sleep -Seconds 15

# 从临时分支创建Pull Request
$prRequestBody = @{
    "sourceRefName" = $tempBranch;
    "targetRefName" = $targetBranch;
    "title" = "批量Cherry Pick提交到主分支";
    "description" = "本次Cherry Pick包含以下提交:`n- 405d4e2:[提交1的描述]`n- a1b2c3d:[提交2的描述]`n- 1234567:[提交3的描述]"
}

$prApiUrl = "https://dev.azure.com/你的组织/_apis/git/repositories/$RepoName/pullrequests?api-version=7.1-preview.1"
$prResponse = Invoke-RestMethod -Uri $prApiUrl -Method Post -Body ($prRequestBody | ConvertTo-Json -Depth 10) -ContentType "application/json" -Headers $headers

Write-Host "Pull Request创建成功,链接:$($prResponse.url)"

关键说明

  • commitList里每个提交都是独立的哈希表对象,用逗号分隔,确保每个对象都包含commitId字段
  • 临时分支名建议设置成唯一值(比如加时间戳),避免和现有分支冲突
  • API调用部分需要根据你使用的代码托管平台(GitHub/GitLab/Azure DevOps等)调整URL和认证方式:
    • GitHub的Cherry Pick API逻辑不同,需要先创建分支再逐个Cherry Pick,然后创建PR
    • GitLab则对应projects/:id/repository/commits/:id/cherry_pick接口,批量提交需要循环调用或用流水线处理
  • 创建PR时,源分支就是Cherry Pick生成的临时分支,目标分支和Cherry Pick的ontoRefName保持一致

内容的提问来源于stack exchange,提问作者P Sriharsha

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.29 23:00:42