From d4f6b4bca001418c86dddee34c602102cf9c1306 Mon Sep 17 00:00:00 2001 From: Chidozie Ononiwu Date: Wed, 9 Sep 2020 15:38:43 -0700 Subject: [PATCH 1/6] Update get-codeowners powershell file --- eng/common/scripts/get-codeowners.ps1 | 82 +++++++++++++++++++++++---- 1 file changed, 70 insertions(+), 12 deletions(-) diff --git a/eng/common/scripts/get-codeowners.ps1 b/eng/common/scripts/get-codeowners.ps1 index f28cb3df789..3150d9fe965 100644 --- a/eng/common/scripts/get-codeowners.ps1 +++ b/eng/common/scripts/get-codeowners.ps1 @@ -1,7 +1,10 @@ param ( $TargetDirectory, # should be in relative form from root of repo. EG: sdk/servicebus $RootDirectory, # ideally $(Build.SourcesDirectory) - $VsoVariable = "" # target devops output variable + $AuthToken, + $OwningUsers = "", # target devops output variable + $OwningTeams = "", + $OwningLabels = "" ) $target = $TargetDirectory.ToLower().Trim("/") $codeOwnersLocation = Join-Path $RootDirectory -ChildPath ".github/CODEOWNERS" @@ -13,6 +16,7 @@ if (!(Test-Path $codeOwnersLocation)) { } $codeOwnersContent = Get-Content $codeOwnersLocation +$previousLine foreach ($contentLine in $codeOwnersContent) { if (-not $contentLine.StartsWith("#") -and $contentLine){ @@ -20,27 +24,81 @@ foreach ($contentLine in $codeOwnersContent) { # CODEOWNERS file can also have labels present after the owner aliases # gh aliases start with @ in codeowners. don't pass on to API calls - $ownedFolders[$splitLine[0].ToLower().Trim("/")] = ($splitLine[1..$($splitLine.Length)] ` - | ? { $_.StartsWith("@") } ` - | % { return $_.substring(1) }) -join "," + + $aliases = ($splitLine[1..$($splitLine.Length)] | ? { $_.StartsWith("@") } | % { return $_.substring(1) }) -join "," + $labels = "" + + if ($null -ne $previousLine -and $previousLine.StartsWith("# PRLabel:")) + { + $previousLine = $previousLine.Replace("# PRLabel: ","") + $splitPrevLine = $previousLine -split "%" + $labels = ($splitPrevLine[1..$($splitPrevLine.Length)] | % { return $_.Trim() }) -join "," + } + + $ownedFolders[$splitLine[0].ToLower().Trim("/")] = @{ Aliases = $aliases; Labels = $labels } } + $previousLine = $contentLine } $results = $ownedFolders[$target] if ($results) { - Write-Host "Found a folder $results to match $target" - - if ($VsoVariable) { - $alreadyPresent = [System.Environment]::GetEnvironmentVariable($VsoVariable) + Write-Host "Found a folder to match $target" + $aliases = $results.Aliases -split "," + $users + $teams + + if ($AuthToken) { + $headers = @{ + Authorization = "bearer $AuthToken" + } + } + + foreach ($str in $aliases) + { + $usersApiUrl = "https://api.github.com/users/$str" + try + { + $response = Invoke-RestMethod -Headers $headers $usersApiUrl + if ($users) { $users += ("," + $str) } else { $users += $str } + } + catch { + if ($_.Exception.Response.StatusCode.Value__ -eq 404) # Consider it a team Alias + { + if ($teams) { $teams += ("," + $str) } else { $teams += $str } + } + else{ + LogError "Invoke-RestMethod ${usersApiUrl} failed with exception:`n$_" + exit 1 + } + } + } + + if ($OwningUsers) { + $presentOwningUsers = [System.Environment]::GetEnvironmentVariable($OwningUsers) + if ($presentOwningUsers) { + $users += ",$presentOwningUsers" + } + Write-Host "##vso[task.setvariable variable=$OwningUsers;]$users" + } + + if ($OwningTeams) { + $presentOwningTeams = [System.Environment]::GetEnvironmentVariable($OwningTeams) + if ($presentOwningTeams) { + $teams += ",$presentOwningTeams" + } + Write-Host "##vso[task.setvariable variable=$OwningTeams;]$teams" + } - if ($alreadyPresent) { - $results += ",$alreadyPresent" + if ($OwningLabels) { + $presentOwningLabels = [System.Environment]::GetEnvironmentVariable($OwningLabels) + if ($presentOwningLabels) { + $labels += ",$presentOwningLabels" } - Write-Host "##vso[task.setvariable variable=$VsoVariable;]$results" + Write-Host "##vso[task.setvariable variable=$OwningLabels;]$($result.Labels)" } - return $results + return @{ Users = $users; Teams = $teams; Labels = $results.Labels } } else { Write-Host "Unable to match path $target in CODEOWNERS file located at $codeOwnersLocation." From dfc66bc4a916948293a008c5c21b5ef6531d49ca Mon Sep 17 00:00:00 2001 From: Chidozie Ononiwu Date: Fri, 11 Sep 2020 11:18:48 -0700 Subject: [PATCH 2/6] Update alias verification logic --- eng/common/scripts/get-codeowners.ps1 | 76 ++++++++++++++++----------- 1 file changed, 46 insertions(+), 30 deletions(-) diff --git a/eng/common/scripts/get-codeowners.ps1 b/eng/common/scripts/get-codeowners.ps1 index 3150d9fe965..9396cc4213f 100644 --- a/eng/common/scripts/get-codeowners.ps1 +++ b/eng/common/scripts/get-codeowners.ps1 @@ -2,9 +2,9 @@ param ( $TargetDirectory, # should be in relative form from root of repo. EG: sdk/servicebus $RootDirectory, # ideally $(Build.SourcesDirectory) $AuthToken, - $OwningUsers = "", # target devops output variable - $OwningTeams = "", - $OwningLabels = "" + $VsoOwningUsers = "", # target devops output variable + $VsoOwningTeams = "", + $VsoOwningLabels = "" ) $target = $TargetDirectory.ToLower().Trim("/") $codeOwnersLocation = Join-Path $RootDirectory -ChildPath ".github/CODEOWNERS" @@ -16,7 +16,26 @@ if (!(Test-Path $codeOwnersLocation)) { } $codeOwnersContent = Get-Content $codeOwnersLocation -$previousLine + +function VerifyAlias($APIUrl) +{ + if ($AuthToken) { + $headers = @{ + Authorization = "bearer $AuthToken" + } + } + try + { + $response = Invoke-RestMethod -Headers $headers $APIUrl + } + catch + { + Write-Host "Invoke-RestMethod ${APIUrl} failed with exception:`n$_" + Write-Host "This might be because a team alias was used for user API request or vice versa." + return $false + } + return $true +} foreach ($contentLine in $codeOwnersContent) { if (-not $contentLine.StartsWith("#") -and $contentLine){ @@ -28,9 +47,9 @@ foreach ($contentLine in $codeOwnersContent) { $aliases = ($splitLine[1..$($splitLine.Length)] | ? { $_.StartsWith("@") } | % { return $_.substring(1) }) -join "," $labels = "" - if ($null -ne $previousLine -and $previousLine.StartsWith("# PRLabel:")) + if ($null -ne $previousLine -and $previousLine.Contains("PRLabel:")) { - $previousLine = $previousLine.Replace("# PRLabel: ","") + $previousLine = $previousLine.substring($previousLine.IndexOf(':') + 1) $splitPrevLine = $previousLine -split "%" $labels = ($splitPrevLine[1..$($splitPrevLine.Length)] | % { return $_.Trim() }) -join "," } @@ -48,54 +67,51 @@ if ($results) { $users $teams - if ($AuthToken) { - $headers = @{ - Authorization = "bearer $AuthToken" - } - } - foreach ($str in $aliases) { $usersApiUrl = "https://api.github.com/users/$str" - try + if (VerifyAlias -APIUrl $usersApiUrl) { - $response = Invoke-RestMethod -Headers $headers $usersApiUrl if ($users) { $users += ("," + $str) } else { $users += $str } } - catch { - if ($_.Exception.Response.StatusCode.Value__ -eq 404) # Consider it a team Alias + else { + if ($str.IndexOf('/') -ne -1) # Check if it's a team alias e.g. Azure/azure-sdk-eng { - if ($teams) { $teams += ("," + $str) } else { $teams += $str } - } - else{ - LogError "Invoke-RestMethod ${usersApiUrl} failed with exception:`n$_" - exit 1 + $org = $str.substring(0, $str.IndexOf('/')) + $team_slug = $str.substring($str.IndexOf('/') + 1) + $teamApiUrl = "https://api.github.com/orgs/$org/teams/$team_slug" + if (VerifyAlias -APIUrl $teamApiUrl) + { + if ($teams) { $teams += ("," + $str) } else { $teams += $str } + continue + } } + Write-Host "Alias ${str} is neither a recognized github user nor a team" } } - if ($OwningUsers) { - $presentOwningUsers = [System.Environment]::GetEnvironmentVariable($OwningUsers) + if ($VsoOwningUsers) { + $presentOwningUsers = [System.Environment]::GetEnvironmentVariable($VsoOwningUsers) if ($presentOwningUsers) { $users += ",$presentOwningUsers" } - Write-Host "##vso[task.setvariable variable=$OwningUsers;]$users" + Write-Host "##vso[task.setvariable variable=$VsoOwningUsers;]$users" } - if ($OwningTeams) { - $presentOwningTeams = [System.Environment]::GetEnvironmentVariable($OwningTeams) + if ($VsoOwningTeams) { + $presentOwningTeams = [System.Environment]::GetEnvironmentVariable($VsoOwningTeams) if ($presentOwningTeams) { $teams += ",$presentOwningTeams" } - Write-Host "##vso[task.setvariable variable=$OwningTeams;]$teams" + Write-Host "##vso[task.setvariable variable=$VsoOwningTeams;]$teams" } - if ($OwningLabels) { - $presentOwningLabels = [System.Environment]::GetEnvironmentVariable($OwningLabels) + if ($VsoOwningLabels) { + $presentOwningLabels = [System.Environment]::GetEnvironmentVariable($VsoOwningLabels) if ($presentOwningLabels) { $labels += ",$presentOwningLabels" } - Write-Host "##vso[task.setvariable variable=$OwningLabels;]$($result.Labels)" + Write-Host "##vso[task.setvariable variable=$VsoOwningLabels;]$($result.Labels)" } return @{ Users = $users; Teams = $teams; Labels = $results.Labels } From 2f1cc3fb047f665d4ba5ca67be30bacc6b0d8ec4 Mon Sep 17 00:00:00 2001 From: Chidozie Ononiwu Date: Fri, 18 Sep 2020 12:57:00 -0700 Subject: [PATCH 3/6] remove label functionality --- eng/common/scripts/get-codeowners.ps1 | 46 ++++++++++----------------- 1 file changed, 17 insertions(+), 29 deletions(-) diff --git a/eng/common/scripts/get-codeowners.ps1 b/eng/common/scripts/get-codeowners.ps1 index 9396cc4213f..801330213d4 100644 --- a/eng/common/scripts/get-codeowners.ps1 +++ b/eng/common/scripts/get-codeowners.ps1 @@ -43,36 +43,26 @@ foreach ($contentLine in $codeOwnersContent) { # CODEOWNERS file can also have labels present after the owner aliases # gh aliases start with @ in codeowners. don't pass on to API calls - - $aliases = ($splitLine[1..$($splitLine.Length)] | ? { $_.StartsWith("@") } | % { return $_.substring(1) }) -join "," - $labels = "" - - if ($null -ne $previousLine -and $previousLine.Contains("PRLabel:")) - { - $previousLine = $previousLine.substring($previousLine.IndexOf(':') + 1) - $splitPrevLine = $previousLine -split "%" - $labels = ($splitPrevLine[1..$($splitPrevLine.Length)] | % { return $_.Trim() }) -join "," - } - - $ownedFolders[$splitLine[0].ToLower().Trim("/")] = @{ Aliases = $aliases; Labels = $labels } + $ownedFolders[$splitLine[0].ToLower().Trim("/")] = ($splitLine[1..$($splitLine.Length)] ` + | ? { $_.StartsWith("@") } ` + | % { return $_.substring(1) }) -join "," } - $previousLine = $contentLine } $results = $ownedFolders[$target] if ($results) { Write-Host "Found a folder to match $target" - $aliases = $results.Aliases -split "," - $users - $teams + $aliases = $results -split "," + $users = [System.Collections.ArrayList]::new() + $teams = [System.Collections.ArrayList]::new() foreach ($str in $aliases) { $usersApiUrl = "https://api.github.com/users/$str" if (VerifyAlias -APIUrl $usersApiUrl) { - if ($users) { $users += ("," + $str) } else { $users += $str } + [void]$users.Add($str) } else { if ($str.IndexOf('/') -ne -1) # Check if it's a team alias e.g. Azure/azure-sdk-eng @@ -82,7 +72,7 @@ if ($results) { $teamApiUrl = "https://api.github.com/orgs/$org/teams/$team_slug" if (VerifyAlias -APIUrl $teamApiUrl) { - if ($teams) { $teams += ("," + $str) } else { $teams += $str } + [void]$teams.Add($team_slug) continue } } @@ -90,10 +80,16 @@ if ($results) { } } + $usersString = $users -join ", " + $teamsString = $teams -join ", " + + Write-Host "User $usersString" + Write-Host "Teams $teamsString" + if ($VsoOwningUsers) { $presentOwningUsers = [System.Environment]::GetEnvironmentVariable($VsoOwningUsers) if ($presentOwningUsers) { - $users += ",$presentOwningUsers" + $usersString += ",$presentOwningUsers" } Write-Host "##vso[task.setvariable variable=$VsoOwningUsers;]$users" } @@ -101,20 +97,12 @@ if ($results) { if ($VsoOwningTeams) { $presentOwningTeams = [System.Environment]::GetEnvironmentVariable($VsoOwningTeams) if ($presentOwningTeams) { - $teams += ",$presentOwningTeams" + $teamsString += ",$presentOwningTeams" } Write-Host "##vso[task.setvariable variable=$VsoOwningTeams;]$teams" } - if ($VsoOwningLabels) { - $presentOwningLabels = [System.Environment]::GetEnvironmentVariable($VsoOwningLabels) - if ($presentOwningLabels) { - $labels += ",$presentOwningLabels" - } - Write-Host "##vso[task.setvariable variable=$VsoOwningLabels;]$($result.Labels)" - } - - return @{ Users = $users; Teams = $teams; Labels = $results.Labels } + return $results } else { Write-Host "Unable to match path $target in CODEOWNERS file located at $codeOwnersLocation." From 853499f3ff55eb16238ce69378ff677256ba4860 Mon Sep 17 00:00:00 2001 From: Chidozie Ononiwu Date: Mon, 21 Sep 2020 13:56:00 -0700 Subject: [PATCH 4/6] Re-Add Labels extraction functionality to eng\common\scripts\get-codeowners.ps1 --- .../templates/steps/get-pr-owners.yml | 6 ++- eng/common/scripts/get-codeowners.ps1 | 40 +++++++++++++------ 2 files changed, 32 insertions(+), 14 deletions(-) diff --git a/eng/common/pipelines/templates/steps/get-pr-owners.yml b/eng/common/pipelines/templates/steps/get-pr-owners.yml index 545143ce09b..41b92bc06f2 100644 --- a/eng/common/pipelines/templates/steps/get-pr-owners.yml +++ b/eng/common/pipelines/templates/steps/get-pr-owners.yml @@ -1,5 +1,6 @@ parameters: - TargetVariable: '' + TargetUserVariable: '' + TargetTeamVariable: '' ServiceDirectory: '' steps: @@ -42,4 +43,5 @@ steps: arguments: > -TargetDirectory "/sdk/${{ parameters.ServiceDirectory }}/" -RootDirectory "$(Build.SourcesDirectory)" - -VsoVariable "${{ parameters.TargetVariable }}" \ No newline at end of file + -VsoOwningUsers = "${{ parameters.TargetUserVariable }}" + -VsoOwningTeams = "${{ parameters.TargetTeamVariable }}" \ No newline at end of file diff --git a/eng/common/scripts/get-codeowners.ps1 b/eng/common/scripts/get-codeowners.ps1 index 801330213d4..422c8094e4d 100644 --- a/eng/common/scripts/get-codeowners.ps1 +++ b/eng/common/scripts/get-codeowners.ps1 @@ -43,17 +43,27 @@ foreach ($contentLine in $codeOwnersContent) { # CODEOWNERS file can also have labels present after the owner aliases # gh aliases start with @ in codeowners. don't pass on to API calls - $ownedFolders[$splitLine[0].ToLower().Trim("/")] = ($splitLine[1..$($splitLine.Length)] ` - | ? { $_.StartsWith("@") } ` - | % { return $_.substring(1) }) -join "," + + $aliases = ($splitLine[1..$($splitLine.Length)] | ? { $_.StartsWith("@") } | % { return $_.substring(1) }) -join "," + $labels = "" + + if ($null -ne $previousLine -and $previousLine.Contains("PRLabel:")) + { + $previousLine = $previousLine.substring($previousLine.IndexOf(':') + 1) + $splitPrevLine = $previousLine -split "%" + $labels = ($splitPrevLine[1..$($splitPrevLine.Length)] | % { return $_.Trim() }) -join "," + } + + $ownedFolders[$splitLine[0].ToLower().Trim("/")] = @{ Aliases = $aliases; Labels = $labels } } + $previousLine = $contentLine } $results = $ownedFolders[$target] if ($results) { Write-Host "Found a folder to match $target" - $aliases = $results -split "," + $aliases = $results.Aliases -split "," $users = [System.Collections.ArrayList]::new() $teams = [System.Collections.ArrayList]::new() @@ -81,25 +91,31 @@ if ($results) { } $usersString = $users -join ", " - $teamsString = $teams -join ", " - - Write-Host "User $usersString" - Write-Host "Teams $teamsString" + $teamsString = $teams -join ", " + $labelsString = $results.Labels if ($VsoOwningUsers) { $presentOwningUsers = [System.Environment]::GetEnvironmentVariable($VsoOwningUsers) if ($presentOwningUsers) { - $usersString += ",$presentOwningUsers" + if ($usersString) { $usersString += ",$presentOwningUsers" } else { $usersString = "$presentOwningUsers" } } - Write-Host "##vso[task.setvariable variable=$VsoOwningUsers;]$users" + Write-Host "##vso[task.setvariable variable=$VsoOwningUsers;]$usersString" } if ($VsoOwningTeams) { $presentOwningTeams = [System.Environment]::GetEnvironmentVariable($VsoOwningTeams) if ($presentOwningTeams) { - $teamsString += ",$presentOwningTeams" + if ($teamsString) { $teamsString += ",$presentOwningTeams" } else { $teamsString = "$presentOwningTeams" } + } + Write-Host "##vso[task.setvariable variable=$VsoOwningTeams;]$teamsString" + } + + if ($VsoOwningLabels) { + $presentOwningLabels = [System.Environment]::GetEnvironmentVariable($VsoOwningLabels) + if ($presentOwningLabels) { + if ($labelsString) { $labelsString += ",$presentOwningLabels" } else { $teamsString = "$presentOwningLabels" } } - Write-Host "##vso[task.setvariable variable=$VsoOwningTeams;]$teams" + Write-Host "##vso[task.setvariable variable=$VsoOwningLabels;]$labelsString" } return $results From 9d90efd963225d2084627c134e69b91ebbd9301f Mon Sep 17 00:00:00 2001 From: Chidozie Ononiwu Date: Mon, 21 Sep 2020 14:23:04 -0700 Subject: [PATCH 5/6] Update eng\common\pipelines\templates\steps\get-pr-owners.yml --- .../templates/steps/get-pr-owners.yml | 7 +-- eng/common/scripts/get-codeowners.ps1 | 44 +++++++++---------- 2 files changed, 26 insertions(+), 25 deletions(-) diff --git a/eng/common/pipelines/templates/steps/get-pr-owners.yml b/eng/common/pipelines/templates/steps/get-pr-owners.yml index 41b92bc06f2..48a8b4815b4 100644 --- a/eng/common/pipelines/templates/steps/get-pr-owners.yml +++ b/eng/common/pipelines/templates/steps/get-pr-owners.yml @@ -1,4 +1,5 @@ -parameters: +parameters: + TargetVariable: '' TargetUserVariable: '' TargetTeamVariable: '' ServiceDirectory: '' @@ -19,7 +20,7 @@ steps: --kusto-database-var KUSTO_DB ` --kusto-table-var KUSTO_TABLE ` --identity "$(Build.QueuedBy)" ` - --targetvar "${{ parameters.TargetVariable }}" + --targetvar "${{ coalesce(parameters.TargetVariable, parameters.TargetUserVariable) }}" displayName: 'Resolving Queuing User' continueOnError: true workingDirectory: $(Build.SourcesDirectory)/tools_repo/tools/notification-configuration/identity-resolver @@ -43,5 +44,5 @@ steps: arguments: > -TargetDirectory "/sdk/${{ parameters.ServiceDirectory }}/" -RootDirectory "$(Build.SourcesDirectory)" - -VsoOwningUsers = "${{ parameters.TargetUserVariable }}" + -VsoOwningUsers = "${{ coalesce(parameters.TargetVariable, parameters.TargetUserVariable) }}" -VsoOwningTeams = "${{ parameters.TargetTeamVariable }}" \ No newline at end of file diff --git a/eng/common/scripts/get-codeowners.ps1 b/eng/common/scripts/get-codeowners.ps1 index 422c8094e4d..d5f3b7856bb 100644 --- a/eng/common/scripts/get-codeowners.ps1 +++ b/eng/common/scripts/get-codeowners.ps1 @@ -64,55 +64,55 @@ $results = $ownedFolders[$target] if ($results) { Write-Host "Found a folder to match $target" $aliases = $results.Aliases -split "," - $users = [System.Collections.ArrayList]::new() - $teams = [System.Collections.ArrayList]::new() + $users = @() + $teams = @() foreach ($str in $aliases) { - $usersApiUrl = "https://api.github.com/users/$str" - if (VerifyAlias -APIUrl $usersApiUrl) + if ($str.IndexOf('/') -ne -1) # Check if it's a team alias e.g. Azure/azure-sdk-eng { - [void]$users.Add($str) + $org = $str.substring(0, $str.IndexOf('/')) + $team_slug = $str.substring($str.IndexOf('/') + 1) + $teamApiUrl = "https://api.github.com/orgs/$org/teams/$team_slug" + if (VerifyAlias -APIUrl $teamApiUrl) + { + $teams += $team_slug + continue + } } - else { - if ($str.IndexOf('/') -ne -1) # Check if it's a team alias e.g. Azure/azure-sdk-eng + else + { + $usersApiUrl = "https://api.github.com/users/$str" + if (VerifyAlias -APIUrl $usersApiUrl) { - $org = $str.substring(0, $str.IndexOf('/')) - $team_slug = $str.substring($str.IndexOf('/') + 1) - $teamApiUrl = "https://api.github.com/orgs/$org/teams/$team_slug" - if (VerifyAlias -APIUrl $teamApiUrl) - { - [void]$teams.Add($team_slug) - continue - } + $users += $str + continue } Write-Host "Alias ${str} is neither a recognized github user nor a team" } } - $usersString = $users -join ", " - $teamsString = $teams -join ", " $labelsString = $results.Labels if ($VsoOwningUsers) { $presentOwningUsers = [System.Environment]::GetEnvironmentVariable($VsoOwningUsers) if ($presentOwningUsers) { - if ($usersString) { $usersString += ",$presentOwningUsers" } else { $usersString = "$presentOwningUsers" } + $user += $presentOwningUsers } - Write-Host "##vso[task.setvariable variable=$VsoOwningUsers;]$usersString" + Write-Host "##vso[task.setvariable variable=$VsoOwningUsers;]($user -join ',')" } if ($VsoOwningTeams) { $presentOwningTeams = [System.Environment]::GetEnvironmentVariable($VsoOwningTeams) if ($presentOwningTeams) { - if ($teamsString) { $teamsString += ",$presentOwningTeams" } else { $teamsString = "$presentOwningTeams" } + $teams += $presentOwningUsers } - Write-Host "##vso[task.setvariable variable=$VsoOwningTeams;]$teamsString" + Write-Host "##vso[task.setvariable variable=$VsoOwningTeams;]($teams -join ',')" } if ($VsoOwningLabels) { $presentOwningLabels = [System.Environment]::GetEnvironmentVariable($VsoOwningLabels) - if ($presentOwningLabels) { + if ($presentOwningLabels) { if ($labelsString) { $labelsString += ",$presentOwningLabels" } else { $teamsString = "$presentOwningLabels" } } Write-Host "##vso[task.setvariable variable=$VsoOwningLabels;]$labelsString" From a587e763ce01216e23caf6ae2e6cc8715902c687 Mon Sep 17 00:00:00 2001 From: Chidozie Ononiwu Date: Wed, 23 Sep 2020 20:33:55 -0700 Subject: [PATCH 6/6] Use logging from eng\common\scripts\logging.ps1 --- eng/common/scripts/get-codeowners.ps1 | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/eng/common/scripts/get-codeowners.ps1 b/eng/common/scripts/get-codeowners.ps1 index d5f3b7856bb..327a3b1e205 100644 --- a/eng/common/scripts/get-codeowners.ps1 +++ b/eng/common/scripts/get-codeowners.ps1 @@ -6,6 +6,9 @@ param ( $VsoOwningTeams = "", $VsoOwningLabels = "" ) + +. (Join-Path ${PSScriptRoot} logging.ps1) + $target = $TargetDirectory.ToLower().Trim("/") $codeOwnersLocation = Join-Path $RootDirectory -ChildPath ".github/CODEOWNERS" $ownedFolders = @{} @@ -45,13 +48,13 @@ foreach ($contentLine in $codeOwnersContent) { # gh aliases start with @ in codeowners. don't pass on to API calls $aliases = ($splitLine[1..$($splitLine.Length)] | ? { $_.StartsWith("@") } | % { return $_.substring(1) }) -join "," - $labels = "" + $labels = @() if ($null -ne $previousLine -and $previousLine.Contains("PRLabel:")) { $previousLine = $previousLine.substring($previousLine.IndexOf(':') + 1) $splitPrevLine = $previousLine -split "%" - $labels = ($splitPrevLine[1..$($splitPrevLine.Length)] | % { return $_.Trim() }) -join "," + $labels = ($splitPrevLine[1..$($splitPrevLine.Length)] | % { return $_.Trim() }) } $ownedFolders[$splitLine[0].ToLower().Trim("/")] = @{ Aliases = $aliases; Labels = $labels } @@ -88,18 +91,18 @@ if ($results) { $users += $str continue } - Write-Host "Alias ${str} is neither a recognized github user nor a team" + LogWarning "Alias ${str} is neither a recognized github user nor a team" } } - $labelsString = $results.Labels + $labels = $results.Labels if ($VsoOwningUsers) { $presentOwningUsers = [System.Environment]::GetEnvironmentVariable($VsoOwningUsers) if ($presentOwningUsers) { - $user += $presentOwningUsers + $users += $presentOwningUsers } - Write-Host "##vso[task.setvariable variable=$VsoOwningUsers;]($user -join ',')" + Write-Host "##vso[task.setvariable variable=$VsoOwningUsers;]{0}" -F ($users -join ',') } if ($VsoOwningTeams) { @@ -107,15 +110,15 @@ if ($results) { if ($presentOwningTeams) { $teams += $presentOwningUsers } - Write-Host "##vso[task.setvariable variable=$VsoOwningTeams;]($teams -join ',')" + Write-Host "##vso[task.setvariable variable=$VsoOwningTeams;]{0}" -F ($teams -join ',') } if ($VsoOwningLabels) { $presentOwningLabels = [System.Environment]::GetEnvironmentVariable($VsoOwningLabels) if ($presentOwningLabels) { - if ($labelsString) { $labelsString += ",$presentOwningLabels" } else { $teamsString = "$presentOwningLabels" } + $labels += $presentOwningLabels } - Write-Host "##vso[task.setvariable variable=$VsoOwningLabels;]$labelsString" + Write-Host "##vso[task.setvariable variable=$VsoOwningLabels;]{0}" -F ($labels -join ',') } return $results