Skip to content
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

Add a github workflow to enforce reviews #23034

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .github/enforce_review_paths.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
src/Compute/Compute
src/Compute/Compute.AlcWrapper
src/Compute/Compute.Autorest
src/Compute/Compute.Helpers
src/Compute/Compute.Management.Sdk
src/Compute/documentation
19 changes: 19 additions & 0 deletions .github/workflows/enforce_required_reviewers.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name: Enforce Required Reviewers

on:
pull_request:
pull_request_review:

jobs:
Check:
runs-on: ubuntu-latest
steps:
- name: Check Reviewers
shell: pwsh
env:
REPOSITORY: ${{github.repository}}
PR_NO: ${{github.event.number || github.event.pull_request.number}}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
Invoke-WebRequest -Uri "https://raw.githubusercontent.com/${env:REPOSITORY}/main/.github/workflows/script/enforce_required_reviewers_check.ps1" -OutFile "./enforce_required_reviewers_check.ps1"
./enforce_required_reviewers_check.ps1
52 changes: 52 additions & 0 deletions .github/workflows/script/enforce_required_reviewers_check.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# set the repository to $env:repository, e.g., $env:repository="Azure/azure-powershell"
# set the PR number to $env:prNo, e.g., $env:prNo=22145
# set the GITHUB_TOKEN to $env:GITHUB_TOKEN if necessay, e.g., $env:GITHUB_TOKEN="******"

$prNo = $env:PR_NO
$repository = $env:REPOSITORY
Invoke-WebRequest -Uri "https://raw.githubusercontent.com/$repository/main/.github/enforce_review_paths.txt" -OutFile "./enforce_review_paths.txt"
$enforceReivewPaths = Get-Content ./enforce_review_paths.txt
if ($enforceReivewPaths.Length -eq 0) {
Write-Host "No enforce review paths are configured. Skip it."
return
}

$updatedFiles = (gh api -H "Accept: application/vnd.github+json" -H "X-GitHub-Api-Version: 2022-11-28" /repos/$repository/pulls/$prNo/files | ConvertFrom-Json).filename
Write-Host "All updated files in this PR $prNo : "
$updatedFiles

$updatedEnforcePaths = @()
foreach ($enforceReivewPath in $enforceReivewPaths) {
$updatedEnforcePaths += $updatedFiles | Where-Object {$_.StartsWith($enforceReivewPath)}
}
$updatedEnforcePaths = $updatedEnforcePaths | Sort-Object -Unique
if ($updatedEnforcePaths.Length -eq 0) {
Write-Host "No enforce review paths are updated in this PR. Skip it."
return
}
Invoke-WebRequest -Uri "https://raw.githubusercontent.com/$repository/main/.github/CODEOWNERS" -OutFile "./CODEOWNERS"
$codeOwnerLines = Get-Content ./CODEOWNERS | Where-Object { ($_.trim().Length -gt 1) -and ($_.trim() -notlike "#*") }
$actualReviewers = (gh api -H "Accept: application/vnd.github+json" -H "X-GitHub-Api-Version: 2022-11-28" /repos/$repository/pulls/$prNo/reviews | ConvertFrom-Json).user.login
$actualReviewers = $actualReviewers ? $actualReviewers : @() # if no reviewers
Write-Host "All reviewers for this PR $prNo : "
$actualReviewers

foreach ($updatedEnforcePath in $updatedEnforcePaths) {
foreach ($codeOwnerLine in $codeOwnerLines) {
$codeOwnerLineArray = ($codeOwnerLine.trim() -split "[ ]+")
if ($codeOwnerLineArray.Length -le 1) {
continue
}
$codeReivewPath = $codeOwnerLineArray[0]
if ($codeReivewPath.StartsWith('/')) {
$codeReivewPath = $codeReivewPath.Substring(1)
}
if ($updatedEnforcePath.StartsWith($codeReivewPath)) {
$codeReivewOwners = $codeOwnerLineArray[1..$codeOwnerLineArray.Length] | ForEach-Object { $_.Substring(1) } # remove '@'
$reviewerOwners = Compare-Object $codeReivewOwners $actualReviewers -PassThru -IncludeEqual -ExcludeDifferent
if ($reviewerOwners.Length -eq 0) {
throw "error: At least one of the required reviewers '$($codeReivewOwners -join "', '")' must approve the PR"
}
}
}
}
Loading