aboutsummaryrefslogtreecommitdiffstats
path: root/bridge/launchpad
diff options
context:
space:
mode:
authorMichael Muré <batolettre@gmail.com>2019-06-07 02:53:21 +0200
committerGitHub <noreply@github.com>2019-06-07 02:53:21 +0200
commitd69dcce806d280ddbd6a4fb17700153bc03da90b (patch)
treeda07a7beadc77b3fe5e99f09bd38d919566a0191 /bridge/launchpad
parenta9629dbad15f0e80ea244eb81abda4ddc08f7a0e (diff)
parent1c2ad95960c09d029e6306ac5a5ea76c58e8b5c9 (diff)
downloadgit-bug-d69dcce806d280ddbd6a4fb17700153bc03da90b.tar.gz
Merge pull request #153 from A-Hilaly/bridge-configuration
[Breaking] Bridge configuration enhancements
Diffstat (limited to 'bridge/launchpad')
-rw-r--r--bridge/launchpad/config.go79
-rw-r--r--bridge/launchpad/config_test.go93
2 files changed, 167 insertions, 5 deletions
diff --git a/bridge/launchpad/config.go b/bridge/launchpad/config.go
index 11a465be..d8efea46 100644
--- a/bridge/launchpad/config.go
+++ b/bridge/launchpad/config.go
@@ -2,26 +2,65 @@ package launchpad
import (
"bufio"
+ "errors"
"fmt"
+ "net/http"
"os"
+ "regexp"
"strings"
+ "time"
"github.com/MichaelMure/git-bug/bridge/core"
"github.com/MichaelMure/git-bug/repository"
)
-const keyProject = "project"
+var ErrBadProjectURL = errors.New("bad Launchpad project URL")
+
+const (
+ keyProject = "project"
+ defaultTimeout = 60 * time.Second
+)
+
+func (*Launchpad) Configure(repo repository.RepoCommon, params core.BridgeParams) (core.Configuration, error) {
+ if params.Token != "" {
+ fmt.Println("warning: --token is ineffective for a Launchpad bridge")
+ }
+ if params.Owner != "" {
+ fmt.Println("warning: --owner is ineffective for a Launchpad bridge")
+ }
-func (*Launchpad) Configure(repo repository.RepoCommon) (core.Configuration, error) {
conf := make(core.Configuration)
+ var err error
+ var project string
+
+ if params.Project != "" {
+ project = params.Project
- projectName, err := promptProjectName()
+ } else if params.URL != "" {
+ // get project name from url
+ project, err = splitURL(params.URL)
+ if err != nil {
+ return nil, err
+ }
+
+ } else {
+ // get project name from terminal prompt
+ project, err = promptProjectName()
+ if err != nil {
+ return nil, err
+ }
+ }
+
+ // verify project
+ ok, err := validateProject(project)
if err != nil {
return nil, err
}
+ if !ok {
+ return nil, fmt.Errorf("project doesn't exist")
+ }
- conf[keyProject] = projectName
-
+ conf[keyProject] = project
return conf, nil
}
@@ -52,3 +91,33 @@ func promptProjectName() (string, error) {
return line, nil
}
}
+
+func validateProject(project string) (bool, error) {
+ url := fmt.Sprintf("%s/%s", apiRoot, project)
+
+ client := &http.Client{
+ Timeout: defaultTimeout,
+ }
+
+ resp, err := client.Get(url)
+ if err != nil {
+ return false, err
+ }
+
+ return resp.StatusCode == http.StatusOK, nil
+}
+
+// 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)
+ if res == nil {
+ return "", ErrBadProjectURL
+ }
+
+ return res[1], nil
+}
diff --git a/bridge/launchpad/config_test.go b/bridge/launchpad/config_test.go
new file mode 100644
index 00000000..275c0d24
--- /dev/null
+++ b/bridge/launchpad/config_test.go
@@ -0,0 +1,93 @@
+package launchpad
+
+import (
+ "testing"
+
+ "github.com/stretchr/testify/assert"
+)
+
+func TestSplitURL(t *testing.T) {
+ type args struct {
+ url string
+ }
+ type want struct {
+ project string
+ err error
+ }
+ tests := []struct {
+ name string
+ args args
+ want want
+ }{
+ {
+ name: "default project url",
+ args: args{
+ url: "https://launchpad.net/ubuntu",
+ },
+ want: want{
+ project: "ubuntu",
+ err: nil,
+ },
+ },
+ {
+ name: "project bugs url",
+ args: args{
+ url: "https://bugs.launchpad.net/ubuntu",
+ },
+ want: want{
+ project: "ubuntu",
+ err: nil,
+ },
+ },
+ {
+ name: "bad url",
+ args: args{
+ url: "https://launchpa.net/ubuntu",
+ },
+ want: want{
+ err: ErrBadProjectURL,
+ },
+ },
+ }
+
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ project, err := splitURL(tt.args.url)
+ assert.Equal(t, tt.want.err, err)
+ assert.Equal(t, tt.want.project, project)
+ })
+ }
+}
+
+func TestValidateProject(t *testing.T) {
+ type args struct {
+ project string
+ }
+ tests := []struct {
+ name string
+ args args
+ want bool
+ }{
+ {
+ name: "public project",
+ args: args{
+ project: "ubuntu",
+ },
+ want: true,
+ },
+ {
+ name: "non existing project",
+ args: args{
+ project: "cant-find-this",
+ },
+ want: false,
+ },
+ }
+
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ ok, _ := validateProject(tt.args.project)
+ assert.Equal(t, tt.want, ok)
+ })
+ }
+}