diff --git a/.azure-pipelines/sync-aliases.yml b/.azure-pipelines/sync-aliases.yml new file mode 100644 index 000000000000..3a23dc628122 --- /dev/null +++ b/.azure-pipelines/sync-aliases.yml @@ -0,0 +1,39 @@ +# Variable 'ADOToken' and 'BotAccessToken' was defined in the Variables tab +schedules: +- cron: "50 15 * * *" + displayName: 11:50 PM (UTC + 8:00) China Daily Run + branches: + include: + - main + +jobs: +- job: UpdateJson + displayName: Update fabricbot.json + steps: + - pwsh: | + ./tools/Github/ParseWiki2Json.ps1 -ADOToken $(ADOToken) + displayName: Update fabricbot.json file locally + + - task: PowerShell@2 + displayName: Git commit and push + inputs: + targetType: inline + script: | + git config --global user.email "65331932+azure-powershell-bot@users.noreply.github.com" + git config --global user.name "azure-powershell-bot" + git checkout -b "internal/sync-fabricbot-json" + + git add . + git commit -m "Sync fabricbot.json" + + git remote set-url origin https://$(BotAccessToken)@github.com/Azure/azure-powershell.git; + git push origin internal/sync-fabricbot-json --force + + - pwsh: | + $Title = "Sync fabricbot.json According To ADO Wiki Page" + $HeadBranch = "internal/sync-fabricbot-json" + $BaseBranch = "main" + $Description = "This PR sync taskType: scheduledAndTrigger part of fabricbot.json from table of Service-Team-Label-and-Contact-List in ADO wiki page" + ./tools/Github/CreatePR.ps1 -Title $Title -HeadBranch $HeadBranch -BaseBranch $BaseBranch -BotAccessToken $(BotAccessToken) -Description $Description + displayName: Create PR to main branch + \ No newline at end of file diff --git a/tools/Github/CreatePR.ps1 b/tools/Github/CreatePR.ps1 index 5214a699d282..874e3d434b51 100644 --- a/tools/Github/CreatePR.ps1 +++ b/tools/Github/CreatePR.ps1 @@ -31,10 +31,12 @@ param( [string]$BaseBranch, [Parameter(Mandatory = $false)] - [string]$Description + [string]$Description, + + [switch]$FailOnPRExisted = $false ) -$Headers = @{"Accept" = "application/vnd.github+json"; "Authorization" = "Bearer $BotAccessToken"} +$Headers = @{"Accept" = "application/vnd.github+json"; "Authorization" = "Bearer $BotAccessToken" } $PrBody = @" @@ -63,5 +65,23 @@ $Description * **SHOULD NOT** introduce [breaking changes](../blob/main/documentation/breaking-changes/breaking-changes-definition.md) in Az minor release except preview version. * **SHOULD NOT** adjust version of module manually in pull request "@ + $RequestBody = @{"title" = $Title; "body" = $PrBody; "head" = $HeadBranch; "base" = $BaseBranch } -Invoke-WebRequest -Uri https://api.github.com/repos/Azure/azure-powershell/pulls -method POST -Headers $Headers -Body ($RequestBody | ConvertTo-Json) \ No newline at end of file +$Uri = "https://api.github.com/repos/Azure/azure-powershell/pulls" + +$PrUri = "https://api.github.com/repos/Azure/azure-powershell/pulls?head=Azure:$HeadBranch&base=$BaseBranch" +$PullRequests = Invoke-RestMethod -Uri $PrUri -Method GET -Headers $Headers +if ($PullRequests.Length -eq 0) { + Invoke-WebRequest -Uri $Uri -Method POST -Headers $Headers -Body ($RequestBody | ConvertTo-Json) + exit 0 +} + +if ($FailOnPRExisted) { + Write-Error "The PR named $Title already exists." + exit 1 +} +else { + Write-Host "The PR named $Title already exists. Skipping creation because FailOnPRExisted is set to $FailOnPRExisted." + exit 0 +} + diff --git a/tools/Github/ParseWiki2Json.ps1 b/tools/Github/ParseWiki2Json.ps1 new file mode 100644 index 000000000000..c80ea2094335 --- /dev/null +++ b/tools/Github/ParseWiki2Json.ps1 @@ -0,0 +1,74 @@ +# ---------------------------------------------------------------------------------- +# +# 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 aliases content to fabricbot.json. + +#> +param( + [Parameter(Mandatory = $true)] + [string]$ADOToken +) + +# get wiki content +$username = "" +$password = $ADOToken +$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/azure-sdk/internal/_apis/wiki/wikis/internal.wiki/pages?path=/Engineering%20System/GitHub%20Repos/Issue%20Management/Service%20Team%20Label%20and%20Contact%20List&includeContent=True' -Headers $headers +$rows = ($response.content -split "\n") | Where-Object { $_ -like '|*' } | Select-Object -Skip 2 +$aliases = [System.Collections.SortedList]::new() + +foreach ($item in $rows) { + $list = $item -split "\|" + if ($list.Count -eq 1) { continue } + if ($list[1].Trim().Length -gt 0) { + if ($list.Count -gt 3) { + $aliases.Add($list[1].Trim(), $list[3].Trim()) + } + else { + $aliases.Add($list[1].Trim(), "") + } + } +} + +# change json file +$WholeJson = Get-Content -Raw -Path .github/fabricbot.json | ConvertFrom-Json + +$WholeJson.tasks | ForEach-Object { + if ($_.taskType -eq 'scheduledAndTrigger') { + $labelsAndMentionsArrayList = New-Object System.Collections.ArrayList + foreach ($entry in $aliases.GetEnumerator()) { + if ($entry.Value -eq "") { + continue + } + $labels = @("Service Attention", $entry.Key) + $mentionees = @($entry.Value -split "," | ForEach-Object { $_.Trim() }) + $item = [PSCustomObject]@{ + labels = $labels + mentionees = $mentionees + } + [void]$labelsAndMentionsArrayList.Add($item) + } + $_.config.labelsAndMentions = $labelsAndMentionsArrayList.ToArray() + } +} + +($WholeJson | ConvertTo-Json -Depth 32) | Out-File -FilePath .github/fabricbot.json