Created
November 3, 2020 14:31
-
-
Save g3rv4/bc92b7eccae64d003c0e089ed9cd7963 to your computer and use it in GitHub Desktop.
Script to download all the gists for a user
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
param( | |
[Parameter(Mandatory = $true)] | |
[string]$PathToApi, | |
[Parameter(Mandatory = $true)] | |
[string]$PAT, | |
[Parameter(Mandatory = $true)] | |
[string]$Username, | |
[Parameter(Mandatory = $true)] | |
[string]$PathToPrivateGists, | |
[Parameter(Mandatory = $true)] | |
[string]$PathToPublicGists | |
) | |
function New-TemporaryDirectory { | |
$parent = [System.IO.Path]::GetTempPath() | |
[string] $name = [System.Guid]::NewGuid() | |
New-Item -ItemType Directory -Path (Join-Path $parent $name) | |
} | |
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]" | |
$headers.Add("authorization", 'token ' + $PAT) | |
$headers.Add("accept", 'application/vnd.github.v3+json') | |
$gistsUrl = "$($PathToApi)/users/$($Username)/gists?page=" | |
$allGists = @() | |
$page = 1 | |
do { | |
$gists = Invoke-RestMethod -Method 'Get' -Uri "$($gistsUrl)$($page)" -Body $body -Headers $headers | |
$allGists += $gists | |
$page++ | |
} while ($gists.Length -gt 0) | |
$pullPath = New-TemporaryDirectory | |
Push-Location $pullPath | |
foreach ($gist in $allGists) { | |
git clone $gist.git_pull_url | |
Push-Location $gist.id | |
$dest = if($gist.public) {$PathToPublicGists} else {$PathToPrivateGists} | |
Copy-Item * $dest | |
Pop-Location | |
} | |
Pop-Location |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment