我正在尝试在 Azure 板上自动创建任务和分支,但在创建任务后,我很难以编程方式创建分支。 我在 PowerShell 中使用 Azure/Azure Devops 扩展,但在文档中看不到任何可以复制下图所示的“创建分支”链接的内容:
感谢任何帮助
最佳答案
要以编程方式创建分支,有两种方法:
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:
在项目设置 >> 您的存储库中获取存储库 ID,或使用 REST API Repositories - List 并使用 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
使用 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/