Skip to content

Commit 8f89abd

Browse files
authored
{Misc.} Added changes for internal team developer operations (#33011)
1 parent 7bdba8d commit 8f89abd

File tree

1 file changed

+92
-23
lines changed

1 file changed

+92
-23
lines changed

tools/Github/ParseSquadMappingList.ps1

Lines changed: 92 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,27 @@ function UpdateNode {
193193
}
194194
}
195195

196+
function GetLabelsFromConditions {
197+
[CmdletBinding()]
198+
param([string[]] $Lines, [int] $ThenLineIndex, [int] $BaseIndentLength)
199+
$labels = [System.Collections.Generic.List[string]]::new()
200+
$ifLineIndex = -1
201+
for ($p = $ThenLineIndex - 1; $p -ge 0; $p--) {
202+
$lineAtP = $Lines[$p]
203+
$indentAtP = GetIndentLength -Line $lineAtP
204+
if ($lineAtP -match '^\s*-\s+if:\s*$' -and $indentAtP -le $BaseIndentLength) { $ifLineIndex = $p; break }
205+
if ($indentAtP -lt $BaseIndentLength - 2) { break }
206+
}
207+
if ($ifLineIndex -lt 0) { return $labels }
208+
for ($q = $ifLineIndex + 1; $q -lt $ThenLineIndex; $q++) {
209+
$candidate = TryParseLabelValue -Line $Lines[$q]
210+
if ($null -ne $candidate -and -not [string]::IsNullOrWhiteSpace($candidate)) {
211+
if (-not $labels.Contains($candidate)) { $labels.Add($candidate) }
212+
}
213+
}
214+
return $labels
215+
}
216+
196217
function AddSquadLabelsToYaml {
197218
[CmdletBinding()]
198219
param(
@@ -257,57 +278,105 @@ function AddSquadLabelsToYaml {
257278
}
258279
}
259280

260-
if ($lastAddLabelEnd -ge 0) {
261-
$labelsToAdd = [System.Collections.Generic.List[string]]::new()
262-
foreach ($label in $labelsPresent.Keys) {
263-
if ($LabelToSquad.ContainsKey($label)) {
264-
$squadLabel = $LabelToSquad[$label]
265-
if (-not $labelsPresent.ContainsKey($squadLabel) -and -not $labelsToAdd.Contains($squadLabel)) {
266-
$labelsToAdd.Add($squadLabel)
267-
}
281+
$labelsFromConditions = GetLabelsFromConditions -Lines $Lines -ThenLineIndex $i -BaseIndentLength $baseIndentLength
282+
foreach ($condLabel in $labelsFromConditions) {
283+
if (-not [string]::IsNullOrWhiteSpace($condLabel)) {
284+
$labelsPresent[$condLabel] = $true
285+
}
286+
}
287+
288+
$labelsToAdd = [System.Collections.Generic.List[string]]::new()
289+
$sortedLabels = $labelsPresent.Keys | Sort-Object -CaseSensitive
290+
foreach ($label in $sortedLabels) {
291+
if ($LabelToSquad.ContainsKey($label)) {
292+
$squadLabel = $LabelToSquad[$label]
293+
if (-not $labelsPresent.ContainsKey($squadLabel) -and -not $labelsToAdd.Contains($squadLabel)) {
294+
$labelsToAdd.Add($squadLabel)
268295
}
269296
}
297+
}
270298

271-
if ($labelsToAdd.Count -gt 0) {
299+
if ($labelsToAdd.Count -gt 0) {
300+
if ($lastAddLabelEnd -ge 0) {
272301
$insertLines = [System.Collections.Generic.List[string]]::new()
273302
foreach ($squadLabel in $labelsToAdd) {
274303
$insertLines.Add((" " * $listIndentLength) + "- addLabel:")
275304
$insertLines.Add((" " * $listIndentLength) + " label: $squadLabel")
276305
}
277306
$insertions.Add([PSCustomObject]@{ Index = $lastAddLabelEnd + 1; Lines = $insertLines })
307+
}
278308

279-
$isPR = $false
309+
$isPR = $false
280310
for ($p = $i - 1; $p -ge 0; $p--) {
281311
if ($Lines[$p] -match '^\s*description:') { break }
282312
if ($Lines[$p] -match 'payloadType:\s*Pull_Request') { $isPR = $true; break }
283313
}
284314
if ($isPR) {
285-
$lastUserEnd = -1
286-
$usersIndent = $listIndentLength + 4
287-
$existingUsers = @{}
315+
$lastReviewEnd = -1
316+
$reviewIndent = $listIndentLength + 4
317+
$existingReviewers = @{}
288318
for ($b = $j; $b -lt $k; $b++) {
289-
if ($Lines[$b] -match '^\s*-\s+assignTo:\s*$' -and (GetIndentLength -Line $Lines[$b]) -eq $listIndentLength) {
319+
if ($Lines[$b] -match '^\s*-\s+requestReview:\s*$' -and (GetIndentLength -Line $Lines[$b]) -eq $listIndentLength) {
290320
for ($c = $b + 1; $c -lt $k; $c++) {
291321
if ($Lines[$c] -match '^\s*-\s+' -and (GetIndentLength -Line $Lines[$c]) -eq $listIndentLength) { break }
292-
if ($Lines[$c] -match '^\s*-\s+(\S+)\s*$' -and (GetIndentLength -Line $Lines[$c]) -eq $usersIndent) {
293-
$existingUsers[$Matches[1]] = $true
294-
$lastUserEnd = $c
322+
if ($Lines[$c] -match '^\s*reviewer:\s*(\S+)\s*$') {
323+
$existingReviewers[$Matches[1]] = $true
324+
$lastReviewEnd = $c
295325
}
296326
}
297327
}
298328
}
299-
if ($lastUserEnd -ge 0) {
300-
$userInsertLines = [System.Collections.Generic.List[string]]::new()
329+
if ($lastReviewEnd -lt 0) {
330+
$lastReviewEnd = $lastAddLabelEnd
331+
}
332+
if ($lastReviewEnd -ge 0) {
333+
$reviewInsertLines = [System.Collections.Generic.List[string]]::new()
301334
foreach ($squadLabel in $labelsToAdd) {
302-
if (-not $existingUsers.ContainsKey($squadLabel)) {
303-
$userInsertLines.Add((" " * $usersIndent) + "- $squadLabel")
335+
if (-not $existingReviewers.ContainsKey($squadLabel)) {
336+
$reviewInsertLines.Add((" " * $listIndentLength) + "- requestReview:")
337+
$reviewInsertLines.Add((" " * $reviewIndent) + "reviewer: $squadLabel")
304338
}
305339
}
306-
if ($userInsertLines.Count -gt 0) {
307-
$insertions.Add([PSCustomObject]@{ Index = $lastUserEnd + 1; Lines = $userInsertLines })
340+
if ($reviewInsertLines.Count -gt 0) {
341+
$insertions.Add([PSCustomObject]@{ Index = $lastReviewEnd + 1; Lines = $reviewInsertLines })
308342
}
309343
}
310344
}
345+
346+
$mentionUsersIndex = -1
347+
$mentioneesIndent = -1
348+
$mentionItemIndent = -1
349+
$existingMentions = @{}
350+
$lastMentionEnd = -1
351+
for ($b = $j; $b -lt $k; $b++) {
352+
$lineAtB = $Lines[$b]
353+
$indentAtB = GetIndentLength -Line $lineAtB
354+
if ($lineAtB -match '^\s*-\s+mentionUsers:\s*$' -and $indentAtB -eq $listIndentLength) {
355+
$mentionUsersIndex = $b
356+
continue
357+
}
358+
if ($mentionUsersIndex -ge 0) {
359+
if ($lineAtB.Trim().Length -ne 0 -and $indentAtB -le $listIndentLength -and $lineAtB -match '^\s*-\s+') { break }
360+
if ($lineAtB -match '^\s*mentionees:\s*$') { $mentioneesIndent = $indentAtB; continue }
361+
if ($lineAtB -match '^\s*-\s+(\S+)\s*$') {
362+
if ($mentionItemIndent -lt 0) { $mentionItemIndent = $indentAtB }
363+
$existingMentions[$Matches[1]] = $true
364+
$lastMentionEnd = $b
365+
}
366+
}
367+
}
368+
369+
if ($mentionUsersIndex -ge 0 -and $lastMentionEnd -ge 0) {
370+
if ($mentionItemIndent -lt 0) { $mentionItemIndent = ($mentioneesIndent -gt 0) ? $mentioneesIndent : ($listIndentLength + 4) }
371+
$mentionInsertLines = [System.Collections.Generic.List[string]]::new()
372+
foreach ($squadLabel in $labelsToAdd) {
373+
if (-not $existingMentions.ContainsKey($squadLabel)) {
374+
$mentionInsertLines.Add((" " * $mentionItemIndent) + "- $squadLabel")
375+
}
376+
}
377+
if ($mentionInsertLines.Count -gt 0) {
378+
$insertions.Add([PSCustomObject]@{ Index = $lastMentionEnd + 1; Lines = $mentionInsertLines })
379+
}
311380
}
312381
}
313382
}

0 commit comments

Comments
 (0)