Skip to content

Instantly share code, notes, and snippets.

@g3rv4
Created November 3, 2020 14:31
Show Gist options
  • Save g3rv4/bc92b7eccae64d003c0e089ed9cd7963 to your computer and use it in GitHub Desktop.
Save g3rv4/bc92b7eccae64d003c0e089ed9cd7963 to your computer and use it in GitHub Desktop.
Script to download all the gists for a user
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