Skip to content

Commit 0b11070

Browse files
authored
Merge pull request #13911 from MicrosoftDocs/workflows-test
Rework reviewer assignment to use github teams
2 parents 7d17dc5 + a539d4e commit 0b11070

File tree

1 file changed

+90
-89
lines changed

1 file changed

+90
-89
lines changed

.github/workflows/Shared-AutoLabelAssign.yml

Lines changed: 90 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,20 @@ jobs:
5151
- name: Script
5252
shell: pwsh
5353
env:
54+
GlobalExcludedReviewerList: '[
55+
"dstrome",
56+
"sc-dstrome",
57+
"garycentric",
58+
"garymoore-sc",
59+
"serdarsoysal",
60+
"cx-ktsuji",
61+
"dawntanner",
62+
"pamgreen-msft",
63+
"pebaum",
64+
"SerdarSoysal",
65+
"vinaypamnani-msft"
66+
]'
67+
5468
PayloadJson: ${{ inputs.PayloadJson }}
5569
AccessToken: ${{ secrets.AccessToken }}
5670
AppGitHubAccessToken: ${{ steps.app-token.outputs.token }}
@@ -59,6 +73,7 @@ jobs:
5973
AutoLabel: ${{ inputs.AutoLabel }}
6074
ExcludedUserList: ${{ inputs.ExcludedUserList }}
6175
ExcludedBranchList: ${{ inputs.ExcludedBranchList }}
76+
6277
run: |
6378
# Get runspace info
6479
$RepoRoot = $env:RUNNER_WORKSPACE
@@ -68,6 +83,7 @@ jobs:
6883
$WorkflowRunAttempt = $env:GITHUB_RUN_ATTEMPT
6984
$GitHubData = $env:PayloadJson | ConvertFrom-Json -Depth 50
7085
$AccessToken = $env:AccessToken
86+
$GlobalExcludedReviewerList = $env:GlobalExcludedReviewerList | ConvertFrom-Json
7187
$GitHubApiUrl = "https://api.github.com/repos/MicrosoftDocs/"
7288
7389
$GitRequestEvent = $GitHubData.event_name
@@ -144,19 +160,16 @@ jobs:
144160
TimeStamp = (Get-Date).ToString("o")
145161
}
146162
147-
# Path to GE Taxonomy service/subservice to Content Lead GitHub account mapping file.
148-
$ServiceToGitHubUserMapRepo = "officedocs-pr"
149-
$ServiceToGitHubUserMapFilePath = "/contents/.github/workflows/resources/service-subservice-to-github-user-map.csv"
150-
$ServiceToGitHubUserMapRef = "main"
151-
$ServiceToGitHubUserMapUrl = $GitHubApiUrl + $ServiceToGitHubUserMapRepo + $ServiceToGitHubUserMapFilePath + "?ref=$ServiceToGitHubUserMapRef"
163+
# Path to GE Taxonomy service/subservice to Content Lead GitHub team mapping file.
164+
$ServiceToGitHubTeamMapRepo = "officedocs-pr"
165+
$ServiceToGitHubTeamMapFilePath = "/contents/.github/workflows/resources/service-subservice-to-github-team-map.csv"
166+
$ServiceToGitHubTeamMapRef = "main"
167+
$ServiceToGitHubTeamMapUrl = $GitHubApiUrl + $ServiceToGitHubTeamMapRepo + $ServiceToGitHubTeamMapFilePath + "?ref=$ServiceToGitHubTeamMapRef"
152168
153169
# Path to central workflow resources location.
154170
$WorkflowsResourcePath = "https://api.github.com/repos/MicrosoftDocs/microsoft-365-docs/contents/.github/workflows/resources"
155171
$WorkflowsRef = "workflows-prod"
156172
157-
# URL of the org team to check. We're using the "everyone" team in MicrosoftDocs.
158-
$OrgTeamUrl = "https://api.github.com/orgs/microsoftdocs/teams/everyone/memberships/"
159-
160173
# Set repo variables
161174
$RepoUrl = $GitHubData.event.repository.url
162175
@@ -614,20 +627,20 @@ jobs:
614627
615628
#####################
616629
#####################
617-
# Get-ServiceGitHubAccountMappingTable
630+
# Get-ServiceGitHubTeamMappingTable
618631
619-
Function Get-ServiceGitHubAccountMappingTable {
632+
Function Get-ServiceGitHubTeamMappingTable {
620633
621634
Try {
622635
623-
Write-Host "Getting service/subservice to GitHub user mapping from $ServiceToGitHubUserMapUrl"
636+
Write-Host "Getting service/subservice to GitHub team mapping from $ServiceToGitHubTeamMapUrl"
624637
625-
$GitHubMappingTableData = Invoke-RestMethod -Uri $ServiceToGitHubUserMapUrl -Headers $AppGitHubAccessHeaders
638+
$GitHubMappingTableData = Invoke-RestMethod -Uri $ServiceToGitHubTeamMapUrl -Headers $AppGitHubAccessHeaders
626639
$GitHubMappingTable = [System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String($GitHubMappingTableData.content));
627640
628641
} Catch {
629642
630-
Write-Host "Failed to get service/subservice to GitHub user mapping from $ServiceToGitHubUserMapUrl"
643+
Write-Host "Failed to get service/subservice to GitHub user mapping from $ServiceToGitHubTeamMapUrl"
631644
632645
$GitHubMappingTable = $Null
633646
@@ -670,7 +683,7 @@ jobs:
670683
Product = $Row.Product
671684
Service = $Service
672685
SubService = $SubService
673-
ContentLeadGitHubAccounts = $Row.ContentLeadGitHubAccounts
686+
GitHubTeam = $Row.GitHubTeam
674687
}
675688
$ExpandedRows += $NewRow
676689
}
@@ -682,9 +695,9 @@ jobs:
682695
683696
#####################
684697
#####################
685-
# Get-ContentLeadAccounts
698+
# Get-GitHubReviewerTeams
686699
687-
Function Get-ContentLeadAccounts {
700+
Function Get-GitHubReviewerTeams {
688701
[CmdletBinding()]
689702
Param(
690703
[Parameter(Mandatory = $true)]
@@ -698,7 +711,7 @@ jobs:
698711
)
699712
700713
701-
$AccountsList = @()
714+
$GitHubTeam = @()
702715
703716
Write-Host "Article service: $Service"
704717
Write-Host "Article subservice: $SubService"
@@ -726,7 +739,7 @@ jobs:
726739
Write-Host "SubService found"
727740
728741
# Found exact match with SubService
729-
$AccountsList += $ExactMatch.contentleadgithubaccounts
742+
$GitHubTeam += $ExactMatch.GitHubTeam
730743
}
731744
Else {
732745
@@ -740,10 +753,10 @@ jobs:
740753
741754
If ($HasSubServices) {
742755
743-
Write-Host "Service has subservices. Returning all accounts for all subservices."
756+
Write-Host "Service has subservices. Returning all teams for all subservices."
744757
745-
# Multiple subservices exist, return all accounts for this service's subservices
746-
$AccountsList += $HasSubServices.contentleadgithubaccounts
758+
# Multiple subservices exist, return all teams for this service's subservices
759+
$GitHubTeam += $HasSubServices.GitHubTeam
747760
}
748761
}
749762
}
@@ -758,7 +771,7 @@ jobs:
758771
Write-Host "Service with no subservice row found"
759772
760773
# Found exact match without SubService
761-
$AccountsList += $ExactMatch.contentleadgithubaccounts
774+
$GitHubTeam += $ExactMatch.GitHubTeam
762775
}
763776
Else {
764777
@@ -772,37 +785,37 @@ jobs:
772785
773786
If ($HasSubServices.Count -gt 1) {
774787
775-
Write-Host "Service has subservices. Returning all accounts for all subservices."
788+
Write-Host "Service has subservices. Returning all teams for all subservices."
776789
777-
# Multiple subservices exist, return all accounts
778-
$AccountsList += $HasSubServices.contentleadgithubaccounts
790+
# Multiple subservices exist, return all teams
791+
$GitHubTeam += $HasSubServices.GitHubTeam
779792
}
780793
}
781794
}
782795
}
783796
784-
Write-Host "Found accounts"
785-
$AccountsList | ForEach-Object { Write-Host $_ }
797+
Write-Host "Found teams"
798+
$GitHubTeam | ForEach-Object { Write-Host $_ }
786799
787-
# Process the accounts list
788-
$AllAccounts = @()
800+
# Process the teams list
801+
$AllTeams = @()
789802

790-
ForEach ($AccountString In $AccountsList) {
791-
If (![string]::IsNullOrWhiteSpace($AccountString)) {
803+
ForEach ($TeamString In $GitHubTeam) {
804+
If (![string]::IsNullOrWhiteSpace($TeamString)) {
792805
# Remove leading semicolon if present
793-
$CleanedString = $AccountString.TrimStart(';')
806+
$CleanedString = $TeamString.TrimStart(';')
794807

795808
# Split by semicolon and add to array
796-
$Accounts = $CleanedString -split ';' | Where-Object {
809+
$Teams = $CleanedString -split ';' | Where-Object {
797810
![string]::IsNullOrWhiteSpace($_)
798811
}
799812

800-
$AllAccounts += $Accounts
813+
$AllTeams += $Teams
801814
}
802815
}
803816

804817
# Return deduplicated array
805-
Return $AllAccounts | Select-Object -Unique
818+
Return $AllTeams | Select-Object -Unique
806819

807820
}
808821

@@ -902,58 +915,53 @@ jobs:
902915

903916
#####################
904917
#####################
905-
# Test-OrgMembership
918+
# Get-TeamMembership
906919

907-
Function Test-OrgMembership {
920+
Function Get-TeamMembership {
908921

909922
[CmdletBinding()]
910923
Param(
911924
[Parameter(Mandatory = $true)]
912-
[string[]]$GitHubReviewers
925+
[string[]]$ReviewerTeams
913926
)
914927

915-
$ReviewerHashTable = @{}
928+
$ReviewerList = @()
916929

917-
ForEach ($Reviewer in $GitHubReviewers) {
918-
919-
Write-Host "Checking to see if $Reviewer is a member of the MicrosoftDocs `"everyone`" team."
920-
921-
# Create the member URL to check
922-
$ReviewerTeamUrl = $OrgTeamUrl + $Reviewer
930+
ForEach ($Team in $ReviewerTeams) {
931+
932+
$ReviewerTeamUrl = "https://api.github.com/orgs/MicrosoftDocs/teams/$Team/members?per_page=100"
923933

924934
Try {
925935

926-
# Check to see if the reviewer is a member of the MicrosoftDocs "everyone" team
927-
$TeamResult = Invoke-RestMethod -Uri $ReviewerTeamUrl -Headers $AppGitHubAccessHeaders -ErrorAction Stop
928-
929-
If ($TeamResult.state -eq "active") {
936+
Write-Host "Getting membership information for $Team."
930937

931-
Write-Host "$Reviewer is an active member of the MicrosoftDocs `"everyone`" team."
938+
$TeamResult = Invoke-RestMethod -Uri $ReviewerTeamUrl -Headers $AppGitHubAccessHeaders -FollowRelLink -ErrorAction Stop
932939

933-
$ReviewerHashTable.Add($Reviewer, $True)
934-
935-
} Else {
940+
ForEach ($Page in $TeamResult) {
936941

937-
Write-Host "$Reviewer isn't an active member of the MicrosoftDocs `"everyone`" team."
938-
939-
$ReviewerHashTable.Add($Reviewer, $False)
940-
942+
ForEach ($Member in $Page) {
943+
944+
If ($GlobalExcludedReviewerList -notcontains $Member.login) {
945+
946+
$ReviewerList += $Member.login
947+
948+
}
949+
950+
}
941951

942952
}
943-
953+
944954
} Catch {
945-
946-
Write-Host "ERROR: Failed to look up membership info for $Reviewer. Error: $_"
955+
956+
Write-Host "ERROR: Failed to look up membership info for $Team. Error: $_"
947957

948-
# If the call fails, set to false to avoid breaking add reviewer call later in workflow.
949-
$ReviewerHashTable.Add($Reviewer, $False)
950-
951958
}
952-
959+
953960
}
961+
962+
Write-Host "Reviewers found: $($ReviewerList | Sort-Object -Unique)"
954963

955-
Return $ReviewerHashTable
956-
964+
Return $ReviewerList | Sort-Object -Unique
957965

958966
}
959967

@@ -1164,25 +1172,25 @@ jobs:
11641172
If (($UserPermission -eq "read") -or ($UserPermission -eq "") -or ($UserPermission -eq $Null)) {
11651173

11661174
# Get the service/subservice to GitHub account map
1167-
$ServiceToAccountMap = Get-ServiceGitHubAccountMappingTable
1168-
$ExpandedServiceSubServiceRows = Expand-ServiceSubServiceRows -CsvContent $ServiceToAccountMap
1175+
$ServiceToTeamMap = Get-ServiceGitHubTeamMappingTable
1176+
$ExpandedServiceSubServiceRows = Expand-ServiceSubServiceRows -CsvContent $ServiceToTeamMap
11691177

1170-
$ReviewerArray = @()
1178+
$ReviewerTeamArray = @()
11711179
$ServiceSubServiceArray = @() # Used in logging
11721180

11731181
ForEach ($File in $FileMetadataArray) {
11741182

11751183
If ($File.Service -ne $Null) {
11761184

1177-
$FileReviewers = $Null
1185+
$ReviewerTeams = $Null
11781186

1179-
$FileReviewers = Get-ContentLeadAccounts -DataArray $ExpandedServiceSubServiceRows -Service $File.Service -SubService $File.SubService
1187+
$ReviewerTeams = Get-GitHubReviewerTeams -DataArray $ExpandedServiceSubServiceRows -Service $File.Service -SubService $File.SubService
11801188

11811189
$ServiceSubServiceArray += "$($File.Service)\$($File.Subservice)"
11821190

1183-
If ($FileReviewers -ne $Null) {
1191+
If ($ReviewerTeams -ne $Null) {
11841192

1185-
$ReviewerArray += $FileReviewers
1193+
$ReviewerTeamArray += $ReviewerTeams
11861194

11871195
}
11881196

@@ -1194,20 +1202,17 @@ jobs:
11941202

11951203
}
11961204

1197-
$ReviewerArray = $ReviewerArray | Select-Object -Unique
1205+
$ReviewerTeamArray = $ReviewerTeamArray | Select-Object -Unique
11981206

1199-
If ($ReviewerArray.Count -gt 0) {
1207+
If ($ReviewerTeamArray.Count -gt 0) {
12001208

1201-
Write-Host "Checking org membership."
1209+
$ReviewerAccounts = Get-TeamMembership -ReviewerTeams $ReviewerTeamArray
12021210

1203-
$TestedReviewerAccounts = Test-OrgMembership -GitHubReviewers $ReviewerArray
1204-
$ValidatedReviewerAccounts = $TestedReviewerAccounts.keys | Where-Object { $TestedReviewerAccounts[$_] }
1211+
$AtMentionedGitHubAccounts = Add-AtPrefix -GitHubReviewers $ReviewerAccounts
12051212

1206-
$AtMentionedGitHubAccounts = Add-AtPrefix -GitHubReviewers $ValidatedReviewerAccounts
1213+
If ($ReviewerAccounts.Length -gt 0) {
12071214

1208-
If ($ValidatedReviewerAccounts.Length -gt 0) {
1209-
1210-
$MissingReviewers = Compare-PRIndividualReviewers -GitHubReviewers $ValidatedReviewerAccounts
1215+
$MissingReviewers = Compare-PRIndividualReviewers -GitHubReviewers $ReviewerAccounts
12111216

12121217
If ($MissingReviewers) {
12131218

@@ -1217,7 +1222,7 @@ jobs:
12171222

12181223
Try {
12191224

1220-
Set-PrReviewer -PrUrl $PrUrl -GitHubReviewers $ValidatedReviewerAccounts
1225+
Set-PrReviewer -PrUrl $PrUrl -GitHubReviewers $ReviewerAccounts
12211226
Set-PrConversationMessage -Message $ReviewerMessage
12221227

12231228
$LoggingData.ReviewStatus = "ReviewerAddSuccess"
@@ -1278,8 +1283,8 @@ jobs:
12781283
}
12791284

12801285
$LoggingData.ServiceSubService = $($ServiceSubServiceArray | Select-Object -Unique) -Join ";"
1281-
$LoggingData.InitialReviewerList = $ReviewerArray -Join ";"
1282-
$Loggingdata.ValidatedReviewerList = $ValidatedReviewerAccounts -Join ";"
1286+
$LoggingData.InitialReviewerList = $ReviewerTeamArray -Join ";"
1287+
$Loggingdata.ValidatedReviewerList = $ReviewerAccounts -Join ";"
12831288

12841289
Send-WorkflowLog -LogData $LoggingData
12851290

@@ -1304,8 +1309,6 @@ jobs:
13041309

13051310
}
13061311

1307-
1308-
13091312
} Else {
13101313

13111314
Write-Host "Target branch $TargetBranch is an excluded branch. Not adding author assignments."
@@ -1334,6 +1337,4 @@ jobs:
13341337

13351338
Write-Host "Event action not ready_for_review, opened, reopened, or synchronize."
13361339

1337-
1338-
13391340
} # PR event and action check

0 commit comments

Comments
 (0)