2018-12-08 05:58:51 +03:00
|
|
|
package launchpad
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
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"
|
2018-12-08 05:58:51 +03:00
|
|
|
"os"
|
2019-05-25 16:56:52 +03:00
|
|
|
"regexp"
|
2018-12-08 05:58:51 +03:00
|
|
|
"strings"
|
2019-05-29 21:49:55 +03:00
|
|
|
"time"
|
2018-12-08 05:58:51 +03:00
|
|
|
|
|
|
|
"github.com/MichaelMure/git-bug/bridge/core"
|
|
|
|
"github.com/MichaelMure/git-bug/repository"
|
|
|
|
)
|
|
|
|
|
2019-05-30 13:50:21 +03:00
|
|
|
var ErrBadProjectURL = errors.New("bad Launchpad project URL")
|
|
|
|
|
2019-05-29 21:49:55 +03:00
|
|
|
const (
|
2019-06-17 01:08:02 +03:00
|
|
|
target = "launchpad-preview"
|
2019-05-29 21:49:55 +03:00
|
|
|
keyProject = "project"
|
|
|
|
defaultTimeout = 60 * time.Second
|
|
|
|
)
|
2018-12-08 05:58:51 +03:00
|
|
|
|
2019-08-21 15:24:48 +03:00
|
|
|
func (l *Launchpad) Configure(repo repository.RepoCommon, params core.BridgeParams) (core.Configuration, error) {
|
2019-05-25 18:27:25 +03:00
|
|
|
if params.Token != "" {
|
2019-05-26 13:35:53 +03:00
|
|
|
fmt.Println("warning: --token is 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
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
|
if params.Project != "" {
|
|
|
|
project = params.Project
|
|
|
|
|
|
|
|
} else if params.URL != "" {
|
|
|
|
// get project name from url
|
2019-05-30 13:50:21 +03:00
|
|
|
project, err = splitURL(params.URL)
|
2019-05-25 16:56:52 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-12-08 05:58:51 +03:00
|
|
|
|
2019-05-25 16:56:52 +03:00
|
|
|
} else {
|
|
|
|
// get project name from terminal prompt
|
|
|
|
project, err = promptProjectName()
|
2019-05-24 22:04:35 +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-05-25 16:56:52 +03:00
|
|
|
conf[keyProject] = project
|
2019-11-10 19:48:13 +03:00
|
|
|
conf[core.ConfigKeyTarget] = target
|
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 {
|
|
|
|
if _, ok := conf[keyProject]; !ok {
|
|
|
|
return fmt.Errorf("missing %s key", keyProject)
|
|
|
|
}
|
|
|
|
|
2019-11-10 19:48:13 +03:00
|
|
|
if _, ok := conf[core.ConfigKeyTarget]; !ok {
|
|
|
|
return fmt.Errorf("missing %s key", core.ConfigKeyTarget)
|
2019-06-17 01:08:02 +03:00
|
|
|
}
|
|
|
|
|
2018-12-17 15:05:05 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-12-08 05:58:51 +03:00
|
|
|
func promptProjectName() (string, error) {
|
|
|
|
for {
|
|
|
|
fmt.Print("Launchpad project name: ")
|
|
|
|
|
|
|
|
line, err := bufio.NewReader(os.Stdin).ReadString('\n')
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
line = strings.TrimRight(line, "\n")
|
|
|
|
|
|
|
|
if line == "" {
|
|
|
|
fmt.Println("Project name is empty")
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
return line, nil
|
|
|
|
}
|
|
|
|
}
|
2019-05-24 22:04:35 +03:00
|
|
|
|
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
|
|
|
}
|