aboutsummaryrefslogtreecommitdiffstats
path: root/bridge
diff options
context:
space:
mode:
Diffstat (limited to 'bridge')
-rw-r--r--bridge/github/config.go8
-rw-r--r--bridge/launchpad/config.go53
2 files changed, 53 insertions, 8 deletions
diff --git a/bridge/github/config.go b/bridge/github/config.go
index f2ac750e..e723fda6 100644
--- a/bridge/github/config.go
+++ b/bridge/github/config.go
@@ -42,25 +42,27 @@ func (*Github) Configure(repo repository.RepoCommon, params core.BridgeParams) (
var owner string
var project string
- // getting owner and project name:
- // first use directly params if they are both provided, else try to parse
- // them from params URL, and finaly try getting them from terminal prompt
+ // getting owner and project name
if params.Owner != "" && params.Project != "" {
+ // first try to use params if they are both provided
owner = params.Owner
project = params.Project
} else if params.URL != "" {
+ // try to parse them from params URL
_, owner, project, err = splitURL(params.URL)
if err != nil {
return nil, err
}
} else {
+ // remote suggestions
remotes, err := repo.GetRemotes()
if err != nil {
return nil, err
}
+ // try terminal prompt
owner, project, err = promptURL(remotes)
if err != nil {
return nil, err
diff --git a/bridge/launchpad/config.go b/bridge/launchpad/config.go
index 637ffd2c..1c072ad3 100644
--- a/bridge/launchpad/config.go
+++ b/bridge/launchpad/config.go
@@ -3,7 +3,9 @@ package launchpad
import (
"bufio"
"fmt"
+ "net/http"
"os"
+ "regexp"
"strings"
"github.com/MichaelMure/git-bug/bridge/core"
@@ -12,18 +14,43 @@ import (
const keyProject = "project"
+var (
+ rxLaunchpadURL = regexp.MustCompile(`launchpad\.net[\/:]([^\/]*[a-z]+)`)
+)
+
func (*Launchpad) Configure(repo repository.RepoCommon, params core.BridgeParams) (core.Configuration, error) {
conf := make(core.Configuration)
+ var err error
+ var project string
+
+ if params.Project != "" {
+ project = params.Project
+
+ } else if params.URL != "" {
+ // get project name from url
+ _, project, err = splitURL(params.URL)
+ if err != nil {
+ return nil, err
+ }
- if params.Project == "" {
- projectName, err := promptProjectName()
+ } else {
+ // get project name from terminal prompt
+ project, err = promptProjectName()
if err != nil {
return nil, err
}
+ }
- conf[keyProject] = projectName
+ // verify project
+ ok, err := validateProject(project)
+ if err != nil {
+ return nil, err
+ }
+ if !ok {
+ return nil, fmt.Errorf("project doesn't exist")
}
+ conf[keyProject] = project
return conf, nil
}
@@ -55,6 +82,22 @@ func promptProjectName() (string, error) {
}
}
-func validateProject() (bool, error) {
- return false, nil
+func validateProject(project string) (bool, error) {
+ url := fmt.Sprintf("%s/%s", apiRoot, project)
+
+ resp, err := http.Get(url)
+ if err != nil {
+ return false, err
+ }
+
+ return resp.StatusCode == http.StatusOK, nil
+}
+
+func splitURL(url string) (string, string, error) {
+ res := rxLaunchpadURL.FindStringSubmatch(url)
+ if res == nil {
+ return "", "", fmt.Errorf("bad Launchpad project url")
+ }
+
+ return res[0], res[1], nil
}