Skip to content

Instantly share code, notes, and snippets.

@scbedd
Created February 9, 2021 23:38
Show Gist options
  • Save scbedd/f40a64c47014af729542e81c85133a13 to your computer and use it in GitHub Desktop.
Save scbedd/f40a64c47014af729542e81c85133a13 to your computer and use it in GitHub Desktop.
Collect PR updates from an azure-sdk Documentation PR and apply them to local
param(
[string]$RepoId,
[string]$PRNumber,
[string]$RepoRoot = "C:/repo/azure-docs-sdk-python"
)
$API_URL = "https://api.github.com/repos/$RepoId"
function FireAPIRequest($url, $method, $body = $null)
{
$ghToken = $env:GH_TOKEN
$COMMON_AUTH_HEADER = @{
"Content-Type" = "application/json"
"Authorization" = "token $ghToken"
}
$attempts = 1
while($attempts -le 3)
{
try
{
return Invoke-RestMethod -Method $method -Uri $url -Body $body
}
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
$results = $pullRequestFiles
return New-Object PSObject -Property @{
PRNumber = $pullRequestNumber
PRFiles = $results
}
}
$targetPRInfo = GetPRWithAffectedFiles -pullRequestNumber $PRNumber
$ciupdates = @($targetPRInfo.PRFiles | ? { return $_.filename.ToLower().Contains("packages-")})
$readmes = @($targetPRInfo.PRFiles | ? { return $_.filename.ToLower().Contains("readme")})
Write-Host "PR Number $PRNumber has changes to $($ciupdates[0].filename)"
foreach($readme in $readmes){
$content = Invoke-RestMethod -Method GET -Uri $readme.raw_url
Write-Host "Got Content for $($readme.fileName)"
$location = $($readme.fileName)
$fileName = ($location -Split "/")[-1]
if ($location -like "docs-ref-services/*/*-readme*.md") {
$output_location = "$RepoRoot/$location"
}
else {
if ($readme.fileName.Contains("-pre.md")){
$output_location = "$RepoRoot/docs-ref-services/preview/$($fileName.Replace("-pre.md", ".md"))"
}
else {
$output_location = "$RepoRoot/docs-ref-services/latest/$fileName"
}
}
Write-Host "Writing output to $output_location"
Set-Content -Path $output_location -Value $content
}
Write-Host $ciupdates[-1].patch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment