{CI} Pipeline to auto sync resourceManagement.yml according to ADO Wiki Page - Service Contact List (#30012)

This commit is contained in:
Qi Pan 2024-10-24 16:19:58 +11:00 коммит произвёл GitHub
Родитель f5670493a9
Коммит 774f5305c7
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: B5690EEEBB952194
2 изменённых файлов: 238 добавлений и 0 удалений

Просмотреть файл

@ -0,0 +1,92 @@
name: Azure CLI Sync Alias
schedules:
- cron: "50 15 * * *"
displayName: 11:50 PM (UTC + 8:00) China Daily Run
branches:
include:
- dev
# The 'resources' and 'uses' below are used to resolve the error 'Repository associated with wiki ID <WikiId> does not exist or you do not have permissions for the operation you are attempting.'
resources:
repositories:
- repository: ServiceContactList
type: git
name: internal.wiki
jobs:
- job: UpdateYaml
displayName: Update resourceManagement.yml
pool: pool-windows-2019
uses:
repositories:
- ServiceContactList
steps:
- task: UseDotNet@2
displayName: Install .NET 8 SDK
inputs:
packageType: sdk
version: 8.0.x
- pwsh: |
dotnet --version
dotnet new tool-manifest --force
dotnet tool install powershell --version 7.4.*
displayName: Install PowerShell 7.4.x
- pwsh: |
dotnet tool run pwsh -NoLogo -NoProfile -NonInteractive -File ./tools/Github/ParseServiceContactsList.ps1 -AccessToken $env:SYSTEM_ACCESSTOKEN
env:
SYSTEM_ACCESSTOKEN: $(System.AccessToken)
displayName: Update resourceManagement.yml file locally
- pwsh: |
$hasChanges = git diff --name-only .github/policies
if ($null -eq $hasChanges) {
Write-Host "The wiki has no changes."
Write-Host "##vso[task.setvariable variable=ChangesDetected]false"
} else {
Write-Host "There are changes in the repository."
Write-Host "##vso[task.setvariable variable=ChangesDetected]true"
}
displayName: Check if Wiki table has any changes
- task: AzurePowerShell@5
inputs:
pwsh: true
azureSubscription: '$(AZURE_SDK_INFRA_SUB_CONNECTED_SERVICE)'
ScriptType: 'InlineScript'
Inline: |
$GithubToken = Get-AzKeyVaultSecret -VaultName $(GithubPATKeyVaultName) -Name $(GithubPATKeyVaultAccount) -AsPlainText
Write-Host "##vso[task.setvariable variable=GithubToken;issecret=true]$GithubToken"
azurePowerShellVersion: 'LatestVersion'
displayName: Get Github PAT from Key Vault
- pwsh: |
git config --global user.email "AzPyCLI@microsoft.com"
git config --global user.name "Azure CLI Team"
git checkout -b "sync_alias_$env:Build_BuildId"
git add .github/policies
git commit -m "Sync resourceManagement.yml"
git remote set-url origin https://azclibot:$(GithubToken)@github.com/Azure/azure-cli.git;
git push origin "sync_alias_$env:Build_BuildId" --force
displayName: Git commit and push
condition: and(succeeded(), eq(variables['ChangesDetected'], 'true'))
- pwsh: |
$Title = "{CI} Sync resourceManagement.yml according To ADO Wiki Page - Service Contact List"
$HeadBranch = "sync_alias_$env:Build_BuildId"
$BaseBranch = "dev"
$Description = "This PR synchronizes the task: 'Triage issues to the service team' part of resourceManagement.yml from table of Service Contact List in ADO wiki page"
$Headers = @{"Accept" = "application/vnd.github+json"; "Authorization" = "Bearer $(GithubToken)" }
$RequestBody = @{"title" = $Title; "body" = $Description; "head" = $HeadBranch; "base" = $BaseBranch;}
$Uri = "https://api.github.com/repos/Azure/azure-cli/pulls"
Invoke-WebRequest -Uri $Uri -Method POST -Headers $Headers -Body ($RequestBody | ConvertTo-Json)
displayName: Create PR to dev branch
condition: and(succeeded(), eq(variables['ChangesDetected'], 'true'))

Просмотреть файл

@ -0,0 +1,146 @@
# ----------------------------------------------------------------------------------
#
# Copyright Microsoft Corporation
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ----------------------------------------------------------------------------------
<#
.SYNOPSIS
Sync ADO Wiki Service Contact List to resourceManagement.yml.
#>
param(
[Parameter(Mandatory = $true)]
[string] $AccessToken
)
function InitializeRequiredPackages {
[CmdletBinding()]
param ()
$packagesDirectoryName = "JsonYamlPackages"
$packagesDirectory = Join-Path -Path . -ChildPath $packagesDirectoryName
if (Test-Path -LiteralPath $packagesDirectory) {
Remove-Item -LiteralPath $packagesDirectory -Recurse -Force
}
New-Item -Path . -Name $packagesDirectoryName -ItemType Directory -Force
$requiredPackages = @(
@{ PackageName = "Newtonsoft.Json"; PackageVersion = "13.0.2"; DllName = "Newtonsoft.Json.dll" },
@{ PackageName = "YamlDotNet"; PackageVersion = "13.2.0"; DllName = "YamlDotNet.dll" }
)
$requiredPackages | ForEach-Object {
$packageName = $_["PackageName"]
$packageVersion = $_["PackageVersion"]
$packageDll = $_["DllName"]
Install-Package -Name $packageName -RequiredVersion $packageVersion -Source "https://www.nuget.org/api/v2" -Destination $packagesDirectory -SkipDependencies -ExcludeVersion -Force
Add-Type -LiteralPath (Join-Path -Path $packagesDirectory -ChildPath $packageName | Join-Path -ChildPath "lib" | Join-Path -ChildPath "net6.0" | Join-Path -ChildPath $packageDll) -ErrorAction SilentlyContinue
}
}
# get wiki content
$username = ""
$password = $AccessToken
$pair = "{0}:{1}" -f ($username, $password)
$bytes = [System.Text.Encoding]::ASCII.GetBytes($pair)
$token = [System.Convert]::ToBase64String($bytes)
$headers = @{
Authorization = "Basic {0}" -f ($token)
}
$response = Invoke-RestMethod 'https://dev.azure.com/azclitools/internal/_apis/wiki/wikis/internal.wiki/pages?path=/Service%20Contact%20List&includeContent=true' -Headers $headers -ErrorAction Stop
$contactsList = ($response.content -split "\n") | Where-Object { $_ -like '|*' } | Select-Object -Skip 2
if ($null -ne $contactsList) {
$idxServiceTeamLabel = 2
$idxCLINotifyGithubHandler = 5
$serviceContacts = [System.Collections.Generic.SortedList[System.String, PSCustomObject]]::new()
foreach ($contacts in $contactsList) {
$items = $contacts -split "\|"
$colServiceTeamLabel = $items[$idxServiceTeamLabel]
if (![string]::IsNullOrWhiteSpace($colServiceTeamLabel)) {
$serviceTeamLabel = $colServiceTeamLabel.Trim()
$colCLINotifyGithubHandler = $items[$idxCLINotifyGithubHandler]
if (![string]::IsNullOrWhiteSpace($colCLINotifyGithubHandler)) {
$CLINotifyGithubHandler = $colCLINotifyGithubHandler.Trim()
[array]$mentionees = $CLINotifyGithubHandler.Split(",", [StringSplitOptions]::RemoveEmptyEntries) | ForEach-Object {
$_.Trim()
}
$serviceContacts.Add($serviceTeamLabel, [PSCustomObject]@{
if = @(
[PSCustomObject]@{
or = @(
[PSCustomObject]@{
labelAdded = [PSCustomObject]@{
label = 'Service Attention'
}
},
[PSCustomObject]@{
labelAdded = [PSCustomObject]@{
label = $serviceTeamLabel
}
}
)
},
[PSCustomObject]@{
hasLabel = [PSCustomObject]@{
label = 'Service Attention'
}
},
[PSCustomObject]@{
hasLabel = [PSCustomObject]@{
label = $serviceTeamLabel
}
}
)
then = @(
[PSCustomObject]@{
mentionUsers = [PSCustomObject]@{
mentionees = $mentionees
replyTemplate = 'Thanks for the feedback! We are routing this to the appropriate team for follow-up. cc ${mentionees}.'
assignMentionees = 'False'
}
}
)
})
}
}
}
}
# Update yaml file
InitializeRequiredPackages
$yamlConfigPath = $PSScriptRoot | Split-Path | Split-Path | Join-Path -ChildPath ".github" | Join-Path -ChildPath "policies" | Join-Path -ChildPath "resourceManagement.yml"
$yamlContent = Get-Content -Path $yamlConfigPath -Raw
$yamlDeserializer = [YamlDotNet.Serialization.DeserializerBuilder]::new().Build()
$yamlObjectGraph = $yamlDeserializer.Deserialize($yamlContent)
$jsonSerializer = [YamlDotNet.Serialization.SerializerBuilder]::new().JsonCompatible().Build()
$jsonObjectGraph = $jsonSerializer.Serialize($yamlObjectGraph) | ConvertFrom-Json
$serviceContactsTask = $jsonObjectGraph.configuration.resourceManagementConfiguration.eventResponderTasks | Where-Object { $_.description -eq "Triage issues to the service team" }
if ($null -ne $serviceContactsTask) {
$serviceContactsTask.then = $serviceContacts.Values
}
$updatedJsonContent = $jsonObjectGraph | ConvertTo-Json -Depth 64
$updatedJsonObjectGraph = [Newtonsoft.Json.JsonConvert]::DeserializeObject[System.Dynamic.ExpandoObject]($updatedJsonContent)
$yamlSerializer = [YamlDotNet.Serialization.SerializerBuilder]::new().Build()
$updatedYamlContent = $yamlSerializer.Serialize($updatedJsonObjectGraph)
$updatedYamlContent | Out-File -FilePath $yamlConfigPath -NoNewline -Force
# Remove trailing space on each line
(Get-Content -Path $yamlConfigPath) | ForEach-Object { $_.TrimEnd() } | Set-Content -Path $yamlConfigPath