aboutsummaryrefslogtreecommitdiffstats
path: root/bridge/github/config.go
diff options
context:
space:
mode:
authorMichael Muré <batolettre@gmail.com>2020-11-22 14:03:19 +0100
committerMichael Muré <batolettre@gmail.com>2020-11-22 14:04:47 +0100
commit9daa8ad04def334ff2ee23f85a5f583044428290 (patch)
tree387aaca6c9352f4a89497f8a92e61f9eb7876aab /bridge/github/config.go
parent8d5c47011aa2a14960637b58ff0ebd08291a9608 (diff)
downloadgit-bug-9daa8ad04def334ff2ee23f85a5f583044428290.tar.gz
github: minor cleanups
Diffstat (limited to 'bridge/github/config.go')
-rw-r--r--bridge/github/config.go38
1 files changed, 26 insertions, 12 deletions
diff --git a/bridge/github/config.go b/bridge/github/config.go
index 87d266da..2b5af7fb 100644
--- a/bridge/github/config.go
+++ b/bridge/github/config.go
@@ -23,9 +23,10 @@ import (
"github.com/MichaelMure/git-bug/repository"
)
+const githubClientID = "ce3600aa56c2e69f18a5" // git-bug org
+
var (
ErrBadProjectURL = errors.New("bad project url")
- githubClientID = "ce3600aa56c2e69f18a5"
)
func (g *Github) ValidParams() map[string]interface{} {
@@ -170,13 +171,6 @@ func (*Github) ValidateConfig(conf core.Configuration) error {
return nil
}
-type githRespT struct {
- uri string
- userCode string
- deviceCode string
- interval int64
-}
-
func requestToken() (string, error) {
scope, err := promptUserForProjectVisibility()
if err != nil {
@@ -206,6 +200,13 @@ func promptUserForProjectVisibility() (string, error) {
return []string{"public_repo", "repo"}[index], nil
}
+type githRespT struct {
+ uri string
+ userCode string
+ deviceCode string
+ interval int64
+}
+
func requestUserVerificationCode(scope string) (*githRespT, error) {
params := url.Values{}
params.Set("client_id", githubClientID)
@@ -213,6 +214,7 @@ func requestUserVerificationCode(scope string) (*githRespT, error) {
client := &http.Client{
Timeout: defaultTimeout,
}
+
resp, err := client.PostForm("https://github.com/login/device/code", params)
if err != nil {
return nil, errors.Wrap(err, "error requesting user verification code")
@@ -221,21 +223,28 @@ func requestUserVerificationCode(scope string) (*githRespT, error) {
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("unexpected response status code %d from Github API", resp.StatusCode)
}
+
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, errors.Wrap(err, "error requesting user verification code")
}
+
vals, err := url.ParseQuery(string(data))
if err != nil {
return nil, errors.Wrap(err, "error decoding Github API response")
}
+
interval, err := strconv.ParseInt(vals.Get("interval"), 10, 64) // base 10, bitSize 64
if err != nil {
return nil, errors.Wrap(err, "Error parsing integer received from Github API")
}
- result := githRespT{uri: vals.Get("verification_uri"), userCode: vals.Get("user_code"),
- deviceCode: vals.Get("device_code"), interval: interval}
- return &result, nil
+
+ return &githRespT{
+ uri: vals.Get("verification_uri"),
+ userCode: vals.Get("user_code"),
+ deviceCode: vals.Get("device_code"),
+ interval: interval,
+ }, nil
}
func promptUserToGoToBrowser(url, userCode string) {
@@ -255,19 +264,24 @@ func pollGithubForAuthorization(deviceCode string, intervalSec int64) (string, e
Timeout: defaultTimeout,
}
interval := time.Duration(intervalSec * 1100) // milliseconds, add 10% margin
+
for {
resp, err := client.PostForm("https://github.com/login/oauth/access_token", params)
if err != nil {
return "", errors.Wrap(err, "error polling the Github API")
}
- defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
+ _ = resp.Body.Close()
return "", fmt.Errorf("unexpected response status code %d from Github API", resp.StatusCode)
}
+
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
+ _ = resp.Body.Close()
return "", errors.Wrap(err, "error polling the Github API")
}
+ _ = resp.Body.Close()
+
values, err := url.ParseQuery(string(data))
if err != nil {
return "", errors.Wrap(err, "error decoding Github API response")