bridge/gitlab: change validateProjectURL signature

Amine Hilaly created

bridge/gitlab: code cleanup

Change summary

bridge/core/bridge.go     |  4 +++-
bridge/gitlab/config.go   | 26 +++++++++++---------------
bridge/gitlab/export.go   |  3 +--
bridge/gitlab/gitlab.go   | 12 ++++++------
bridge/gitlab/import.go   |  6 +++---
bridge/gitlab/iterator.go |  8 --------
6 files changed, 24 insertions(+), 35 deletions(-)

Detailed changes

bridge/core/bridge.go 🔗

@@ -19,7 +19,9 @@ var ErrImportNotSupported = errors.New("import is not supported")
 var ErrExportNotSupported = errors.New("export is not supported")
 
 const (
-	KeyTarget             = "target"
+	KeyTarget = "target"
+	KeyOrigin = "origin"
+
 	bridgeConfigKeyPrefix = "git-bug.bridge"
 )
 

bridge/gitlab/config.go 🔗

@@ -61,26 +61,22 @@ func (*Gitlab) Configure(repo repository.RepoCommon, params core.BridgeParams) (
 		}
 	}
 
-	var ok bool
 	// validate project url and get its ID
-	ok, id, err := validateProjectURL(url, token)
+	id, err := validateProjectURL(url, token)
 	if err != nil {
 		return nil, errors.Wrap(err, "project validation")
 	}
-	if !ok {
-		return nil, fmt.Errorf("invalid project id or incorrect token scope")
-	}
 
 	conf[keyProjectID] = strconv.Itoa(id)
 	conf[keyToken] = token
-	conf[keyTarget] = target
+	conf[core.KeyTarget] = target
 
 	return conf, nil
 }
 
 func (*Gitlab) ValidateConfig(conf core.Configuration) error {
-	if v, ok := conf[keyTarget]; !ok {
-		return fmt.Errorf("missing %s key", keyTarget)
+	if v, ok := conf[core.KeyTarget]; !ok {
+		return fmt.Errorf("missing %s key", core.KeyTarget)
 	} else if v != target {
 		return fmt.Errorf("unexpected target name: %v", v)
 	}
@@ -147,7 +143,7 @@ func promptURL(remotes map[string]string) (string, error) {
 			line = strings.TrimRight(line, "\n")
 
 			index, err := strconv.Atoi(line)
-			if err != nil || (index < 0 && index > len(validRemotes)) {
+			if err != nil || index < 0 || index > len(validRemotes) {
 				fmt.Println("invalid input")
 				continue
 			}
@@ -205,18 +201,18 @@ func getValidGitlabRemoteURLs(remotes map[string]string) []string {
 	return urls
 }
 
-func validateProjectURL(url, token string) (bool, int, error) {
-	client := buildClient(token)
-
+func validateProjectURL(url, token string) (int, error) {
 	projectPath, err := getProjectPath(url)
 	if err != nil {
-		return false, 0, err
+		return 0, err
 	}
 
+	client := buildClient(token)
+
 	project, _, err := client.Projects.GetProject(projectPath, &gitlab.GetProjectOptions{})
 	if err != nil {
-		return false, 0, err
+		return 0, err
 	}
 
-	return true, project.ID, nil
+	return project.ID, nil
 }

bridge/gitlab/export.go 🔗

@@ -14,8 +14,7 @@ var (
 )
 
 // gitlabExporter implement the Exporter interface
-type gitlabExporter struct {
-}
+type gitlabExporter struct{}
 
 // Init .
 func (ge *gitlabExporter) Init(conf core.Configuration) error {

bridge/gitlab/gitlab.go 🔗

@@ -10,14 +10,14 @@ import (
 )
 
 const (
-	target         = "gitlab"
-	keyProjectID   = "project-id"
+	target = "gitlab"
+
 	keyGitlabId    = "gitlab-id"
 	keyGitlabUrl   = "gitlab-url"
 	keyGitlabLogin = "gitlab-login"
-	keyToken       = "token"
-	keyTarget      = "target"
-	keyOrigin      = "origin"
+
+	keyProjectID = "project-id"
+	keyToken     = "token"
 
 	defaultTimeout = 60 * time.Second
 )
@@ -37,7 +37,7 @@ func (*Gitlab) NewImporter() core.Importer {
 }
 
 func (*Gitlab) NewExporter() core.Exporter {
-	return &gitlabExporter{}
+	return nil
 }
 
 func buildClient(token string) *gitlab.Client {

bridge/gitlab/import.go 🔗

@@ -108,9 +108,9 @@ func (gi *gitlabImporter) ensureIssue(repo *cache.RepoCache, issue *gitlab.Issue
 			cleanText,
 			nil,
 			map[string]string{
-				keyOrigin:    target,
-				keyGitlabId:  parseID(issue.ID),
-				keyGitlabUrl: issue.WebURL,
+				core.KeyOrigin: target,
+				keyGitlabId:    parseID(issue.ID),
+				keyGitlabUrl:   issue.WebURL,
 			},
 		)
 

bridge/gitlab/iterator.go 🔗

@@ -243,11 +243,3 @@ func (i *iterator) NextLabelEvent() bool {
 func (i *iterator) LabelEventValue() *gitlab.LabelEvent {
 	return i.labelEvent.cache[i.labelEvent.index]
 }
-
-func min(a, b int) int {
-	if a > b {
-		return b
-	}
-
-	return a
-}