aboutsummaryrefslogtreecommitdiffstats
path: root/bridge/launchpad
diff options
context:
space:
mode:
authorAmine Hilaly <hilalyamine@gmail.com>2019-05-25 15:56:52 +0200
committerAmine Hilaly <hilalyamine@gmail.com>2019-05-29 22:58:01 +0200
commit0de2bd92b034ec265dbc66751626b4db67983760 (patch)
tree295e60b84216e06d55a48d590de6fff9a1dc4497 /bridge/launchpad
parent46ce1059b6977d79b37477f02f2a5028ed4f09b0 (diff)
downloadgit-bug-0de2bd92b034ec265dbc66751626b4db67983760.tar.gz
Launchpad bridge configuration from `BridgeParams` Project and URL
Improve Github config comments
Diffstat (limited to 'bridge/launchpad')
-rw-r--r--bridge/launchpad/config.go53
1 files changed, 48 insertions, 5 deletions
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
}