aboutsummaryrefslogtreecommitdiffstats
path: root/bridge/launchpad/config.go
diff options
context:
space:
mode:
authorAmine Hilaly <hilalyamine@gmail.com>2019-05-30 12:50:21 +0200
committerAmine Hilaly <hilalyamine@gmail.com>2019-05-30 12:50:21 +0200
commitebebdfdf35b29b37b5a164d73bc00ca1c2b8e895 (patch)
treece460730f1055afcaa5bdfcc5472354ab672b60d /bridge/launchpad/config.go
parentd064a7127ec612a804e08143c7098bbc60332e83 (diff)
downloadgit-bug-ebebdfdf35b29b37b5a164d73bc00ca1c2b8e895.tar.gz
add unit tests for launchpad bridge configuration
add tests for validateUsername in Github bridge panic when compile regex fail
Diffstat (limited to 'bridge/launchpad/config.go')
-rw-r--r--bridge/launchpad/config.go23
1 files changed, 14 insertions, 9 deletions
diff --git a/bridge/launchpad/config.go b/bridge/launchpad/config.go
index 1514505f..d8efea46 100644
--- a/bridge/launchpad/config.go
+++ b/bridge/launchpad/config.go
@@ -2,6 +2,7 @@ package launchpad
import (
"bufio"
+ "errors"
"fmt"
"net/http"
"os"
@@ -13,15 +14,13 @@ import (
"github.com/MichaelMure/git-bug/repository"
)
+var ErrBadProjectURL = errors.New("bad Launchpad project URL")
+
const (
keyProject = "project"
defaultTimeout = 60 * time.Second
)
-var (
- rxLaunchpadURL = regexp.MustCompile(`launchpad\.net[\/:]([^\/]*[a-z]+)`)
-)
-
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")
@@ -39,7 +38,7 @@ func (*Launchpad) Configure(repo repository.RepoCommon, params core.BridgeParams
} else if params.URL != "" {
// get project name from url
- _, project, err = splitURL(params.URL)
+ project, err = splitURL(params.URL)
if err != nil {
return nil, err
}
@@ -108,11 +107,17 @@ func validateProject(project string) (bool, error) {
return resp.StatusCode == http.StatusOK, nil
}
-func splitURL(url string) (string, string, error) {
- res := rxLaunchpadURL.FindStringSubmatch(url)
+// 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 "", "", fmt.Errorf("bad Launchpad project url")
+ return "", ErrBadProjectURL
}
- return res[0], res[1], nil
+ return res[1], nil
}