2018-12-08 05:58:51 +03:00
|
|
|
package launchpad
|
|
|
|
|
|
|
|
import (
|
2019-05-30 13:50:21 +03:00
|
|
|
"errors"
|
2018-12-08 05:58:51 +03:00
|
|
|
"fmt"
|
2019-05-25 16:56:52 +03:00
|
|
|
"net/http"
|
|
|
|
"regexp"
|
2018-12-08 05:58:51 +03:00
|
|
|
|
|
|
|
"github.com/MichaelMure/git-bug/bridge/core"
|
2019-12-08 23:15:06 +03:00
|
|
|
"github.com/MichaelMure/git-bug/cache"
|
2020-02-04 02:25:27 +03:00
|
|
|
"github.com/MichaelMure/git-bug/input"
|
2018-12-08 05:58:51 +03:00
|
|
|
)
|
|
|
|
|
2019-05-30 13:50:21 +03:00
|
|
|
var ErrBadProjectURL = errors.New("bad Launchpad project URL")
|
|
|
|
|
2019-12-08 23:15:06 +03:00
|
|
|
func (l *Launchpad) Configure(repo *cache.RepoCache, params core.BridgeParams) (core.Configuration, error) {
|
|
|
|
if params.TokenRaw != "" {
|
|
|
|
fmt.Println("warning: token params are ineffective for a Launchpad bridge")
|
2019-05-25 18:27:25 +03:00
|
|
|
}
|
|
|
|
if params.Owner != "" {
|
2019-05-26 13:35:53 +03:00
|
|
|
fmt.Println("warning: --owner is ineffective for a Launchpad bridge")
|
2019-05-25 18:27:25 +03:00
|
|
|
}
|
2019-12-10 22:30:29 +03:00
|
|
|
if params.BaseURL != "" {
|
|
|
|
fmt.Println("warning: --base-url is ineffective for a Launchpad bridge")
|
|
|
|
}
|
2019-05-25 18:27:25 +03:00
|
|
|
|
2018-12-08 05:58:51 +03:00
|
|
|
conf := make(core.Configuration)
|
2019-05-25 16:56:52 +03:00
|
|
|
var err error
|
|
|
|
var project string
|
|
|
|
|
2019-12-08 23:15:06 +03:00
|
|
|
switch {
|
|
|
|
case params.Project != "":
|
2019-05-25 16:56:52 +03:00
|
|
|
project = params.Project
|
2019-12-08 23:15:06 +03:00
|
|
|
case params.URL != "":
|
2019-05-25 16:56:52 +03:00
|
|
|
// get project name from url
|
2019-05-30 13:50:21 +03:00
|
|
|
project, err = splitURL(params.URL)
|
2019-12-08 23:15:06 +03:00
|
|
|
default:
|
2019-05-25 16:56:52 +03:00
|
|
|
// get project name from terminal prompt
|
2020-02-04 02:25:27 +03:00
|
|
|
project, err = input.Prompt("Launchpad project name", "project name", input.Required)
|
2019-12-08 23:15:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2019-05-25 16:56:52 +03:00
|
|
|
}
|
2018-12-08 05:58:51 +03:00
|
|
|
|
2019-05-25 16:56:52 +03:00
|
|
|
// verify project
|
|
|
|
ok, err := validateProject(project)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("project doesn't exist")
|
2019-05-24 22:04:35 +03:00
|
|
|
}
|
2018-12-08 05:58:51 +03:00
|
|
|
|
2019-11-10 19:48:13 +03:00
|
|
|
conf[core.ConfigKeyTarget] = target
|
2019-12-08 23:15:06 +03:00
|
|
|
conf[keyProject] = project
|
2019-08-21 15:24:48 +03:00
|
|
|
|
|
|
|
err = l.ValidateConfig(conf)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-12-08 05:58:51 +03:00
|
|
|
return conf, nil
|
|
|
|
}
|
|
|
|
|
2018-12-17 15:05:05 +03:00
|
|
|
func (*Launchpad) ValidateConfig(conf core.Configuration) error {
|
2019-12-08 23:15:06 +03:00
|
|
|
if v, ok := conf[core.ConfigKeyTarget]; !ok {
|
|
|
|
return fmt.Errorf("missing %s key", core.ConfigKeyTarget)
|
|
|
|
} else if v != target {
|
|
|
|
return fmt.Errorf("unexpected target name: %v", v)
|
2018-12-17 15:05:05 +03:00
|
|
|
}
|
|
|
|
|
2019-12-08 23:15:06 +03:00
|
|
|
if _, ok := conf[keyProject]; !ok {
|
|
|
|
return fmt.Errorf("missing %s key", keyProject)
|
2019-06-17 01:08:02 +03:00
|
|
|
}
|
|
|
|
|
2018-12-17 15:05:05 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-05-25 16:56:52 +03:00
|
|
|
func validateProject(project string) (bool, error) {
|
|
|
|
url := fmt.Sprintf("%s/%s", apiRoot, project)
|
|
|
|
|
2019-05-29 21:49:55 +03:00
|
|
|
client := &http.Client{
|
2019-05-27 21:26:36 +03:00
|
|
|
Timeout: defaultTimeout,
|
|
|
|
}
|
2019-05-29 21:49:55 +03:00
|
|
|
|
2019-05-27 21:26:36 +03:00
|
|
|
resp, err := client.Get(url)
|
2019-05-25 16:56:52 +03:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp.StatusCode == http.StatusOK, nil
|
|
|
|
}
|
|
|
|
|
2019-05-30 13:50:21 +03:00
|
|
|
// extract project name from url
|
|
|
|
func splitURL(url string) (string, error) {
|
|
|
|
re, err := regexp.Compile(`launchpad\.net[\/:]([^\/]*[a-z]+)`)
|
|
|
|
if err != nil {
|
|
|
|
panic("regexp compile:" + err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
res := re.FindStringSubmatch(url)
|
2019-05-25 16:56:52 +03:00
|
|
|
if res == nil {
|
2019-05-30 13:50:21 +03:00
|
|
|
return "", ErrBadProjectURL
|
2019-05-25 16:56:52 +03:00
|
|
|
}
|
|
|
|
|
2019-05-30 13:50:21 +03:00
|
|
|
return res[1], nil
|
2019-05-24 22:04:35 +03:00
|
|
|
}
|