Skip to content

Commit

Permalink
github: minor cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelMure committed Nov 22, 2020
1 parent 8d5c470 commit 9daa8ad
Showing 1 changed file with 26 additions and 12 deletions.
38 changes: 26 additions & 12 deletions bridge/github/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{} {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -206,13 +200,21 @@ 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)
params.Set("scope", scope)
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")
Expand All @@ -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) {
Expand All @@ -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")
Expand Down

0 comments on commit 9daa8ad

Please sign in to comment.