2018-09-21 13:54:48 +03:00
|
|
|
package github
|
|
|
|
|
|
|
|
import (
|
2020-02-10 00:17:10 +03:00
|
|
|
"context"
|
2018-09-21 13:54:48 +03:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"math/rand"
|
|
|
|
"net/http"
|
2020-11-10 00:15:46 +03:00
|
|
|
"net/url"
|
2018-09-21 19:23:46 +03:00
|
|
|
"regexp"
|
2019-11-19 23:12:10 +03:00
|
|
|
"sort"
|
2020-11-11 01:50:38 +03:00
|
|
|
"strconv"
|
2018-09-21 13:54:48 +03:00
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2019-05-29 21:49:55 +03:00
|
|
|
"github.com/pkg/errors"
|
2019-05-24 22:04:25 +03:00
|
|
|
|
2018-09-21 19:23:46 +03:00
|
|
|
"github.com/MichaelMure/git-bug/bridge/core"
|
2019-12-08 23:15:06 +03:00
|
|
|
"github.com/MichaelMure/git-bug/bridge/core/auth"
|
|
|
|
"github.com/MichaelMure/git-bug/cache"
|
2020-01-08 00:06:42 +03:00
|
|
|
"github.com/MichaelMure/git-bug/input"
|
2018-09-21 19:23:46 +03:00
|
|
|
"github.com/MichaelMure/git-bug/repository"
|
2018-09-21 13:54:48 +03:00
|
|
|
)
|
|
|
|
|
2019-05-24 22:04:25 +03:00
|
|
|
var (
|
2019-05-29 21:49:55 +03:00
|
|
|
ErrBadProjectURL = errors.New("bad project url")
|
2020-11-11 01:50:38 +03:00
|
|
|
githubClientID = "ce3600aa56c2e69f18a5"
|
2019-05-24 22:04:25 +03:00
|
|
|
)
|
2018-09-21 13:54:48 +03:00
|
|
|
|
2020-02-15 04:55:19 +03:00
|
|
|
func (g *Github) ValidParams() map[string]interface{} {
|
|
|
|
return map[string]interface{}{
|
|
|
|
"URL": nil,
|
|
|
|
"Login": nil,
|
|
|
|
"CredPrefix": nil,
|
|
|
|
"TokenRaw": nil,
|
|
|
|
"Owner": nil,
|
|
|
|
"Project": nil,
|
2019-12-10 22:30:29 +03:00
|
|
|
}
|
2020-02-15 04:55:19 +03:00
|
|
|
}
|
2019-12-10 22:30:29 +03:00
|
|
|
|
2020-02-15 04:55:19 +03:00
|
|
|
func (g *Github) Configure(repo *cache.RepoCache, params core.BridgeParams) (core.Configuration, error) {
|
2019-05-24 22:04:25 +03:00
|
|
|
var err error
|
2019-11-26 21:46:50 +03:00
|
|
|
var owner string
|
|
|
|
var project string
|
2020-02-29 17:04:48 +03:00
|
|
|
var ok bool
|
2019-12-08 23:15:06 +03:00
|
|
|
|
2019-05-25 16:56:52 +03:00
|
|
|
// getting owner and project name
|
2019-11-26 21:46:50 +03:00
|
|
|
switch {
|
|
|
|
case params.Owner != "" && params.Project != "":
|
2019-05-25 18:27:25 +03:00
|
|
|
// first try to use params if both or project and owner are provided
|
2019-05-24 22:04:25 +03:00
|
|
|
owner = params.Owner
|
|
|
|
project = params.Project
|
2019-11-26 21:46:50 +03:00
|
|
|
case params.URL != "":
|
2019-05-25 18:27:25 +03:00
|
|
|
// try to parse params URL and extract owner and project
|
2019-05-29 21:49:55 +03:00
|
|
|
owner, project, err = splitURL(params.URL)
|
2019-05-24 22:04:25 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-11-26 21:46:50 +03:00
|
|
|
default:
|
2019-05-25 18:27:25 +03:00
|
|
|
// terminal prompt
|
2019-12-08 23:15:06 +03:00
|
|
|
owner, project, err = promptURL(repo)
|
2019-05-24 22:04:25 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-09-21 13:54:48 +03:00
|
|
|
}
|
|
|
|
|
2020-02-29 17:04:48 +03:00
|
|
|
// validate project owner and override with the correct case
|
|
|
|
ok, owner, err = validateUsername(owner)
|
2018-09-21 13:54:48 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-05-24 22:04:25 +03:00
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("invalid parameter owner: %v", owner)
|
2018-09-21 13:54:48 +03:00
|
|
|
}
|
|
|
|
|
2020-02-10 00:17:10 +03:00
|
|
|
var login string
|
2019-12-08 23:15:06 +03:00
|
|
|
var cred auth.Credential
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case params.CredPrefix != "":
|
|
|
|
cred, err = auth.LoadWithPrefix(repo, params.CredPrefix)
|
2018-09-21 19:23:46 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2018-09-21 13:54:48 +03:00
|
|
|
}
|
2020-02-10 00:17:10 +03:00
|
|
|
l, ok := cred.GetMetadata(auth.MetaKeyLogin)
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("credential doesn't have a login")
|
|
|
|
}
|
|
|
|
login = l
|
2019-12-08 23:15:06 +03:00
|
|
|
case params.TokenRaw != "":
|
2020-02-12 20:32:01 +03:00
|
|
|
token := auth.NewToken(target, params.TokenRaw)
|
2020-02-10 00:17:10 +03:00
|
|
|
login, err = getLoginFromToken(token)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
token.SetMetadata(auth.MetaKeyLogin, login)
|
|
|
|
cred = token
|
2019-12-08 23:15:06 +03:00
|
|
|
default:
|
2020-02-29 17:04:48 +03:00
|
|
|
if params.Login == "" {
|
|
|
|
login, err = promptLogin()
|
|
|
|
} else {
|
|
|
|
// validate login and override with the correct case
|
|
|
|
ok, login, err = validateUsername(params.Login)
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("invalid parameter login: %v", params.Login)
|
2020-02-10 00:17:10 +03:00
|
|
|
}
|
|
|
|
}
|
2020-02-29 17:04:48 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-01-08 00:07:25 +03:00
|
|
|
cred, err = promptTokenOptions(repo, login, owner, project)
|
2019-11-23 21:20:32 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-12-08 23:15:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
token, ok := cred.(*auth.Token)
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("the Github bridge only handle token credentials")
|
2018-09-21 13:54:48 +03:00
|
|
|
}
|
|
|
|
|
2019-05-25 16:20:30 +03:00
|
|
|
// verify access to the repository with token
|
2019-12-08 23:15:06 +03:00
|
|
|
ok, err = validateProject(owner, project, token)
|
2019-05-24 22:04:25 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if !ok {
|
2019-06-05 01:45:34 +03:00
|
|
|
return nil, fmt.Errorf("project doesn't exist or authentication token has an incorrect scope")
|
2019-05-24 22:04:25 +03:00
|
|
|
}
|
|
|
|
|
2020-02-15 04:55:19 +03:00
|
|
|
conf := make(core.Configuration)
|
2019-11-10 19:48:13 +03:00
|
|
|
conf[core.ConfigKeyTarget] = target
|
2020-02-15 04:55:19 +03:00
|
|
|
conf[confKeyOwner] = owner
|
|
|
|
conf[confKeyProject] = project
|
2020-02-23 16:05:03 +03:00
|
|
|
conf[confKeyDefaultLogin] = login
|
2018-09-21 13:54:48 +03:00
|
|
|
|
2019-08-21 15:24:48 +03:00
|
|
|
err = g.ValidateConfig(conf)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-12-08 23:15:06 +03:00
|
|
|
// don't forget to store the now known valid token
|
|
|
|
if !auth.IdExist(repo, cred.ID()) {
|
|
|
|
err = auth.Store(repo, cred)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-24 02:30:13 +03:00
|
|
|
return conf, core.FinishConfig(repo, metaKeyGithubLogin, login)
|
2018-09-21 13:54:48 +03:00
|
|
|
}
|
|
|
|
|
2020-02-15 15:45:14 +03:00
|
|
|
func (*Github) ValidateConfig(conf core.Configuration) error {
|
2019-11-10 19:48:13 +03:00
|
|
|
if v, ok := conf[core.ConfigKeyTarget]; !ok {
|
|
|
|
return fmt.Errorf("missing %s key", core.ConfigKeyTarget)
|
2019-06-17 00:02:59 +03:00
|
|
|
} else if v != target {
|
|
|
|
return fmt.Errorf("unexpected target name: %v", v)
|
2019-06-15 03:33:06 +03:00
|
|
|
}
|
2020-02-15 04:55:19 +03:00
|
|
|
if _, ok := conf[confKeyOwner]; !ok {
|
|
|
|
return fmt.Errorf("missing %s key", confKeyOwner)
|
2018-09-24 18:21:24 +03:00
|
|
|
}
|
2020-02-15 04:55:19 +03:00
|
|
|
if _, ok := conf[confKeyProject]; !ok {
|
|
|
|
return fmt.Errorf("missing %s key", confKeyProject)
|
2018-09-24 18:21:24 +03:00
|
|
|
}
|
2020-02-23 16:05:03 +03:00
|
|
|
if _, ok := conf[confKeyDefaultLogin]; !ok {
|
|
|
|
return fmt.Errorf("missing %s key", confKeyDefaultLogin)
|
|
|
|
}
|
2018-09-24 18:21:24 +03:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-11-17 21:59:29 +03:00
|
|
|
type githRespT struct {
|
|
|
|
uri string
|
|
|
|
userCode string
|
|
|
|
deviceCode string
|
|
|
|
interval int64
|
|
|
|
}
|
|
|
|
|
2020-11-10 00:15:46 +03:00
|
|
|
func requestToken() (string, error) {
|
2020-11-11 01:50:38 +03:00
|
|
|
scope, err := promptUserForProjectVisibility()
|
2020-11-10 00:15:46 +03:00
|
|
|
if err != nil {
|
2020-11-11 01:50:38 +03:00
|
|
|
return "", errors.WithStack(err)
|
2018-09-21 15:38:44 +03:00
|
|
|
}
|
2020-11-17 21:59:29 +03:00
|
|
|
resp, err := requestUserVerificationCode(scope)
|
2018-09-21 15:38:44 +03:00
|
|
|
if err != nil {
|
2020-11-10 00:15:46 +03:00
|
|
|
return "", err
|
2018-09-21 15:38:44 +03:00
|
|
|
}
|
2020-11-17 21:59:29 +03:00
|
|
|
promptUserToGoToBrowser(resp.uri, resp.userCode)
|
|
|
|
return pollGithubForAuthorization(resp.deviceCode, resp.interval)
|
2020-11-11 01:50:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func promptUserForProjectVisibility() (string, error) {
|
|
|
|
fmt.Println("git-bug will now generate an access token in your Github profile. The token is stored in the global git config.")
|
|
|
|
fmt.Println()
|
|
|
|
fmt.Println("The access scope depend on the type of repository.")
|
|
|
|
fmt.Println("Public:")
|
|
|
|
fmt.Println(" - 'public_repo': to be able to read public repositories")
|
|
|
|
fmt.Println("Private:")
|
|
|
|
fmt.Println(" - 'repo' : to be able to read private repositories")
|
|
|
|
fmt.Println()
|
|
|
|
index, err := input.PromptChoice("repository visibility", []string{"public", "private"})
|
2020-11-10 00:15:46 +03:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
2018-09-21 15:38:44 +03:00
|
|
|
}
|
2020-11-11 01:50:38 +03:00
|
|
|
return []string{"public_repo", "repo"}[index], nil
|
2020-11-10 00:15:46 +03:00
|
|
|
}
|
2018-09-21 15:38:44 +03:00
|
|
|
|
2020-11-17 21:59:29 +03:00
|
|
|
func requestUserVerificationCode(scope string) (*githRespT, error) {
|
2020-11-10 00:15:46 +03:00
|
|
|
params := url.Values{}
|
2020-11-11 01:50:38 +03:00
|
|
|
params.Set("client_id", githubClientID)
|
2020-11-10 00:15:46 +03:00
|
|
|
params.Set("scope", scope)
|
2019-05-24 22:04:25 +03:00
|
|
|
client := &http.Client{
|
|
|
|
Timeout: defaultTimeout,
|
|
|
|
}
|
2020-11-10 00:15:46 +03:00
|
|
|
resp, err := client.PostForm("https://github.com/login/device/code", params)
|
|
|
|
if err != nil {
|
2020-11-11 01:50:38 +03:00
|
|
|
return nil, errors.Wrap(err, "error requesting user verification code")
|
2020-11-10 00:15:46 +03:00
|
|
|
}
|
2020-11-11 01:50:38 +03:00
|
|
|
defer resp.Body.Close()
|
2020-11-10 00:15:46 +03:00
|
|
|
if resp.StatusCode != http.StatusOK {
|
2020-11-17 18:01:45 +03:00
|
|
|
return nil, fmt.Errorf("unexpected response status code %d from Github API", resp.StatusCode)
|
2020-11-10 00:15:46 +03:00
|
|
|
}
|
2020-11-11 01:50:38 +03:00
|
|
|
data, err := ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "error requesting user verification code")
|
|
|
|
}
|
2020-11-17 21:59:29 +03:00
|
|
|
vals, err := url.ParseQuery(string(data))
|
2020-11-11 01:50:38 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "error decoding Github API response")
|
|
|
|
}
|
2020-11-17 21:59:29 +03:00
|
|
|
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")
|
2020-11-11 01:50:38 +03:00
|
|
|
}
|
2020-11-17 21:59:29 +03:00
|
|
|
result := githRespT{uri: vals.Get("verification_uri"), userCode: vals.Get("user_code"),
|
|
|
|
deviceCode: vals.Get("device_code"), interval: interval}
|
|
|
|
return &result, nil
|
2018-09-21 15:38:44 +03:00
|
|
|
}
|
|
|
|
|
2020-11-11 01:50:38 +03:00
|
|
|
func promptUserToGoToBrowser(url, userCode string) {
|
|
|
|
fmt.Println("Please visit the following Github URL in a browser and enter your user authentication code.")
|
2020-11-10 00:15:46 +03:00
|
|
|
fmt.Println()
|
2020-11-11 01:50:38 +03:00
|
|
|
fmt.Println(" URL:", url)
|
|
|
|
fmt.Println(" user authentiation code:", userCode)
|
2020-11-10 00:15:46 +03:00
|
|
|
fmt.Println()
|
|
|
|
}
|
2018-09-21 13:54:48 +03:00
|
|
|
|
2020-11-11 01:50:38 +03:00
|
|
|
func pollGithubForAuthorization(deviceCode string, intervalSec int64) (string, error) {
|
2020-11-10 00:15:46 +03:00
|
|
|
params := url.Values{}
|
2020-11-11 01:50:38 +03:00
|
|
|
params.Set("client_id", githubClientID)
|
|
|
|
params.Set("device_code", deviceCode)
|
2020-11-10 00:15:46 +03:00
|
|
|
params.Set("grant_type", "urn:ietf:params:oauth:grant-type:device_code") // fixed by RFC 8628
|
|
|
|
client := &http.Client{
|
|
|
|
Timeout: defaultTimeout,
|
2018-09-21 19:23:46 +03:00
|
|
|
}
|
2020-11-11 01:50:38 +03:00
|
|
|
interval := time.Duration(intervalSec * 1100) // milliseconds, add 10% margin
|
2020-11-10 00:15:46 +03:00
|
|
|
for {
|
|
|
|
resp, err := client.PostForm("https://github.com/login/oauth/access_token", params)
|
|
|
|
if err != nil {
|
2020-11-11 01:50:38 +03:00
|
|
|
return "", errors.Wrap(err, "error polling the Github API")
|
2020-11-10 00:15:46 +03:00
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
2020-11-17 18:01:45 +03:00
|
|
|
return "", fmt.Errorf("unexpected response status code %d from Github API", resp.StatusCode)
|
2020-11-10 00:15:46 +03:00
|
|
|
}
|
2020-11-11 01:50:38 +03:00
|
|
|
data, err := ioutil.ReadAll(resp.Body)
|
2020-11-10 00:15:46 +03:00
|
|
|
if err != nil {
|
2020-11-11 01:50:38 +03:00
|
|
|
return "", errors.Wrap(err, "error polling the Github API")
|
2020-11-10 00:15:46 +03:00
|
|
|
}
|
2020-11-11 01:50:38 +03:00
|
|
|
values, err := url.ParseQuery(string(data))
|
2020-11-10 00:15:46 +03:00
|
|
|
if err != nil {
|
2020-11-11 01:50:38 +03:00
|
|
|
return "", errors.Wrap(err, "error decoding Github API response")
|
2020-11-10 00:15:46 +03:00
|
|
|
}
|
2020-11-11 01:50:38 +03:00
|
|
|
|
|
|
|
if token := values.Get("access_token"); token != "" {
|
|
|
|
return token, nil
|
2020-11-10 00:15:46 +03:00
|
|
|
}
|
2020-11-11 01:50:38 +03:00
|
|
|
|
|
|
|
switch apiError := values.Get("error"); apiError {
|
|
|
|
case "slow_down":
|
|
|
|
interval += 5500 // add 5 seconds (RFC 8628), plus some margin
|
|
|
|
time.Sleep(interval * time.Millisecond)
|
|
|
|
continue
|
|
|
|
case "authorization_pending":
|
|
|
|
time.Sleep(interval * time.Millisecond)
|
|
|
|
continue
|
|
|
|
case "":
|
|
|
|
return "", errors.New("unexpected response from Github API")
|
|
|
|
default:
|
|
|
|
// apiError should equal one of: "expired_token", "unsupported_grant_type",
|
|
|
|
// "incorrect_client_credentials", "incorrect_device_code", or "access_denied"
|
|
|
|
return "", fmt.Errorf("error creating token: %v, %v", apiError, values.Get("error_description"))
|
2020-11-10 00:15:46 +03:00
|
|
|
}
|
2018-09-21 13:54:48 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func randomFingerprint() string {
|
|
|
|
// Doesn't have to be crypto secure, it's just to avoid token collision
|
|
|
|
rand.Seed(time.Now().UnixNano())
|
|
|
|
var letterRunes = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
|
|
|
|
b := make([]rune, 32)
|
|
|
|
for i := range b {
|
|
|
|
b[i] = letterRunes[rand.Intn(len(letterRunes))]
|
|
|
|
}
|
|
|
|
return string(b)
|
|
|
|
}
|
|
|
|
|
2020-07-28 13:56:46 +03:00
|
|
|
func promptTokenOptions(repo repository.RepoKeyring, login, owner, project string) (auth.Credential, error) {
|
2020-02-15 04:55:19 +03:00
|
|
|
creds, err := auth.List(repo,
|
|
|
|
auth.WithTarget(target),
|
|
|
|
auth.WithKind(auth.KindToken),
|
|
|
|
auth.WithMeta(auth.MetaKeyLogin, login),
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-05-25 16:20:30 +03:00
|
|
|
|
2020-02-15 15:45:14 +03:00
|
|
|
cred, index, err := input.PromptCredential(target, "token", creds, []string{
|
|
|
|
"enter my token",
|
|
|
|
"interactive token creation",
|
|
|
|
})
|
|
|
|
switch {
|
|
|
|
case err != nil:
|
|
|
|
return nil, err
|
|
|
|
case cred != nil:
|
2020-02-15 04:55:19 +03:00
|
|
|
return cred, nil
|
2020-02-15 15:45:14 +03:00
|
|
|
case index == 0:
|
2020-02-15 04:55:19 +03:00
|
|
|
return promptToken()
|
2020-02-15 15:45:14 +03:00
|
|
|
case index == 1:
|
2020-11-10 00:15:46 +03:00
|
|
|
value, err := requestToken()
|
2019-05-25 16:20:30 +03:00
|
|
|
if err != nil {
|
2019-11-23 21:20:32 +03:00
|
|
|
return nil, err
|
2019-05-25 16:20:30 +03:00
|
|
|
}
|
2020-02-15 04:55:19 +03:00
|
|
|
token := auth.NewToken(target, value)
|
|
|
|
token.SetMetadata(auth.MetaKeyLogin, login)
|
|
|
|
return token, nil
|
|
|
|
default:
|
2020-02-15 15:45:14 +03:00
|
|
|
panic("missed case")
|
2019-11-23 21:20:32 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-10 00:17:10 +03:00
|
|
|
func promptToken() (*auth.Token, error) {
|
2019-05-26 18:35:39 +03:00
|
|
|
fmt.Println("You can generate a new token by visiting https://github.com/settings/tokens.")
|
|
|
|
fmt.Println("Choose 'Generate new token' and set the necessary access scope for your repository.")
|
|
|
|
fmt.Println()
|
|
|
|
fmt.Println("The access scope depend on the type of repository.")
|
|
|
|
fmt.Println("Public:")
|
2019-05-29 21:49:55 +03:00
|
|
|
fmt.Println(" - 'public_repo': to be able to read public repositories")
|
2019-05-26 18:35:39 +03:00
|
|
|
fmt.Println("Private:")
|
2019-05-29 21:49:55 +03:00
|
|
|
fmt.Println(" - 'repo' : to be able to read private repositories")
|
2019-05-26 18:35:39 +03:00
|
|
|
fmt.Println()
|
|
|
|
|
2020-02-23 16:23:34 +03:00
|
|
|
re := regexp.MustCompile(`^[a-zA-Z0-9]{40}$`)
|
2019-06-05 02:42:36 +03:00
|
|
|
|
2020-02-10 00:17:10 +03:00
|
|
|
var login string
|
|
|
|
|
2020-01-08 00:06:42 +03:00
|
|
|
validator := func(name string, value string) (complaint string, err error) {
|
2020-02-10 00:17:10 +03:00
|
|
|
if !re.MatchString(value) {
|
|
|
|
return "token has incorrect format", nil
|
|
|
|
}
|
2020-02-12 20:32:01 +03:00
|
|
|
login, err = getLoginFromToken(auth.NewToken(target, value))
|
2020-02-10 00:17:10 +03:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Sprintf("token is invalid: %v", err), nil
|
2019-05-25 16:20:30 +03:00
|
|
|
}
|
2020-02-10 00:17:10 +03:00
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
|
|
|
|
rawToken, err := input.Prompt("Enter token", "token", input.Required, validator)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2019-05-25 16:20:30 +03:00
|
|
|
}
|
2020-01-08 00:06:42 +03:00
|
|
|
|
2020-02-12 20:32:01 +03:00
|
|
|
token := auth.NewToken(target, rawToken)
|
2020-02-10 00:17:10 +03:00
|
|
|
token.SetMetadata(auth.MetaKeyLogin, login)
|
|
|
|
|
|
|
|
return token, nil
|
2019-05-25 16:20:30 +03:00
|
|
|
}
|
|
|
|
|
2019-12-08 23:15:06 +03:00
|
|
|
func promptURL(repo repository.RepoCommon) (string, string, error) {
|
2020-02-15 04:55:19 +03:00
|
|
|
validRemotes, err := getValidGithubRemoteURLs(repo)
|
2019-12-08 23:15:06 +03:00
|
|
|
if err != nil {
|
|
|
|
return "", "", err
|
|
|
|
}
|
|
|
|
|
2020-02-15 04:55:19 +03:00
|
|
|
validator := func(name, value string) (string, error) {
|
|
|
|
_, _, err := splitURL(value)
|
2018-09-21 19:23:46 +03:00
|
|
|
if err != nil {
|
2020-02-15 04:55:19 +03:00
|
|
|
return err.Error(), nil
|
2018-09-21 19:23:46 +03:00
|
|
|
}
|
2020-02-15 04:55:19 +03:00
|
|
|
return "", nil
|
|
|
|
}
|
2018-09-21 19:23:46 +03:00
|
|
|
|
2020-02-15 04:55:19 +03:00
|
|
|
url, err := input.PromptURLWithRemote("Github project URL", "URL", validRemotes, input.Required, validator)
|
|
|
|
if err != nil {
|
|
|
|
return "", "", err
|
2018-09-21 19:23:46 +03:00
|
|
|
}
|
2020-02-15 04:55:19 +03:00
|
|
|
|
|
|
|
return splitURL(url)
|
2018-09-21 19:23:46 +03:00
|
|
|
}
|
|
|
|
|
2019-05-29 21:49:55 +03:00
|
|
|
// splitURL extract the owner and project from a github repository URL. It will remove the
|
|
|
|
// '.git' extension from the URL before parsing it.
|
|
|
|
// Note that Github removes the '.git' extension from projects names at their creation
|
|
|
|
func splitURL(url string) (owner string, project string, err error) {
|
2019-05-26 18:35:39 +03:00
|
|
|
cleanURL := strings.TrimSuffix(url, ".git")
|
2019-05-29 21:49:55 +03:00
|
|
|
|
2020-02-15 04:55:19 +03:00
|
|
|
re := regexp.MustCompile(`github\.com[/:]([a-zA-Z0-9\-_]+)/([a-zA-Z0-9\-_.]+)`)
|
2019-05-29 21:49:55 +03:00
|
|
|
|
|
|
|
res := re.FindStringSubmatch(cleanURL)
|
2018-09-21 19:23:46 +03:00
|
|
|
if res == nil {
|
2019-05-29 21:49:55 +03:00
|
|
|
return "", "", ErrBadProjectURL
|
2019-05-25 16:20:30 +03:00
|
|
|
}
|
|
|
|
|
2019-05-29 21:49:55 +03:00
|
|
|
owner = res[1]
|
|
|
|
project = res[2]
|
2019-06-05 01:45:34 +03:00
|
|
|
return
|
2019-05-25 16:20:30 +03:00
|
|
|
}
|
|
|
|
|
2020-02-15 04:55:19 +03:00
|
|
|
func getValidGithubRemoteURLs(repo repository.RepoCommon) ([]string, error) {
|
|
|
|
remotes, err := repo.GetRemotes()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-05-25 16:20:30 +03:00
|
|
|
urls := make([]string, 0, len(remotes))
|
|
|
|
for _, url := range remotes {
|
|
|
|
// split url can work again with shortURL
|
2019-05-29 21:49:55 +03:00
|
|
|
owner, project, err := splitURL(url)
|
2019-05-25 16:20:30 +03:00
|
|
|
if err == nil {
|
2019-05-29 21:49:55 +03:00
|
|
|
shortURL := fmt.Sprintf("%s/%s/%s", "github.com", owner, project)
|
2019-05-25 16:20:30 +03:00
|
|
|
urls = append(urls, shortURL)
|
|
|
|
}
|
2018-09-21 13:54:48 +03:00
|
|
|
}
|
|
|
|
|
2019-11-19 23:12:10 +03:00
|
|
|
sort.Strings(urls)
|
|
|
|
|
2020-02-15 04:55:19 +03:00
|
|
|
return urls, nil
|
2018-09-21 13:54:48 +03:00
|
|
|
}
|
|
|
|
|
2020-02-29 17:04:48 +03:00
|
|
|
func promptLogin() (string, error) {
|
|
|
|
var login string
|
|
|
|
|
|
|
|
validator := func(_ string, value string) (string, error) {
|
|
|
|
ok, fixed, err := validateUsername(value)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
if !ok {
|
|
|
|
return "invalid login", nil
|
|
|
|
}
|
|
|
|
login = fixed
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := input.Prompt("Github login", "login", input.Required, validator)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return login, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func validateUsername(username string) (bool, string, error) {
|
2018-09-21 13:54:48 +03:00
|
|
|
url := fmt.Sprintf("%s/users/%s", githubV3Url, username)
|
|
|
|
|
2019-05-29 21:49:55 +03:00
|
|
|
client := &http.Client{
|
|
|
|
Timeout: defaultTimeout,
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := client.Get(url)
|
2018-09-21 13:54:48 +03:00
|
|
|
if err != nil {
|
2020-02-29 17:04:48 +03:00
|
|
|
return false, "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
|
|
return false, "", nil
|
|
|
|
}
|
|
|
|
|
|
|
|
data, err := ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
return false, "", err
|
2018-09-21 13:54:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
err = resp.Body.Close()
|
|
|
|
if err != nil {
|
2020-02-29 17:04:48 +03:00
|
|
|
return false, "", err
|
2018-09-21 13:54:48 +03:00
|
|
|
}
|
|
|
|
|
2020-02-29 17:04:48 +03:00
|
|
|
var decoded struct {
|
|
|
|
Login string `json:"login"`
|
|
|
|
}
|
|
|
|
err = json.Unmarshal(data, &decoded)
|
|
|
|
if err != nil {
|
|
|
|
return false, "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
if decoded.Login == "" {
|
|
|
|
return false, "", fmt.Errorf("validateUsername: missing login in the response")
|
|
|
|
}
|
|
|
|
|
|
|
|
return true, decoded.Login, nil
|
2018-09-21 13:54:48 +03:00
|
|
|
}
|
|
|
|
|
2019-12-08 23:15:06 +03:00
|
|
|
func validateProject(owner, project string, token *auth.Token) (bool, error) {
|
2019-05-24 22:04:25 +03:00
|
|
|
url := fmt.Sprintf("%s/repos/%s/%s", githubV3Url, owner, project)
|
|
|
|
|
|
|
|
req, err := http.NewRequest("GET", url, nil)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
2019-05-25 16:20:30 +03:00
|
|
|
// need the token for private repositories
|
2019-12-08 23:15:06 +03:00
|
|
|
req.Header.Set("Authorization", fmt.Sprintf("token %s", token.Value))
|
2019-05-24 22:04:25 +03:00
|
|
|
|
|
|
|
client := &http.Client{
|
|
|
|
Timeout: defaultTimeout,
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := client.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = resp.Body.Close()
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp.StatusCode == http.StatusOK, nil
|
|
|
|
}
|
2020-02-10 00:17:10 +03:00
|
|
|
|
|
|
|
func getLoginFromToken(token *auth.Token) (string, error) {
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), defaultTimeout)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
client := buildClient(token)
|
|
|
|
|
|
|
|
var q loginQuery
|
|
|
|
|
|
|
|
err := client.Query(ctx, &q, nil)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
if q.Viewer.Login == "" {
|
|
|
|
return "", fmt.Errorf("github say username is empty")
|
|
|
|
}
|
|
|
|
|
|
|
|
return q.Viewer.Login, nil
|
|
|
|
}
|