Skip to content

Commit 64c1955

Browse files
greathongtuHongtu Zhang (FA Talent)
and
Hongtu Zhang (FA Talent)
authored
add pipeline yml for syncing fabricbot.json aliases (#20772)
* add pipeline yml for syncing fabricbot.json aliases * change git user name and email in script * change inline script to GetUncyclo2Json.ps1 file * remove unnecessary code * change wrong dir * change to my personal repo for test * change the usage of token * change CreatePR.ps1 for test * correct ParseUncyclo2Json.ps1 * remove unnecessary output * remove quote * finish test, change back to Azure repo * change branch name * change some names * remove the use of magic number * change commit message * remove redundant git pull * change the way of getting wiki content * change space to - * add md * set current dir * use space * use - * still use ADOToken * use a better wiki url * test purpose * test2 * test oauth * test3 * finish test * test again * test * finish test * test cron * test double pr * solve double pr * test * test * test * finish test * test * finish test * test restapi * te * finish test * test * finish test * filter pr * test * test * test * test * finish test --------- Co-authored-by: Hongtu Zhang (FA Talent) <[email protected]>
1 parent ebe9037 commit 64c1955

File tree

3 files changed

+136
-3
lines changed

3 files changed

+136
-3
lines changed

.azure-pipelines/sync-aliases.yml

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Variable 'ADOToken' and 'BotAccessToken' was defined in the Variables tab
2+
schedules:
3+
- cron: "50 15 * * *"
4+
displayName: 11:50 PM (UTC + 8:00) China Daily Run
5+
branches:
6+
include:
7+
- main
8+
9+
jobs:
10+
- job: UpdateJson
11+
displayName: Update fabricbot.json
12+
steps:
13+
- pwsh: |
14+
./tools/Github/ParseUncyclo2Json.ps1 -ADOToken $(ADOToken)
15+
displayName: Update fabricbot.json file locally
16+
17+
- task: PowerShell@2
18+
displayName: Git commit and push
19+
inputs:
20+
targetType: inline
21+
script: |
22+
git config --global user.email "[email protected]"
23+
git config --global user.name "azure-powershell-bot"
24+
git checkout -b "internal/sync-fabricbot-json"
25+
26+
git add .
27+
git commit -m "Sync fabricbot.json"
28+
29+
git remote set-url origin https://$(BotAccessToken)@github.com/Azure/azure-powershell.git;
30+
git push origin internal/sync-fabricbot-json --force
31+
32+
- pwsh: |
33+
$Title = "Sync fabricbot.json According To ADO Uncyclo Page"
34+
$HeadBranch = "internal/sync-fabricbot-json"
35+
$BaseBranch = "main"
36+
$Description = "This PR sync taskType: scheduledAndTrigger part of fabricbot.json from table of Service-Team-Label-and-Contact-List in ADO wiki page"
37+
./tools/Github/CreatePR.ps1 -Title $Title -HeadBranch $HeadBranch -BaseBranch $BaseBranch -BotAccessToken $(BotAccessToken) -Description $Description
38+
displayName: Create PR to main branch
39+

tools/Github/CreatePR.ps1

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,12 @@ param(
3131
[string]$BaseBranch,
3232

3333
[Parameter(Mandatory = $false)]
34-
[string]$Description
34+
[string]$Description,
35+
36+
[switch]$FailOnPRExisted = $false
3537
)
3638

37-
$Headers = @{"Accept" = "application/vnd.github+json"; "Authorization" = "Bearer $BotAccessToken"}
39+
$Headers = @{"Accept" = "application/vnd.github+json"; "Authorization" = "Bearer $BotAccessToken" }
3840
$PrBody = @"
3941
<!-- DO NOT DELETE THIS TEMPLATE -->
4042
@@ -63,5 +65,23 @@ $Description
6365
* **SHOULD NOT** introduce [breaking changes](../blob/main/documentation/breaking-changes/breaking-changes-definition.md) in Az minor release except preview version.
6466
* **SHOULD NOT** adjust version of module manually in pull request
6567
"@
68+
6669
$RequestBody = @{"title" = $Title; "body" = $PrBody; "head" = $HeadBranch; "base" = $BaseBranch }
67-
Invoke-WebRequest -Uri https://api.github.com/repos/Azure/azure-powershell/pulls -method POST -Headers $Headers -Body ($RequestBody | ConvertTo-Json)
70+
$Uri = "https://api.github.com/repos/Azure/azure-powershell/pulls"
71+
72+
$PrUri = "https://api.github.com/repos/Azure/azure-powershell/pulls?head=Azure:$HeadBranch&base=$BaseBranch"
73+
$PullRequests = Invoke-RestMethod -Uri $PrUri -Method GET -Headers $Headers
74+
if ($PullRequests.Length -eq 0) {
75+
Invoke-WebRequest -Uri $Uri -Method POST -Headers $Headers -Body ($RequestBody | ConvertTo-Json)
76+
exit 0
77+
}
78+
79+
if ($FailOnPRExisted) {
80+
Write-Error "The PR named $Title already exists."
81+
exit 1
82+
}
83+
else {
84+
Write-Host "The PR named $Title already exists. Skipping creation because FailOnPRExisted is set to $FailOnPRExisted."
85+
exit 0
86+
}
87+

tools/Github/ParseUncyclo2Json.ps1

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# ----------------------------------------------------------------------------------
2+
#
3+
# Copyright Microsoft Corporation
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
# Unless required by applicable law or agreed to in writing, software
9+
# distributed under the License is distributed on an "AS IS" BASIS,
10+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
# See the License for the specific language governing permissions and
12+
# limitations under the License.
13+
# ----------------------------------------------------------------------------------
14+
15+
<#
16+
.SYNOPSIS
17+
Sync ADO wiki aliases content to fabricbot.json.
18+
19+
#>
20+
param(
21+
[Parameter(Mandatory = $true)]
22+
[string]$ADOToken
23+
)
24+
25+
# get wiki content
26+
$username = ""
27+
$password = $ADOToken
28+
$pair = "{0}:{1}" -f ($username, $password)
29+
$bytes = [System.Text.Encoding]::ASCII.GetBytes($pair)
30+
$token = [System.Convert]::ToBase64String($bytes)
31+
$headers = @{
32+
Authorization = "Basic {0}" -f ($token)
33+
}
34+
35+
$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
36+
$rows = ($response.content -split "\n") | Where-Object { $_ -like '|*' } | Select-Object -Skip 2
37+
$aliases = [System.Collections.SortedList]::new()
38+
39+
foreach ($item in $rows) {
40+
$list = $item -split "\|"
41+
if ($list.Count -eq 1) { continue }
42+
if ($list[1].Trim().Length -gt 0) {
43+
if ($list.Count -gt 3) {
44+
$aliases.Add($list[1].Trim(), $list[3].Trim())
45+
}
46+
else {
47+
$aliases.Add($list[1].Trim(), "")
48+
}
49+
}
50+
}
51+
52+
# change json file
53+
$WholeJson = Get-Content -Raw -Path .github/fabricbot.json | ConvertFrom-Json
54+
55+
$WholeJson.tasks | ForEach-Object {
56+
if ($_.taskType -eq 'scheduledAndTrigger') {
57+
$labelsAndMentionsArrayList = New-Object System.Collections.ArrayList
58+
foreach ($entry in $aliases.GetEnumerator()) {
59+
if ($entry.Value -eq "") {
60+
continue
61+
}
62+
$labels = @("Service Attention", $entry.Key)
63+
$mentionees = @($entry.Value -split "," | ForEach-Object { $_.Trim() })
64+
$item = [PSCustomObject]@{
65+
labels = $labels
66+
mentionees = $mentionees
67+
}
68+
[void]$labelsAndMentionsArrayList.Add($item)
69+
}
70+
$_.config.labelsAndMentions = $labelsAndMentionsArrayList.ToArray()
71+
}
72+
}
73+
74+
($WholeJson | ConvertTo-Json -Depth 32) | Out-File -FilePath .github/fabricbot.json

0 commit comments

Comments
 (0)