Skip to content

Commit

Permalink
add pipeline yml for syncing fabricbot.json aliases (#20772)
Browse files Browse the repository at this point in the history
* add pipeline yml for syncing fabricbot.json aliases

* change git user name and email in script

* change inline script to GetWiki2Json.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 ParseWiki2Json.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) <v-hongtzhang@microsoft.com>
  • Loading branch information
greathongtu and Hongtu Zhang (FA Talent) authored Feb 9, 2023
1 parent ebe9037 commit 64c1955
Show file tree
Hide file tree
Showing 3 changed files with 136 additions and 3 deletions.
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:
- 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
26 changes: 23 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,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)
$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
}

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

0 comments on commit 64c1955

Please sign in to comment.