Skip to content

Commit

Permalink
Allow GitHub integration to work on multiple machines
Browse files Browse the repository at this point in the history
  • Loading branch information
Keno committed Aug 20, 2014
1 parent 6a1cc96 commit c83ad07
Showing 1 changed file with 30 additions and 3 deletions.
33 changes: 30 additions & 3 deletions base/pkg/github.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ module GitHub

import Main, ..Git, ..Dir

const AUTH_NOTE = "Julia Package Manager"
const AUTH_DATA = {
"scopes" => ["repo"],
"note" => "Julia Package Manager",
"note" => AUTH_NOTE,
"note_url" => "http:https://docs.julialang.org/en/latest/manual/packages/",
}

Expand Down Expand Up @@ -45,12 +46,34 @@ curl(url::String, data::Nothing, opts::Cmd=``) = curl(url,opts)
curl(url::String, data, opts::Cmd=``) =
curl(url,`--data $(sprint(io->json().print(io,data))) $opts`)

function delete_token()
tokfile = Dir.path(".github","token")
Base.rm(tokfile)
info("Could not authenticate with existing token. Deleting token and trying again.")
end

function token(user::String=user())
tokfile = Dir.path(".github","token")
isfile(tokfile) && return strip(readchomp(tokfile))
status, content = curl("https://api.github.com/authorizations",AUTH_DATA,`-u $user`)
(status != 401 && status != 403) || error("$status: $(json().parse(content)["message"])")
tok = json().parse(content)["token"]
if status == 422
error_code = json().parse(content)["errors"][1]["code"]
if error_code == "already_exists"
info("Retrieving existing GitHub token (you may have to enter you password again)")
status,content = curl("https://api.github.com/authorizations",`-u $user`)
(status >= 400) && error("$status: $(json().parse(content)["message"])")
for entry in json().parse(content)
if entry["note"] == AUTH_NOTE
tok = entry["token"]
end
end
else
error("GitHub returned validation error (422): $error_code")
end
else
(status != 401 && status != 403) || error("$status: $(json().parse(content)["message"])")
tok = json().parse(content)["token"]
end
mkpath(dirname(tokfile))
open(io->println(io,tok),tokfile,"w")
return tok
Expand Down Expand Up @@ -85,6 +108,10 @@ end

function fork(owner::String, repo::String)
status, response = POST("repos/$owner/$repo/forks")
if status == 401
delete_token()
status, response = POST("repos/$owner/$repo/forks")
end
status == 202 || error("forking $owner/$repo failed: $(response["message"])")
return response
end
Expand Down

0 comments on commit c83ad07

Please sign in to comment.