Created
May 10, 2019 07:49
-
-
Save AGBrown/cfdde27cf8b1af7ffe7ca2209d25178f to your computer and use it in GitHub Desktop.
Recursively fetch all forks for a git repo
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
function Get-BasicAuthCreds { | |
param([string]$Username,[string]$Password) | |
$AuthString = "{0}:{1}" -f $Username,$Password | |
$AuthBytes = [System.Text.Encoding]::Ascii.GetBytes($AuthString) | |
return [Convert]::ToBase64String($AuthBytes) | |
} | |
function fetch-forks ($orig, $Creds) { | |
#Write-host $orig.forks_url | |
#Write-host $Creds | |
if ([String]::IsNullOrWhitespace($orig.forks_url) -eq $false) { | |
$d = Invoke-RestMethod -Uri $($orig.forks_url) -Headers @{"Authorization"="Basic $Creds"} | |
#Write-host "next" | |
$d |% { | |
$r = [ordered]@{ | |
clone_url=$_.clone_url; | |
forks_url=$_.forks_url; | |
owner=$("{0}{1}-" -f $orig.owner, $_.owner.login); | |
} | |
$r | |
fetch-forks $r $Creds | |
} | |
} | |
} | |
$token = # enter git personal access token with public repo and user read rights | |
$user = # enter git username | |
$BasicCreds = Get-BasicAuthCreds -Username $user -Password $token | |
# confirm that you are auth'd correctly and the rate limits are maxed | |
Invoke-RestMethod https://api.github.com/rate_limit -Headers @{"Authorization"="Basic $BasicCreds"} | |
$o = [ordered]@{ | |
forks_url= # enter the top-level repo forks url. e.g. "https://api.github.com/repos/AGBrown/pouchdb.d.ts/forks"; | |
owner=""; | |
} | |
$data = fetch-forks $o $BasicCreds | |
# add remotes and fetch them | |
$data |% { | |
$nm = $($_.owner.TrimEnd("-")) | |
git remote add $nm $_.clone_url | |
git fetch $nm | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment