-
Notifications
You must be signed in to change notification settings - Fork 773
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[repo] Auto label pull requests (#5681)
- Loading branch information
1 parent
fd18bb1
commit a763c0d
Showing
8 changed files
with
196 additions
and
32 deletions.
There are no files selected for viewing
This file contains 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,25 +1,53 @@ | ||
name: 'Add labels for area found in bug issue descriptions' | ||
name: 'Add labels to issues and pull requests' | ||
on: | ||
issues: | ||
types: [opened] | ||
types: [ opened ] | ||
|
||
pull_request: | ||
branches: [ 'main*' ] | ||
|
||
permissions: | ||
issues: write | ||
pull-requests: write | ||
|
||
jobs: | ||
add-labels: | ||
if: ${{ !github.event.issue.pull_request }} | ||
add-labels-on-issues: | ||
if: github.event_name == 'issues' && !github.event.issue.pull_request | ||
|
||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- name: check out code | ||
uses: actions/checkout@v4 | ||
|
||
- name: Add labels for areas found in bug issue descriptions | ||
- name: Add labels for package found in bug issue descriptions | ||
shell: pwsh | ||
run: | | ||
.\build\scripts\add-labels.ps1 -issueNumber $env:ISSUE_NUMBER -issueBody $env:ISSUE_BODY | ||
Import-Module .\build\scripts\add-labels.psm1 | ||
AddLabelsOnIssuesForPackageFoundInBody ` | ||
-issueNumber ${{ github.event.issue.number }} ` | ||
-issueBody $env:ISSUE_BODY | ||
env: | ||
GH_TOKEN: ${{ github.token }} | ||
ISSUE_NUMBER: ${{ github.event.issue.number }} | ||
ISSUE_BODY: ${{ github.event.issue.body }} | ||
|
||
add-labels-on-pull-requests: | ||
if: github.event_name == 'pull_request' | ||
|
||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- name: check out code | ||
uses: actions/checkout@v4 | ||
|
||
- name: Add labels for files changed on pull requests | ||
shell: pwsh | ||
run: | | ||
Import-Module .\build\scripts\add-labels.psm1 | ||
AddLabelsOnPullRequestsBasedOnFilesChanged ` | ||
-pullRequestNumber ${{ github.event.pull_request.number }} ` | ||
-labelPackagePrefix 'pkg:' | ||
env: | ||
GH_TOKEN: ${{ github.token }} |
This file contains 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
This file contains 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
This file contains 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
This file was deleted.
Oops, something went wrong.
This file contains 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 |
---|---|---|
@@ -0,0 +1,148 @@ | ||
function AddLabelsOnIssuesForPackageFoundInBody { | ||
param( | ||
[Parameter(Mandatory=$true)][int]$issueNumber, | ||
[Parameter(Mandatory=$true)][string]$issueBody | ||
) | ||
|
||
$match = [regex]::Match($issueBody, '^[#]+ Package\s*(OpenTelemetry(?:\.\w+)*)') | ||
if ($match.Success -eq $false) | ||
{ | ||
Return | ||
} | ||
|
||
gh issue edit $issueNumber --add-label $("pkg:" + $match.Groups[1].Value) | ||
} | ||
|
||
Export-ModuleMember -Function AddLabelsOnIssuesForPackageFoundInBody | ||
|
||
function AddLabelsOnPullRequestsBasedOnFilesChanged { | ||
param( | ||
[Parameter(Mandatory=$true)][int]$pullRequestNumber, | ||
[Parameter(Mandatory=$true)][string]$labelPackagePrefix # 'pkg:' on main repo, 'comp:' on contrib repo | ||
) | ||
|
||
# Note: This function is intended to work on main repo and on contrib. Please | ||
# keep them in sync. | ||
|
||
$repoLabels = gh label list --json name,id | ConvertFrom-Json | ||
|
||
$filesChangedOnPullRequest = gh pr diff $pullRequestNumber --name-only | ||
|
||
$labelsOnPullRequest = (gh pr view $pullRequestNumber --json labels | ConvertFrom-Json).labels | ||
|
||
$visitedProjects = New-Object System.Collections.Generic.HashSet[string] | ||
$labelsToAdd = New-Object System.Collections.Generic.HashSet[string] | ||
$labelsToRemove = New-Object System.Collections.Generic.HashSet[string] | ||
|
||
# Note: perf label may be added but it is kind of a guess so we don't remove | ||
# it automatically in order to also allow manual inclusion after reviewing files | ||
$managedLabels = 'infra', 'documentation', 'dependencies' | ||
$rootInfraFiles = 'global.json', 'NuGet.config', 'codeowners' | ||
$documentationFiles = 'readme.md', 'contributing.md', 'releasing.md', 'versioning.md' | ||
|
||
foreach ($fileChanged in $filesChangedOnPullRequest) | ||
{ | ||
$fileChanged = $fileChanged.ToLower() | ||
$fullFileName = [System.IO.Path]::GetFileName($fileChanged) | ||
$fileName = [System.IO.Path]::GetFileNameWithoutExtension($fileChanged) | ||
$fileExtension = [System.IO.Path]::GetExtension($fileChanged) | ||
|
||
if ($fileChanged.StartsWith('src/') -or $fileChanged.StartsWith('test/')) | ||
{ | ||
$match = [regex]::Match($fileChanged, '^(?:(?:src)|(?:test))\/(.*?)\/.+$') | ||
if ($match.Success -eq $false) | ||
{ | ||
continue | ||
} | ||
$rawProjectName = $match.Groups[1].Value | ||
if ($rawProjectName.Contains(".benchmarks") -or $rawProjectName.Contains(".stress")) | ||
{ | ||
$added = $labelsToAdd.Add("perf") | ||
} | ||
|
||
$projectName = $rawProjectName.Replace(".tests", "").Replace(".benchmarks", "").Replace(".stress", "") | ||
if ($visitedProjects.Contains($projectName)) | ||
{ | ||
continue | ||
} | ||
|
||
$added = $visitedProjects.Add($projectName); | ||
|
||
foreach ($repoLabel in $repoLabels) | ||
{ | ||
if ($repoLabel.name.StartsWith($labelPackagePrefix)) | ||
{ | ||
$package = $repoLabel.name.Substring($labelPackagePrefix.Length).ToLower() | ||
if ($package.StartsWith('opentelemetry') -eq $false) | ||
{ | ||
# Note: On contrib labels don't have "OpenTelemetry." prefix | ||
$package = 'opentelemetry.' + $package | ||
} | ||
if ($package -eq $projectName) | ||
{ | ||
$added = $labelsToAdd.Add($repoLabel.name) | ||
break | ||
} | ||
} | ||
} | ||
} | ||
|
||
if ($documentationFiles.Contains($fullFileName) -or | ||
$fileChanged.StartsWith('docs/') -or | ||
$fileChanged.StartsWith('examples/')) | ||
{ | ||
$added = $labelsToAdd.Add("documentation") | ||
} | ||
|
||
if ($fileChanged.StartsWith('build/') -or | ||
$fileChanged.StartsWith('.github/') -or | ||
$rootInfraFiles.Contains($fullFileName) -or | ||
$fileExtension -eq ".props" -or | ||
$fileExtension -eq ".targets" -or | ||
$fileChanged.StartsWith('test\openTelemetry.aotcompatibility')) | ||
{ | ||
$added = $labelsToAdd.Add("infra") | ||
} | ||
|
||
if ($fileChanged.StartsWith('test\benchmarks')) | ||
{ | ||
$added = $labelsToAdd.Add("perf") | ||
} | ||
|
||
if ($fullFileName -eq 'directory.packages.props') | ||
{ | ||
$added = $labelsToAdd.Add("dependencies") | ||
} | ||
} | ||
|
||
foreach ($labelOnPullRequest in $labelsOnPullRequest) | ||
{ | ||
if ($labelsToAdd.Contains($labelOnPullRequest.name)) | ||
{ | ||
$removed = $labelsToAdd.Remove($labelOnPullRequest.name) | ||
} | ||
elseif ($labelOnPullRequest.name.StartsWith($labelPackagePrefix) -or | ||
$managedLabels.Contains($labelOnPullRequest.name)) | ||
{ | ||
$added = $labelsToRemove.Add($labelOnPullRequest.name) | ||
} | ||
} | ||
|
||
if ($labelsToAdd.Count -gt 0) | ||
{ | ||
foreach ($label in $labelsToAdd) | ||
{ | ||
gh pr edit $pullRequestNumber --add-label $label | ||
} | ||
} | ||
|
||
if ($labelsToRemove.Count -gt 0) | ||
{ | ||
foreach ($label in $labelsToRemove) | ||
{ | ||
gh pr edit $pullRequestNumber --remove-label $label | ||
} | ||
} | ||
} | ||
|
||
Export-ModuleMember -Function AddLabelsOnPullRequestsBasedOnFilesChanged |
This file contains 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
This file contains 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