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")
|
|
|
|
|
2020-02-15 04:55:19 +03:00
|
|
|
func (Launchpad) ValidParams() map[string]interface{} {
|
|
|
|
return map[string]interface{}{
|
|
|
|
"URL": nil,
|
|
|
|
"Project": nil,
|
2019-12-10 22:30:29 +03:00
|
|
|
}
|
2020-02-15 04:55:19 +03:00
|
|
|
}
|
2019-05-25 18:27:25 +03:00
|
|
|
|
2020-02-15 04:55:19 +03:00
|
|
|
func (l *Launchpad) Configure(repo *cache.RepoCache, params core.BridgeParams) (core.Configuration, error) {
|
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
|
|
|
|
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[confKeyProject] = 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
|
|
|
}
|
2020-02-15 04:55:19 +03:00
|
|
|
if _, ok := conf[confKeyProject]; !ok {
|
|
|
|
return fmt.Errorf("missing %s key", confKeyProject)
|
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
|
|
|
|
}
|
|
|
|
|
2020-02-29 17:04:48 +03:00
|
|
|
_ = resp.Body.Close()
|
|
|
|
|
2019-05-25 16:56:52 +03:00
|
|
|
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) {
|
2020-02-23 16:23:34 +03:00
|
|
|
re := regexp.MustCompile(`launchpad\.net[\/:]([^\/]*[a-z]+)`)
|
2019-05-30 13:50:21 +03:00
|
|
|
|
|
|
|
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
|
|
|
}
|