File: ParseServiceContactsList.ps1

package info (click to toggle)
azure-cli 2.82.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 2,359,416 kB
  • sloc: python: 1,910,381; sh: 1,343; makefile: 406; cs: 145; javascript: 74; sql: 37; xml: 21
file content (132 lines) | stat: -rw-r--r-- 6,105 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# ----------------------------------------------------------------------------------
#
# 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]@{
                            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