Implement oauth authorization instead of basic.
This commit is contained in:
parent
9052f43a63
commit
d60498ad7f
1 changed files with 30 additions and 37 deletions
|
|
@ -4,7 +4,7 @@
|
||||||
## Implements 'nimble publish' to create a pull request against
|
## Implements 'nimble publish' to create a pull request against
|
||||||
## nim-lang/packages automatically.
|
## nim-lang/packages automatically.
|
||||||
|
|
||||||
import httpclient, base64, strutils, rdstdin, json, os
|
import httpclient, base64, strutils, rdstdin, json, os, browsers
|
||||||
import tools, nimbletypes
|
import tools, nimbletypes
|
||||||
|
|
||||||
type
|
type
|
||||||
|
|
@ -16,56 +16,49 @@ type
|
||||||
proc userAborted() =
|
proc userAborted() =
|
||||||
raise newException(NimbleError, "User aborted the process.")
|
raise newException(NimbleError, "User aborted the process.")
|
||||||
|
|
||||||
proc getGithubAuth(): Auth =
|
proc createHeaders(a: Auth): string =
|
||||||
var user = ""
|
(("Authorization: token $1\c\L" % a.token) &
|
||||||
let (output, exitCode) = doCmdEx("git config user.name")
|
"Content-Type: application/x-www-form-urlencoded\c\L" &
|
||||||
if exitCode == 0:
|
"Accept: */*\c\L")
|
||||||
user = output.string.strip
|
|
||||||
if user.len == 0:
|
|
||||||
user = readLineFromStdin("Github user name: ")
|
|
||||||
if user.len == 0: userAborted()
|
|
||||||
let pw = readPasswordFromStdin("Github password for " & user & ": ")
|
|
||||||
if pw.len == 0: userAborted()
|
|
||||||
result.user = user
|
|
||||||
result.pw = pw
|
|
||||||
result.token = encode(user & ':' & pw)
|
|
||||||
|
|
||||||
proc searchFork(j: JsonNode): bool =
|
proc getGithubAuth(): Auth =
|
||||||
# Searches for: "fork":true recursively.
|
echo("Please create a new personal access token on Github in order to " &
|
||||||
case j.kind
|
"allow Nimble to fork the packages repository.")
|
||||||
of JObject:
|
sleep(5000)
|
||||||
for k, v in items(j.fields):
|
echo("Your default browser should open with the following URL: " &
|
||||||
if k == "fork" and v.kind == JBool: return v.bval
|
"https://github.com/settings/tokens/new")
|
||||||
for k, v in items(j.fields):
|
sleep(3000)
|
||||||
if searchFork(v): return true
|
openDefaultBrowser("https://github.com/settings/tokens/new")
|
||||||
of JArray:
|
result.token = readLineFromStdin("Personal access token: ").strip()
|
||||||
for x in j.elems:
|
let resp = getContent("https://api.github.com/user",
|
||||||
if searchFork(x): return true
|
extraHeaders=createHeaders(result)).parseJson()
|
||||||
else: discard
|
|
||||||
|
result.user = resp["login"].str
|
||||||
|
echo("Successfully verified as ", result.user)
|
||||||
|
|
||||||
|
proc isCorrectFork(j: JsonNode): bool =
|
||||||
|
# Check whether this is a fork of the nimble packages repo.
|
||||||
|
result = false
|
||||||
|
if j{"fork"}.getBVal():
|
||||||
|
result = j{"parent"}{"full_name"}.getStr() == "nim-lang/packages"
|
||||||
|
|
||||||
proc forkExists(a: Auth): bool =
|
proc forkExists(a: Auth): bool =
|
||||||
try:
|
try:
|
||||||
let x = getContent("https://api.github.com/repos/" & a.user & "/packages",
|
let x = getContent("https://api.github.com/repos/" & a.user & "/packages",
|
||||||
extraHeaders=("Authorization: Basic $1\c\L" % a.token) &
|
extraHeaders=createHeaders(a))
|
||||||
"Content-Type: application/x-www-form-urlencoded\c\L" &
|
|
||||||
"Accept: */*\c\L")
|
|
||||||
let j = parseJson(x)
|
let j = parseJson(x)
|
||||||
result = searchFork(j)
|
result = isCorrectFork(j)
|
||||||
except JsonParsingError, IOError:
|
except JsonParsingError, IOError:
|
||||||
result = false
|
result = false
|
||||||
|
|
||||||
proc createFork(a: Auth) =
|
proc createFork(a: Auth) =
|
||||||
discard postContent("https://api.github.com/repos/nim-lang/packages/forks",
|
discard postContent("https://api.github.com/repos/nim-lang/packages/forks",
|
||||||
extraHeaders=("Authorization: Basic $1\c\L" % a.token) &
|
extraHeaders=createHeaders(a))
|
||||||
"Content-Type: application/x-www-form-urlencoded\c\L" &
|
|
||||||
"Accept: */*\c\L")
|
|
||||||
|
|
||||||
proc createPullRequest(a: Auth; packageName: string) =
|
proc createPullRequest(a: Auth; packageName: string) =
|
||||||
echo "creating PR"
|
echo("Creating PR")
|
||||||
discard postContent("https://api.github.com/repos/nim-lang/packages/pulls",
|
discard postContent("https://api.github.com/repos/nim-lang/packages/pulls",
|
||||||
extraHeaders=("Authorization: Basic $1\c\L" % a.token) &
|
extraHeaders=createHeaders(a),
|
||||||
"Content-Type: application/x-www-form-urlencoded\c\L" &
|
|
||||||
"Accept: */*\c\L",
|
|
||||||
body="""{"title": "Add package $#", "head": "$#:master",
|
body="""{"title": "Add package $#", "head": "$#:master",
|
||||||
"base": "master"}""" % [packageName, a.user])
|
"base": "master"}""" % [packageName, a.user])
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue