Skip to content

Instantly share code, notes, and snippets.

@breezhang
Forked from timheuer/gistdiff.ps1
Created August 30, 2012 07:33
Show Gist options
  • Save breezhang/3523780 to your computer and use it in GitHub Desktop.
Save breezhang/3523780 to your computer and use it in GitHub Desktop.
My hack start at creating a script for auto-submitting a diff to gist.github.com
# ask the user for a gist name
param (
[string]$gistname = "$(Read-Host 'Enter a name for the gist')"
)
# constant
$gistapi = "https://api.github.com/gists"
$filename = $gistname + '.diff'
$teststring = "`{`"description`": `"the description for this gist`",`"public`": true,`"files`": `{`"file1.txt`": `{`"content`": `"String file contents`"`}`}`}"
# get the diff from the current session
$diff = git diff
# build up the json object data
$data = (New-Object PSObject |
Add-Member -PassThru NoteProperty description $gistname |
Add-Member -PassThru NoteProperty public true |
Add-Member -PassThru NoteProperty files (New-Object PSObject |
Add-Member -PassThru NoteProperty $filename (New-Object PSObject |
Add-Member -PassThru NoteProperty content "testing"
) | ConvertTo-Json -Compress
) | ConvertTo-Json -Compress
) | ConvertTo-Json -Compress
# get the byte[] for the POST
#$bytes = [System.Text.Encoding]::ASCII.GetBytes($data)
Write-Host $data
# create the web client to post
$wc = New-Object System.Net.WebClient
$wc.Headers.Add("Content-Type", "application/vnd.github.beta+json")
$resp = $wc.UploadString($gistapi, $data)
# temp: write out the response
# ideally this would parse response, write out the resulting URI to the gist
Write-Host ([System.Text.Encoding]::ASCII).GetString($resp)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment