Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add pipeline yml for syncing fabricbot.json aliases #20772

Merged
merged 53 commits into from
Feb 9, 2023
Merged
Show file tree
Hide file tree
Changes from 42 commits
Commits
Show all changes
53 commits
Select commit Hold shift + click to select a range
2e9c6d2
add pipeline yml for syncing fabricbot.json aliases
Jan 30, 2023
f77bd21
change git user name and email in script
Jan 30, 2023
38b1f70
change inline script to GetWiki2Json.ps1 file
Feb 1, 2023
2e5c08a
remove unnecessary code
Feb 1, 2023
80aa187
change wrong dir
Feb 1, 2023
2831172
change to my personal repo for test
Feb 1, 2023
74313ff
change the usage of token
Feb 1, 2023
d25175c
change CreatePR.ps1 for test
Feb 1, 2023
c91b484
correct ParseWiki2Json.ps1
Feb 1, 2023
59a49c5
remove unnecessary output
Feb 1, 2023
d5e453e
remove quote
Feb 1, 2023
cf4bc10
finish test, change back to Azure repo
Feb 1, 2023
17b5be6
change branch name
Feb 1, 2023
11eccd6
change some names
Feb 1, 2023
bb4d892
remove the use of magic number
Feb 2, 2023
5b248b3
change commit message
Feb 2, 2023
91438f7
remove redundant git pull
Feb 2, 2023
dc0e489
change the way of getting wiki content
Feb 2, 2023
b5e6f7e
change space to -
Feb 2, 2023
0d2bbff
add md
Feb 2, 2023
affdb10
set current dir
Feb 2, 2023
ab482d1
use space
Feb 2, 2023
59d5cd6
use -
Feb 2, 2023
5d90c4b
still use ADOToken
Feb 2, 2023
635afa3
use a better wiki url
Feb 2, 2023
bcec48e
test purpose
Feb 2, 2023
0eef6bb
test2
Feb 2, 2023
fabc2b9
test oauth
Feb 2, 2023
60c55cb
test3
Feb 2, 2023
d796b9d
finish test
Feb 2, 2023
fdf250c
test again
Feb 5, 2023
198f2b0
test
Feb 6, 2023
51fffc1
finish test
Feb 6, 2023
ed35c0e
test cron
Feb 6, 2023
0f4ae7c
test double pr
Feb 6, 2023
b1924f2
solve double pr
Feb 6, 2023
d323d0d
test
Feb 7, 2023
d5ac369
test
Feb 7, 2023
e663991
test
Feb 7, 2023
95ff796
finish test
Feb 7, 2023
2d350cb
test
Feb 7, 2023
9bc5aab
finish test
Feb 7, 2023
2c5648d
test restapi
Feb 8, 2023
e829d30
te
Feb 8, 2023
e603ebb
finish test
Feb 8, 2023
878e781
test
Feb 8, 2023
077a711
finish test
Feb 8, 2023
9681afb
filter pr
Feb 8, 2023
40f8997
test
Feb 8, 2023
1b3634a
test
Feb 8, 2023
734537a
test
Feb 8, 2023
72f6684
test
Feb 8, 2023
1c4b9f3
finish test
Feb 8, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions .azure-pipelines/sync-aliases.yml
Original file line number Diff line number Diff line change
@@ -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:
greathongtu marked this conversation as resolved.
Show resolved Hide resolved
- 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

27 changes: 24 additions & 3 deletions tools/Github/CreatePR.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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 = @"
<!-- DO NOT DELETE THIS TEMPLATE -->

Expand Down Expand Up @@ -63,5 +65,24 @@ $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)
$Uri = "https://api.github.com/repos/Azure/azure-powershell/pulls"
wyunchi-ms marked this conversation as resolved.
Show resolved Hide resolved

$PullRequests = Invoke-WebRequest -Uri $Uri -Headers $Headers | ConvertFrom-Json
$ExistingPr = $PullRequests | Where-Object { $_.head.ref -eq $HeadBranch -and $_.base.ref -eq $BaseBranch }
greathongtu marked this conversation as resolved.
Show resolved Hide resolved

if (!$ExistingPr) {
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
}

78 changes: 78 additions & 0 deletions tools/Github/ParseWiki2Json.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
# ----------------------------------------------------------------------------------
#
# 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