|
| 1 | +parameters: |
| 2 | +- name: targetBranch |
| 3 | + displayName: Target branch where the PR will be submitted |
| 4 | + type: string |
| 5 | + default: main |
| 6 | + |
| 7 | +- name: scope |
| 8 | + displayName: Select target Autorest modules |
| 9 | + type: string |
| 10 | + default: autorest-all |
| 11 | + values: |
| 12 | + - autorest-all |
| 13 | + - autorest-v3-only |
| 14 | + - autorest-v4-only |
| 15 | + - autorest-selected |
| 16 | + |
| 17 | +- name: selectedServices |
| 18 | + displayName: Input service names you want to refresh separated by semi-colon (;). Wildcard match is supported. |
| 19 | + type: string |
| 20 | + default: ' ' |
| 21 | + |
| 22 | +- name: changeLogMessage |
| 23 | + displayName: Input change log message for the Autorest modules |
| 24 | + type: string |
| 25 | + |
| 26 | +pr: none |
| 27 | +trigger: none |
| 28 | + |
| 29 | +jobs: |
| 30 | +- job: |
| 31 | + displayName: Refresh Autorest Modules |
| 32 | + timeoutInMinutes: 360 |
| 33 | + pool: pool-windows-2019 |
| 34 | + steps: |
| 35 | + - template: util/get-github-pat-steps.yml |
| 36 | + |
| 37 | + - task: NodeTool@0 |
| 38 | + displayName: Use Node 14.15.5 |
| 39 | + inputs: |
| 40 | + versionSpec: 14.15.5 |
| 41 | + |
| 42 | + - task: Npm@1 |
| 43 | + displayName: Install Autorest |
| 44 | + inputs: |
| 45 | + command: custom |
| 46 | + verbose: false |
| 47 | + customCommand: install autorest@latest -g |
| 48 | + |
| 49 | + - task: PowerShell@2 |
| 50 | + displayName: Get services list |
| 51 | + inputs: |
| 52 | + pwsh: true |
| 53 | + targetType: inline |
| 54 | + workingDirectory: ./src |
| 55 | + script: | |
| 56 | + $autorestPaths = @() |
| 57 | + $autorestModules = Get-ChildItem -Filter "*.Autorest" -Directory -Recurse | Select-Object -ExpandProperty FullName |
| 58 | + if ("${{ parameters.scope }}" -eq "autorest-v3-v4") { |
| 59 | + $autorestPaths = $autorestModules |
| 60 | + } |
| 61 | + elseif ("${{ parameters.scope }}" -eq "autorest-selected") { |
| 62 | + $selectedModulesList = '${{ parameters.selectedServices }}' |
| 63 | + if (![System.String]::IsNullOrWhiteSpace($selectedModulesList)) { |
| 64 | + $selectedModulePatterns = $selectedModulesList -split ";" |
| 65 | + $autorestPaths = $selectedModulePatterns | Foreach-Object { |
| 66 | + $modulePattern = $_.Trim() |
| 67 | + $autorestModules | Where-Object { |
| 68 | + $modulePath = Split-Path -Path $_ -Parent |
| 69 | + $moduleName = (Get-Item -Path $modulePath).Name |
| 70 | + $moduleName -like $modulePattern |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + else { |
| 76 | + $isV3Only = "${{ parameters.scope }}" -eq "autorest-v3-only" |
| 77 | + $isV4Only = "${{ parameters.scope }}" -eq "autorest-v4-only" |
| 78 | + $readmeMDs = $autorestModules | Get-ChildItem -Filter "README.md" -File |
| 79 | + $readmeMDs | Foreach-Object { |
| 80 | + $content = Get-Content -Path $_ -Raw |
| 81 | + $pattern = 'use-extension:\s*"@autorest/powershell":\s*"([^"]+)"' |
| 82 | + if ($content -match $pattern) { |
| 83 | + $version = $Matches[1].Substring(0, 1) |
| 84 | + if (($version -eq "3" -and $isV3Only) -or ($version -eq "4" -and $isV4Only)) { |
| 85 | + $autorestPaths += $_.Directory.FullName |
| 86 | + } |
| 87 | + } |
| 88 | + elseif ($isV4Only) { |
| 89 | + $autorestPaths += $_.Directory.FullName |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | +
|
| 94 | + $autorestPaths = $autorestPaths | Sort-Object -Unique |
| 95 | + Write-Host "##[section]Autorest modules to refresh: `n`t$($autorestPaths -join "`n`t")" |
| 96 | + $autorestPathsList = $autorestPaths -join ";" |
| 97 | + Write-Host "##vso[task.setvariable variable=selectedAutorestPaths;isreadonly=true]$autorestPathsList" |
| 98 | +
|
| 99 | + - task: PowerShell@2 |
| 100 | + displayName: Generate & Build |
| 101 | + inputs: |
| 102 | + pwsh: true |
| 103 | + targetType: inline |
| 104 | + script: | |
| 105 | + # Autorest and build the modules |
| 106 | + '$(selectedAutorestPaths)'.Split(";") | Foreach-Object { |
| 107 | + Write-Host "##[group]Start generating module $_" |
| 108 | +
|
| 109 | + Set-Location -Path $_ |
| 110 | +
|
| 111 | + Write-Host "##[section]Start running autorest command." |
| 112 | + autorest --max-memory-size=8192 |
| 113 | + Write-Host "##[section]Finish running autorest command." |
| 114 | +
|
| 115 | + Write-Host "##[section]Start building the module." |
| 116 | + ./build-module.ps1 |
| 117 | + Write-Host "##[section]Finish building the module." |
| 118 | +
|
| 119 | + Write-Host "##[endgroup]" |
| 120 | + Write-Host |
| 121 | + } |
| 122 | +
|
| 123 | + # Backup the modules in the artifacts folder and restore the changes |
| 124 | + $modules = '$(selectedAutorestPaths)' -split ";" | Foreach-Object { Split-Path -Path $_ -Parent } | Select-Object -Unique |
| 125 | + $modules | Foreach-Object { |
| 126 | + Write-Host "##[group]Start backing up module $_" |
| 127 | +
|
| 128 | + Set-Location -Path $_ |
| 129 | + $servicePath = Get-Item -Path . |
| 130 | + $serviceName = $servicePath.Name |
| 131 | +
|
| 132 | + Write-Host "##[section]Start copying content to artifacts." |
| 133 | + New-Item -Path ../../artifacts/src/$serviceName -ItemType Directory |
| 134 | + Copy-Item -Path ./* -Destination ../../artifacts/src/$serviceName -Recurse |
| 135 | + Write-Host "##[section]Finish copying content to artifacts." |
| 136 | +
|
| 137 | + Set-Location .. |
| 138 | +
|
| 139 | + Write-Host "##[section]Start restoring the changes." |
| 140 | + Remove-Item -Path ./$serviceName -Recurse -Force |
| 141 | + git checkout $serviceName/ |
| 142 | + Write-Host "##[section]Finish restoring the changes." |
| 143 | +
|
| 144 | + Write-Host "##[endgroup]" |
| 145 | + Write-Host |
| 146 | + } |
| 147 | +
|
| 148 | + Set-Location .. |
| 149 | + git checkout -b 'codegen/${{ parameters.scope }}' 'origin/${{ parameters.targetBranch }}' |
| 150 | +
|
| 151 | + - task: PowerShell@2 |
| 152 | + displayName: Migrate from generation to target branch |
| 153 | + inputs: |
| 154 | + pwsh: true |
| 155 | + targetType: inline |
| 156 | + script: | |
| 157 | + Install-Module -Name PowerShellGet -RequiredVersion 2.2.3 -Force |
| 158 | + Install-Module -Name platyPS -RequiredVersion 0.14.2 -Force |
| 159 | + Install-Module -Name Az.Accounts -Force |
| 160 | + Import-Module ./tools/Gen2Master/MoveFromGeneration2Master.ps1 |
| 161 | +
|
| 162 | + $modules = '$(selectedAutorestPaths)' -split ";" | Foreach-Object { Split-Path -Path $_ -Parent } | Select-Object -Unique |
| 163 | + $modules | Foreach-Object { |
| 164 | + Write-Host "##[group]Start migrating module $_" |
| 165 | +
|
| 166 | + Set-Location -Path $_ |
| 167 | + $servicePath = Get-Item -Path . |
| 168 | + $serviceName = $servicePath.Name |
| 169 | +
|
| 170 | + Write-Host "##[section]Start migrating from generation to target." |
| 171 | + Move-Generation2Master -SourcePath ../../artifacts/src/$serviceName -DestPath . |
| 172 | + Write-Host "##[section]Finish migrating from generation to target." |
| 173 | +
|
| 174 | + Write-Host "##[section]Start updating change log for module." |
| 175 | + $changeLog = $servicePath | Get-ChildItem -Filter "ChangeLog.md" -File -Recurse | Select-Object -First 1 |
| 176 | + $changeLogPath = $changeLog.FullName |
| 177 | + $changeLogMessage = '${{ parameters.changeLogMessage }}' |
| 178 | + (Get-Content -Path $changeLogPath -Raw) -replace "`n## Upcoming Release", ("`n## Upcoming Release`n" + "* $changeLogMessage") | Set-Content -Path $changeLogPath -NoNewline |
| 179 | + Write-Host "##[section]Finish updating change log for module." |
| 180 | +
|
| 181 | + Write-Host "##[endgroup]" |
| 182 | + Write-Host |
| 183 | + } |
| 184 | +
|
| 185 | + - task: PowerShell@2 |
| 186 | + displayName: Create PR to target branch |
| 187 | + inputs: |
| 188 | + pwsh: true |
| 189 | + targetType: inline |
| 190 | + script: | |
| 191 | + $sourceBranch = '$(Build.SourceBranch)'.Replace("refs/heads/", "") |
| 192 | + $headBranch = 'codegen/${{ parameters.scope }}' |
| 193 | + $baseBranch = '${{ parameters.targetBranch }}' |
| 194 | + git config user.email "[email protected]" |
| 195 | + git config user.name "azure-powershell-bot" |
| 196 | + git add src |
| 197 | + git add tools/CreateMappings_rules.json |
| 198 | + git commit -m "Refresh ${{ parameters.scope }} modules from $sourceBranch to $baseBranch" |
| 199 | + git remote set-url origin https://azure-powershell-bot:$(GithubToken)@github.com/Azure/azure-powershell.git |
| 200 | + git push origin $headBranch --force |
| 201 | +
|
| 202 | + $title = "Refresh ${{ parameters.scope }} modules from $sourceBranch to $baseBranch" |
| 203 | + $description = "Refresh ${{ parameters.scope }} modules from $sourceBranch to $baseBranch." |
| 204 | + if ("${{ parameters.scope }}" -eq "autorest-selected") { |
| 205 | + $description = "$description `nSelected services are: ${{ parameters.selectedServices }}." |
| 206 | + } |
| 207 | +
|
| 208 | + ./tools/Github/CreatePR.ps1 -Title $title -HeadBranch $headBranch -BaseBranch $baseBranch -BotAccessToken $(GithubToken) -Description $description |
0 commit comments