-
Notifications
You must be signed in to change notification settings - Fork 229
Update get-codeowners PowerShell file #981
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
d4f6b4b
Update get-codeowners powershell file
chidozieononiwu dfc66bc
Update alias verification logic
chidozieononiwu 2f1cc3f
remove label functionality
chidozieononiwu 853499f
Re-Add Labels extraction functionality to eng\common\scripts\get-code…
chidozieononiwu 9d90efd
Update eng\common\pipelines\templates\steps\get-pr-owners.yml
chidozieononiwu a587e76
Use logging from eng\common\scripts\logging.ps1
chidozieononiwu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next
Next commit
Update get-codeowners powershell file
- Loading branch information
commit d4f6b4bca001418c86dddee34c602102cf9c1306
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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,34 +16,89 @@ if (!(Test-Path $codeOwnersLocation)) { | |
| } | ||
|
|
||
| $codeOwnersContent = Get-Content $codeOwnersLocation | ||
| $previousLine | ||
chidozieononiwu marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| foreach ($contentLine in $codeOwnersContent) { | ||
| if (-not $contentLine.StartsWith("#") -and $contentLine){ | ||
| $splitLine = $contentLine -split "\s+" | ||
|
|
||
| # 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 "," | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Given that you split on "," below we should just avoid the join here and just keep an array. |
||
| $labels = "" | ||
|
|
||
| if ($null -ne $previousLine -and $previousLine.StartsWith("# PRLabel:")) | ||
chidozieononiwu marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| { | ||
| $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 | ||
chidozieononiwu marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| $teams | ||
|
|
||
| if ($AuthToken) { | ||
| $headers = @{ | ||
| Authorization = "bearer $AuthToken" | ||
chidozieononiwu marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
|
|
||
| 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 } | ||
chidozieononiwu marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| catch { | ||
| if ($_.Exception.Response.StatusCode.Value__ -eq 404) # Consider it a team Alias | ||
chidozieononiwu marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| { | ||
| 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" | ||
chidozieononiwu marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| 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." | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.