Last active
June 11, 2019 18:15
-
-
Save scbedd/8efb2654c9bdddd93f3cb38297274497 to your computer and use it in GitHub Desktop.
A little script to get active PRs that affect a readme
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
# A little script to get active PRs that affect a readme | |
# | |
# There are limitations here. Note that pulling the files list associated with a PR does have a maximum. I wouldn't trust this scripts | |
# to properly assess PRs that have more than 300 files modified. | |
# | |
# To get a GITHUB Token: https://help.github.com/en/articles/creating-a-personal-access-token-for-the-command-line | |
# | |
# EXAMPLE USAGE (from powershell) | |
# | |
# ./get_prs_with_readme.ps1 -labelFilter Dev -gh_token <YOUR GITHUB PAT> -repoName release-testing -repoOwner scbedd | |
# | |
# Note that this script does return an object, so you can do $results = ./get_prs_with_readme.ps1 | |
# and interact with the results on your own | |
# | |
# or copy this script locally, set the default repo and owner, set and environment variable of name GH_TOKEN with your PAT, and | |
# run it | |
param ( | |
$ghToken = "", | |
$repoOwner = "Azure", # the owning organization of the repository. EG "Azure" | |
$repoName = "azure-sdk-for-js"# the name of the repository. EG "azure-sdk-for-java" | |
) | |
$API_URL = "https://api.github.com/repos/$repoOwner/$repoName" | |
function FireAPIRequest($url, $method, $body = $null, $headers = $null) | |
{ | |
$attempts = 1 | |
while($attempts -le 3) | |
{ | |
try | |
{ | |
return Invoke-RestMethod -Method $method -Uri $url -Body $body -Headers $headers -FollowRelLink | |
} | |
catch | |
{ | |
$response = $_.Exception.Response | |
$statusCode = $response.StatusCode.value__ | |
$statusDescription = $response.StatusDescription | |
Write-Host "API request attempt number $attempts to $url failed with statuscode $statusCode" | |
Write-Host $statusDescription | |
Write-Host "Rate Limit Details:" | |
Write-Host "Total: $($response.Headers.GetValues("X-RateLimit-Limit"))" | |
Write-Host "Remaining: $($response.Headers.GetValues("X-RateLimit-Remaining"))" | |
Write-Host "Reset Epoch: $($response.Headers.GetValues("X-RateLimit-Reset"))" | |
if ($attempts -gt 3) | |
{ | |
Write-Host "Abandoning Request $url after 3 attempts." | |
exit(1) | |
} | |
Start-Sleep -s 10 | |
} | |
$attempts += 1 | |
} | |
} | |
# credit to https://stackoverflow.com/a/33545660 for this beautiful piece of software | |
function Flatten-Array{ | |
$input | ForEach-Object{ | |
if ($_ -is [array]){$_ | Flatten-Array}else{$_} | |
} | Where-Object{![string]::IsNullorEmpty($_)} | |
# | Where-Object{$_} would also work. | |
} | |
function GetPRWithAffectedFiles($pullRequestNumber) | |
{ | |
$prDetailsUrl = "$API_URL/pulls/$pullRequestNumber/files" | |
$pullRequestFiles = FireAPIRequest -method "GET" -url $prDetailsUrl -headers $COMMON_AUTH_HEADER | |
$results = $pullRequestFiles | % { return $_.filename } | ? { return $_.ToLower().Contains("readme") } | |
return New-Object PSObject -Property @{ | |
PRNumber = $pullRequestNumber | |
PRFiles = $results | |
} | |
} | |
function GetActivePRs($pullRequestList) | |
{ | |
$resultArray = @() | |
$resultTotal = 1 | |
$denominator = $pullRequestList.Length | |
# iterate across them. get the file list? | |
foreach($pullRequest in $pullRequestList) | |
{ | |
$percentComplete = (($resultTotal*1.0)/$denominator)*100 | |
$resultArray += (GetPRWithAffectedFiles -pullRequestNumber $pullRequest.number) | |
$resultTotal += 1 | |
Write-Progress -Activity "Extracting PR Files" -Status "$resultTotal / $denominator Complete:" -PercentComplete $percentComplete; | |
} | |
return $resultArray | |
} | |
# fall back to environment variable | |
if($ghToken -eq "") | |
{ | |
$ghToken = $env:GH_TOKEN | |
} | |
$COMMON_AUTH_HEADER = @{ | |
"Content-Type" = "application/json" | |
"Authorization" = "token $ghToken" | |
} | |
$prListUrl = "$API_URL/pulls" | |
$pullRequests = FireAPIRequest -url $prListUrl -method "Get" -headers $COMMON_AUTH_HEADER | Flatten-Array | |
$prSummaries = GetActivePRs -pullRequestList $pullRequests | |
$prSummariesWithReadmes = @() | |
foreach($prSummary in $prSummaries) | |
{ | |
if($prSummary.PRFiles.Length -gt 0) | |
{ | |
Write-Host "$($prSummary.PRNumber) has affected readme files: " | |
Write-Host $prSummary.PRFiles | |
$prSummariesWithReadmes += $prSummary | |
} | |
} | |
return New-Object PSObject -Property @{ | |
PRsAffectingReadMes = $prSummariesWithReadmes | |
PRSummaries = $prSummaries | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment