[英]Multiple Initial Branches in Azure DevOps Repositories
I was wondering if it is possible to make Azure DevOps to automatically create both master
and develop
branches upon creating a new repository.我想知道是否有可能使 Azure DevOps 在创建新存储库时自动创建
master
分支和develop
分支。 I want both branches protected.我希望两个分支都受到保护。
If project level branch policies are set up for both, but only master
is created by default, when creating develop
manually via git push --set-upstream origin develop
I am able to push a number of commits directly on the branch, the protective policies are only applied after.如果为两者都设置了项目级分支策略,但默认情况下只创建
master
,当通过git push --set-upstream origin develop
develop
创建开发时,我可以直接在分支上推送一些提交,保护策略仅在之后应用。
You could use REST API to automatically create a new repo with two branch created at the same time.您可以使用 REST API 自动创建一个新的 repo,同时创建两个分支。 Use API to Create a repository first, and get repository Id from the response.
使用 API 首先创建一个存储库,并从响应中获取存储库 ID。 Use the $RepoID in next API to Create new branch .
使用下一个 API 中的 $RepoID 创建新分支。
You could create two new branches with the oldObjectId "0000000000000000000000000000000000000000".您可以使用 oldObjectId“0000000000000000000000000000000000000000”创建两个新分支。 Here is the PowerShell script that works for me.
这是对我有用的 PowerShell 脚本。 Need to replace orgName, PAT, NewRepoName, ProjectID, branch name .
需要替换orgName, PAT, NewRepoName, ProjectID, branch name 。
$url = "https://dev.azure.com/<orgName>/_apis/git/repositories?api-version=7.0"
$AzureDevOpsPAT = '<PAT>'
$AzureDevOpsAuthenicationHeader = @{Authorization = 'Basic ' + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(":$($AzureDevOpsPAT)")) }
$body = "{`"name`": `"<NewRepoName>`", `"project`": {`"id`": `"<ProjectID>`"}}"
$response = Invoke-RestMethod -Uri $url -Method POST -Headers $AzureDevOpsAuthenicationHeader -ContentType "application/json" -Body $body
$response | ConvertTo-Json
$RepoID = $response.id
$url2 = "https://dev.azure.com/<orgName>/_apis/git/repositories/$RepoID/pushes?api-version=5.1"
$body2 = "{`"refUpdates`": [{`"name`": `"refs/heads/<master>`", `"oldObjectId`": `"0000000000000000000000000000000000000000`"}], `"commits`": [{`"comment`": `"Initial commit.`", `"changes`": [{`"changeType`": `"add`", `"item`": {`"path`": `"/readme.md`"}, `"newContent`": {`"content`": `"My first file!`", `"contentType`": `"rawtext`"}}]}]}"
Invoke-RestMethod -Uri $url2 -Method POST -Headers $AzureDevOpsAuthenicationHeader -ContentType "application/json" -Body $body2
$body3 = "{`"refUpdates`": [{`"name`": `"refs/heads/<develop>`", `"oldObjectId`": `"0000000000000000000000000000000000000000`"}], `"commits`": [{`"comment`": `"Initial commit.`", `"changes`": [{`"changeType`": `"add`", `"item`": {`"path`": `"/readm.md`"}, `"newContent`": {`"content`": `"My first file!`", `"contentType`": `"rawtext`"}}]}]}"
Invoke-RestMethod -Uri $url2 -Method POST -Headers $AzureDevOpsAuthenicationHeader -ContentType "application/json" -Body $body3
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.