在Azure Devops中创建repo时自动创建文件



创建存储库时是否可以自动创建文件?

例如:创建"Changelog.md">

就像"Readme.md"是用每个repo创建的。

简短回答:不,没有设置模板回购或更改默认模板的选项。

您可以使用rest api创建带有预定义文件的repos:

  1. 创建回购
  2. 初始提交(创建新分支(

PowerShell脚本示例:

$user = ""
$token = "<pat>" #https://learn.microsoft.com/en-us/azure/devops/organizations/accounts/use-personal-access-tokens-to-authenticate
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $user,$token)))
$orgUrl = "https://dev.azure.com/<org>"
$teamProject = "<TeamProjectName>"
$repoName = "NEW_REPO"
$restApiInitialCommit = "$orgUrl/$teamProject/_apis/git/repositories/$repoName/pushes?api-version=6.1-preview.2"
$restApiCreateRepo = "$orgUrl/$teamProject/_apis/git/repositories?api-version=7.1-preview.1"
$repoBody = "{`"name`": `"$repoName`"}"
$commitBody = @"
{
"refUpdates": [
{
"name": "refs/heads/main",
"oldObjectId": "0000000000000000000000000000000000000000"
}
],
"commits": [
{
"comment": "Updates file",
"changes": [
{
"changeType": "add",
"item": {
"path": "/Changelog.md"
},
"newContent": {
"content": "{newFileContentToUpdate}",
"contentType": "rawtext"
}
}
]
}
]
}
"@
function InvokePostRequest ($PostUrl, $body)
{   
return Invoke-RestMethod -Uri $PostUrl -Method Post -ContentType "application/json" -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}  -Body $body
}
InvokePostRequest $restApiCreateRepo $repoBody
$fileContent = Get-Content -Path "C:/temp/Changelog.md" -Raw
$fileContentToCommit = $fileContent.Replace("", "\")
$fileContentToCommit = $fileContentToCommit.Replace("`"", "`"")
$updateBody = $commitBody.Replace("{newFileContentToUpdate}", $fileContentToCommit);
InvokePostRequest $restApiInitialCommit $updateBody

相关内容

  • 没有找到相关文章

最新更新