2019-07-09 23:58:47 +03:00
|
|
|
package gitlab
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2019-07-22 19:56:14 +03:00
|
|
|
"net/url"
|
2020-01-03 15:53:59 +03:00
|
|
|
"path"
|
2019-07-09 23:58:47 +03:00
|
|
|
"regexp"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/xanzy/go-gitlab"
|
|
|
|
|
|
|
|
"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-02-04 02:25:27 +03:00
|
|
|
"github.com/MichaelMure/git-bug/input"
|
2019-07-09 23:58:47 +03:00
|
|
|
"github.com/MichaelMure/git-bug/repository"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
ErrBadProjectURL = errors.New("bad project url")
|
|
|
|
)
|
|
|
|
|
2020-02-15 04:55:19 +03:00
|
|
|
func (g *Gitlab) ValidParams() map[string]interface{} {
|
|
|
|
return map[string]interface{}{
|
|
|
|
"URL": nil,
|
|
|
|
"BaseURL": nil,
|
|
|
|
"Login": nil,
|
|
|
|
"CredPrefix": nil,
|
|
|
|
"TokenRaw": nil,
|
2019-07-09 23:58:47 +03:00
|
|
|
}
|
2020-02-15 04:55:19 +03:00
|
|
|
}
|
2019-07-09 23:58:47 +03:00
|
|
|
|
2020-02-15 04:55:19 +03:00
|
|
|
func (g *Gitlab) Configure(repo *cache.RepoCache, params core.BridgeParams) (core.Configuration, error) {
|
2019-07-09 23:58:47 +03:00
|
|
|
var err error
|
2019-12-26 22:16:18 +03:00
|
|
|
var baseUrl string
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case params.BaseURL != "":
|
|
|
|
baseUrl = params.BaseURL
|
|
|
|
default:
|
2020-02-15 04:55:19 +03:00
|
|
|
baseUrl, err = input.PromptDefault("Gitlab server URL", "URL", defaultBaseURL, input.Required, input.IsURL)
|
2019-12-26 22:16:18 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "base url prompt")
|
|
|
|
}
|
2019-12-10 22:30:29 +03:00
|
|
|
}
|
|
|
|
|
2020-02-10 00:17:10 +03:00
|
|
|
var projectURL string
|
2019-12-08 23:15:06 +03:00
|
|
|
|
2019-07-09 23:58:47 +03:00
|
|
|
// get project url
|
2019-12-08 23:15:06 +03:00
|
|
|
switch {
|
|
|
|
case params.URL != "":
|
2020-02-10 00:17:10 +03:00
|
|
|
projectURL = params.URL
|
2019-12-08 23:15:06 +03:00
|
|
|
default:
|
2019-07-09 23:58:47 +03:00
|
|
|
// terminal prompt
|
2020-02-10 00:17:10 +03:00
|
|
|
projectURL, err = promptProjectURL(repo, baseUrl)
|
2019-07-09 23:58:47 +03:00
|
|
|
if err != nil {
|
2019-07-22 19:56:14 +03:00
|
|
|
return nil, errors.Wrap(err, "url prompt")
|
2019-07-09 23:58:47 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-10 00:17:10 +03:00
|
|
|
if !strings.HasPrefix(projectURL, params.BaseURL) {
|
|
|
|
return nil, fmt.Errorf("base URL (%s) doesn't match the project URL (%s)", params.BaseURL, projectURL)
|
2019-12-10 22:30:29 +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)
|
2019-11-24 16:39:02 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
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(baseUrl, token)
|
2020-02-04 02:25:27 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
token.SetMetadata(auth.MetaKeyLogin, login)
|
2020-02-10 00:17:10 +03:00
|
|
|
token.SetMetadata(auth.MetaKeyBaseURL, baseUrl)
|
2020-02-04 02:25:27 +03:00
|
|
|
cred = token
|
2019-12-08 23:15:06 +03:00
|
|
|
default:
|
2020-03-28 22:10:52 +03:00
|
|
|
if params.Login == "" {
|
2020-02-10 00:17:10 +03:00
|
|
|
// TODO: validate username
|
|
|
|
login, err = input.Prompt("Gitlab login", "login", input.Required)
|
2020-03-28 22:10:52 +03:00
|
|
|
} else {
|
|
|
|
// TODO: validate username
|
|
|
|
login = params.Login
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2020-02-10 00:17:10 +03:00
|
|
|
}
|
|
|
|
cred, err = promptTokenOptions(repo, login, baseUrl)
|
2019-11-24 16:39:02 +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 Gitlab bridge only handle token credentials")
|
2019-11-24 16:39:02 +03:00
|
|
|
}
|
|
|
|
|
2019-07-23 18:10:07 +03:00
|
|
|
// validate project url and get its ID
|
2020-02-10 00:17:10 +03:00
|
|
|
id, err := validateProjectURL(baseUrl, projectURL, token)
|
2019-07-09 23:58:47 +03:00
|
|
|
if err != nil {
|
2019-07-22 19:56:14 +03:00
|
|
|
return nil, errors.Wrap(err, "project validation")
|
2019-07-09 23:58:47 +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[confKeyProjectID] = strconv.Itoa(id)
|
|
|
|
conf[confKeyGitlabBaseUrl] = baseUrl
|
2020-02-23 16:05:03 +03:00
|
|
|
conf[confKeyDefaultLogin] = login
|
2019-07-09 23:58:47 +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-02-10 00:17:10 +03:00
|
|
|
return conf, core.FinishConfig(repo, metaKeyGitlabLogin, login)
|
2019-07-09 23:58:47 +03:00
|
|
|
}
|
|
|
|
|
2019-08-21 15:24:48 +03:00
|
|
|
func (g *Gitlab) 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-07-09 23:58:47 +03:00
|
|
|
} else if v != target {
|
|
|
|
return fmt.Errorf("unexpected target name: %v", v)
|
|
|
|
}
|
2020-02-15 04:55:19 +03:00
|
|
|
if _, ok := conf[confKeyGitlabBaseUrl]; !ok {
|
|
|
|
return fmt.Errorf("missing %s key", confKeyGitlabBaseUrl)
|
2019-12-26 22:16:18 +03:00
|
|
|
}
|
2020-02-15 04:55:19 +03:00
|
|
|
if _, ok := conf[confKeyProjectID]; !ok {
|
|
|
|
return fmt.Errorf("missing %s key", confKeyProjectID)
|
2019-07-09 23:58:47 +03:00
|
|
|
}
|
2020-02-23 16:05:03 +03:00
|
|
|
if _, ok := conf[confKeyDefaultLogin]; !ok {
|
|
|
|
return fmt.Errorf("missing %s key", confKeyDefaultLogin)
|
|
|
|
}
|
2019-07-09 23:58:47 +03:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-07-28 13:56:46 +03:00
|
|
|
func promptTokenOptions(repo repository.RepoKeyring, login, baseUrl 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),
|
|
|
|
auth.WithMeta(auth.MetaKeyBaseURL, baseUrl),
|
|
|
|
)
|
2020-02-04 02:25:27 +03:00
|
|
|
if err != nil {
|
2020-02-15 04:55:19 +03:00
|
|
|
return nil, err
|
2019-12-26 22:16:18 +03:00
|
|
|
}
|
|
|
|
|
2020-02-15 15:45:14 +03:00
|
|
|
cred, index, err := input.PromptCredential(target, "token", creds, []string{
|
|
|
|
"enter my token",
|
|
|
|
})
|
|
|
|
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(baseUrl)
|
|
|
|
default:
|
2020-02-15 15:45:14 +03:00
|
|
|
panic("missed case")
|
2019-11-24 16:39:02 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-04 02:25:27 +03:00
|
|
|
func promptToken(baseUrl string) (*auth.Token, error) {
|
2020-01-03 15:53:59 +03:00
|
|
|
fmt.Printf("You can generate a new token by visiting %s.\n", path.Join(baseUrl, "profile/personal_access_tokens"))
|
2019-07-19 19:49:28 +03:00
|
|
|
fmt.Println("Choose 'Create personal access token' and set the necessary access scope for your repository.")
|
2019-07-09 23:58:47 +03:00
|
|
|
fmt.Println()
|
2019-07-19 19:49:28 +03:00
|
|
|
fmt.Println("'api' access scope: to be able to make api calls")
|
2019-07-09 23:58:47 +03:00
|
|
|
fmt.Println()
|
|
|
|
|
2020-02-23 16:23:34 +03:00
|
|
|
re := regexp.MustCompile(`^[a-zA-Z0-9\-\_]{20}$`)
|
2019-07-09 23:58:47 +03:00
|
|
|
|
2020-02-04 02:25:27 +03:00
|
|
|
var login string
|
2019-07-09 23:58:47 +03:00
|
|
|
|
2020-02-04 02:25:27 +03:00
|
|
|
validator := func(name string, value string) (complaint string, err error) {
|
|
|
|
if !re.MatchString(value) {
|
|
|
|
return "token has incorrect format", nil
|
|
|
|
}
|
2020-02-12 20:32:01 +03:00
|
|
|
login, err = getLoginFromToken(baseUrl, auth.NewToken(target, value))
|
2019-07-09 23:58:47 +03:00
|
|
|
if err != nil {
|
2020-02-04 02:25:27 +03:00
|
|
|
return fmt.Sprintf("token is invalid: %v", err), nil
|
2019-07-09 23:58:47 +03:00
|
|
|
}
|
2020-02-04 02:25:27 +03:00
|
|
|
return "", nil
|
|
|
|
}
|
2019-07-09 23:58:47 +03:00
|
|
|
|
2020-02-04 02:25:27 +03:00
|
|
|
rawToken, err := input.Prompt("Enter token", "token", input.Required, validator)
|
2020-02-09 00:08:35 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-07-09 23:58:47 +03:00
|
|
|
|
2020-02-12 20:32:01 +03:00
|
|
|
token := auth.NewToken(target, rawToken)
|
2020-02-04 02:25:27 +03:00
|
|
|
token.SetMetadata(auth.MetaKeyLogin, login)
|
2020-02-10 00:17:10 +03:00
|
|
|
token.SetMetadata(auth.MetaKeyBaseURL, baseUrl)
|
2020-02-04 02:25:27 +03:00
|
|
|
|
|
|
|
return token, nil
|
2019-07-09 23:58:47 +03:00
|
|
|
}
|
|
|
|
|
2020-02-10 00:17:10 +03:00
|
|
|
func promptProjectURL(repo repository.RepoCommon, baseUrl string) (string, error) {
|
2020-02-15 04:55:19 +03:00
|
|
|
validRemotes, err := getValidGitlabRemoteURLs(repo, baseUrl)
|
2019-12-08 23:15:06 +03:00
|
|
|
if err != nil {
|
2020-02-15 04:55:19 +03:00
|
|
|
return "", err
|
2019-07-09 23:58:47 +03:00
|
|
|
}
|
|
|
|
|
2020-02-15 04:55:19 +03:00
|
|
|
return input.PromptURLWithRemote("Gitlab project URL", "URL", validRemotes, input.Required)
|
2019-07-09 23:58:47 +03:00
|
|
|
}
|
|
|
|
|
2019-12-26 22:16:18 +03:00
|
|
|
func getProjectPath(baseUrl, projectUrl string) (string, error) {
|
2019-07-22 19:56:14 +03:00
|
|
|
cleanUrl := strings.TrimSuffix(projectUrl, ".git")
|
2019-07-19 19:49:28 +03:00
|
|
|
cleanUrl = strings.Replace(cleanUrl, "git@", "https://", 1)
|
2019-07-22 19:56:14 +03:00
|
|
|
objectUrl, err := url.Parse(cleanUrl)
|
2019-07-09 23:58:47 +03:00
|
|
|
if err != nil {
|
2019-07-22 19:56:14 +03:00
|
|
|
return "", ErrBadProjectURL
|
2019-07-09 23:58:47 +03:00
|
|
|
}
|
|
|
|
|
2019-12-26 22:16:18 +03:00
|
|
|
objectBaseUrl, err := url.Parse(baseUrl)
|
|
|
|
if err != nil {
|
|
|
|
return "", ErrBadProjectURL
|
|
|
|
}
|
|
|
|
|
|
|
|
if objectUrl.Hostname() != objectBaseUrl.Hostname() {
|
|
|
|
return "", fmt.Errorf("base url and project url hostnames doesn't match")
|
|
|
|
}
|
2019-07-10 01:41:43 +03:00
|
|
|
return objectUrl.Path[1:], nil
|
2019-07-09 23:58:47 +03:00
|
|
|
}
|
|
|
|
|
2020-02-15 04:55:19 +03:00
|
|
|
func getValidGitlabRemoteURLs(repo repository.RepoCommon, baseUrl string) ([]string, error) {
|
|
|
|
remotes, err := repo.GetRemotes()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-07-09 23:58:47 +03:00
|
|
|
urls := make([]string, 0, len(remotes))
|
|
|
|
for _, u := range remotes {
|
2020-02-23 16:05:03 +03:00
|
|
|
p, err := getProjectPath(baseUrl, u)
|
2019-07-09 23:58:47 +03:00
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-02-23 16:05:03 +03:00
|
|
|
urls = append(urls, fmt.Sprintf("%s/%s", baseUrl, p))
|
2019-07-09 23:58:47 +03:00
|
|
|
}
|
|
|
|
|
2020-02-15 04:55:19 +03:00
|
|
|
return urls, nil
|
2019-07-09 23:58:47 +03:00
|
|
|
}
|
|
|
|
|
2019-12-26 22:16:18 +03:00
|
|
|
func validateProjectURL(baseUrl, url string, token *auth.Token) (int, error) {
|
|
|
|
projectPath, err := getProjectPath(baseUrl, url)
|
2019-07-09 23:58:47 +03:00
|
|
|
if err != nil {
|
2019-07-23 18:29:53 +03:00
|
|
|
return 0, err
|
2019-07-09 23:58:47 +03:00
|
|
|
}
|
|
|
|
|
2019-12-26 22:16:18 +03:00
|
|
|
client, err := buildClient(baseUrl, token)
|
2019-12-10 22:30:29 +03:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
2019-07-23 18:29:53 +03:00
|
|
|
|
2019-07-10 01:41:43 +03:00
|
|
|
project, _, err := client.Projects.GetProject(projectPath, &gitlab.GetProjectOptions{})
|
2019-07-09 23:58:47 +03:00
|
|
|
if err != nil {
|
2020-02-04 02:25:27 +03:00
|
|
|
return 0, errors.Wrap(err, "wrong token scope ou non-existent project")
|
2019-07-09 23:58:47 +03:00
|
|
|
}
|
|
|
|
|
2019-07-23 18:29:53 +03:00
|
|
|
return project.ID, nil
|
2019-07-09 23:58:47 +03:00
|
|
|
}
|
2020-02-04 02:25:27 +03:00
|
|
|
|
|
|
|
func getLoginFromToken(baseUrl string, token *auth.Token) (string, error) {
|
|
|
|
client, err := buildClient(baseUrl, token)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
user, _, err := client.Users.CurrentUser()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
if user.Username == "" {
|
|
|
|
return "", fmt.Errorf("gitlab say username is empty")
|
|
|
|
}
|
|
|
|
|
|
|
|
return user.Username, nil
|
|
|
|
}
|