diff options
Diffstat (limited to 'bridge/github')
-rw-r--r-- | bridge/github/config.go | 112 | ||||
-rw-r--r-- | bridge/github/config_test.go | 4 | ||||
-rw-r--r-- | bridge/github/export_test.go | 2 | ||||
-rw-r--r-- | bridge/github/import_query.go | 14 | ||||
-rw-r--r-- | bridge/github/import_test.go | 2 |
5 files changed, 87 insertions, 47 deletions
diff --git a/bridge/github/config.go b/bridge/github/config.go index 9477801d..afb8086c 100644 --- a/bridge/github/config.go +++ b/bridge/github/config.go @@ -3,6 +3,7 @@ package github import ( "bufio" "bytes" + "context" "encoding/json" "fmt" "io" @@ -70,25 +71,7 @@ func (g *Github) Configure(repo *cache.RepoCache, params core.BridgeParams) (cor return nil, fmt.Errorf("invalid parameter owner: %v", owner) } - login := params.Login - if login == "" { - validator := func(name string, value string) (string, error) { - ok, err := validateUsername(value) - if err != nil { - return "", err - } - if !ok { - return "invalid login", nil - } - return "", nil - } - - login, err = input.Prompt("Github login", "login", input.Required, validator) - if err != nil { - return nil, err - } - } - + var login string var cred auth.Credential switch { @@ -97,10 +80,27 @@ func (g *Github) Configure(repo *cache.RepoCache, params core.BridgeParams) (cor if err != nil { return nil, err } + l, ok := cred.GetMetadata(auth.MetaKeyLogin) + if !ok { + return nil, fmt.Errorf("credential doesn't have a login") + } + login = l case params.TokenRaw != "": - cred = auth.NewToken(params.TokenRaw, target) - cred.SetMetadata(auth.MetaKeyLogin, login) + token := auth.NewToken(target, params.TokenRaw) + login, err = getLoginFromToken(token) + if err != nil { + return nil, err + } + token.SetMetadata(auth.MetaKeyLogin, login) + cred = token default: + login = params.Login + if login == "" { + login, err = input.Prompt("Github login", "login", input.Required, usernameValidator) + if err != nil { + return nil, err + } + } cred, err = promptTokenOptions(repo, login, owner, project) if err != nil { return nil, err @@ -159,6 +159,17 @@ func (*Github) ValidateConfig(conf core.Configuration) error { return nil } +func usernameValidator(name string, value string) (string, error) { + ok, err := validateUsername(value) + if err != nil { + return "", err + } + if !ok { + return "invalid login", nil + } + return "", nil +} + func requestToken(note, login, password string, scope string) (*http.Response, error) { return requestTokenWith2FA(note, login, password, "", scope) } @@ -231,7 +242,11 @@ func randomFingerprint() string { func promptTokenOptions(repo repository.RepoConfig, login, owner, project string) (auth.Credential, error) { for { - creds, err := auth.List(repo, auth.WithTarget(target), auth.WithMeta(auth.MetaKeyLogin, login)) + creds, err := auth.List(repo, + auth.WithTarget(target), + auth.WithKind(auth.KindToken), + auth.WithMeta(auth.MetaKeyLogin, login), + ) if err != nil { return nil, err } @@ -275,19 +290,13 @@ func promptTokenOptions(repo repository.RepoConfig, login, owner, project string switch index { case 1: - value, err := promptToken() - if err != nil { - return nil, err - } - token := auth.NewToken(value, target) - token.SetMetadata(auth.MetaKeyLogin, login) - return token, nil + return promptToken() case 2: value, err := loginAndRequestToken(login, owner, project) if err != nil { return nil, err } - token := auth.NewToken(value, target) + token := auth.NewToken(target, value) token.SetMetadata(auth.MetaKeyLogin, login) return token, nil default: @@ -296,7 +305,7 @@ func promptTokenOptions(repo repository.RepoConfig, login, owner, project string } } -func promptToken() (string, error) { +func promptToken() (*auth.Token, error) { 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() @@ -312,14 +321,28 @@ func promptToken() (string, error) { panic("regexp compile:" + err.Error()) } + var login string + validator := func(name string, value string) (complaint string, err error) { - if re.MatchString(value) { - return "", nil + if !re.MatchString(value) { + return "token has incorrect format", nil + } + login, err = getLoginFromToken(auth.NewToken(target, value)) + if err != nil { + return fmt.Sprintf("token is invalid: %v", err), nil } - return "token has incorrect format", nil + return "", nil + } + + rawToken, err := input.Prompt("Enter token", "token", input.Required, validator) + if err != nil { + return nil, err } - return input.Prompt("Enter token", "token", input.Required, validator) + token := auth.NewToken(target, rawToken) + token.SetMetadata(auth.MetaKeyLogin, login) + + return token, nil } func loginAndRequestToken(login, owner, project string) (string, error) { @@ -543,3 +566,22 @@ func validateProject(owner, project string, token *auth.Token) (bool, error) { return resp.StatusCode == http.StatusOK, nil } + +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 +} diff --git a/bridge/github/config_test.go b/bridge/github/config_test.go index d7b1b38d..fe54c209 100644 --- a/bridge/github/config_test.go +++ b/bridge/github/config_test.go @@ -154,8 +154,8 @@ func TestValidateProject(t *testing.T) { t.Skip("Env var GITHUB_TOKEN_PUBLIC missing") } - tokenPrivate := auth.NewToken(envPrivate, target) - tokenPublic := auth.NewToken(envPublic, target) + tokenPrivate := auth.NewToken(target, envPrivate) + tokenPublic := auth.NewToken(target, envPublic) type args struct { owner string diff --git a/bridge/github/export_test.go b/bridge/github/export_test.go index 7d6e6fb1..56e29835 100644 --- a/bridge/github/export_test.go +++ b/bridge/github/export_test.go @@ -157,7 +157,7 @@ func TestPushPull(t *testing.T) { defer backend.Close() interrupt.RegisterCleaner(backend.Close) - token := auth.NewToken(envToken, target) + token := auth.NewToken(target, envToken) token.SetMetadata(auth.MetaKeyLogin, login) err = auth.Store(repo, token) require.NoError(t, err) diff --git a/bridge/github/import_query.go b/bridge/github/import_query.go index f5cad299..58f6d95e 100644 --- a/bridge/github/import_query.go +++ b/bridge/github/import_query.go @@ -168,14 +168,6 @@ type ghostQuery struct { } `graphql:"user(login: $login)"` } -type labelQuery struct { - Repository struct { - Label struct { - ID string `graphql:"id"` - } `graphql:"label(name: $label)"` - } `graphql:"repository(owner: $owner, name: $name)"` -} - type labelsQuery struct { Repository struct { Labels struct { @@ -189,3 +181,9 @@ type labelsQuery struct { } `graphql:"labels(first: $first, after: $after)"` } `graphql:"repository(owner: $owner, name: $name)"` } + +type loginQuery struct { + Viewer struct { + Login string `graphql:"login"` + } `graphql:"viewer"` +} diff --git a/bridge/github/import_test.go b/bridge/github/import_test.go index a8f8e346..7eb901d3 100644 --- a/bridge/github/import_test.go +++ b/bridge/github/import_test.go @@ -144,7 +144,7 @@ func Test_Importer(t *testing.T) { login := "test-identity" author.SetMetadata(metaKeyGithubLogin, login) - token := auth.NewToken(envToken, target) + token := auth.NewToken(target, envToken) token.SetMetadata(auth.MetaKeyLogin, login) err = auth.Store(repo, token) require.NoError(t, err) |