Skip to content

Commit 0329bfd

Browse files
committed
bridge/gitlab: change validateProjectURL signature
bridge/gitlab: code cleanup
1 parent d098a96 commit 0329bfd

File tree

6 files changed

+24
-35
lines changed

6 files changed

+24
-35
lines changed

Diff for: bridge/core/bridge.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ var ErrImportNotSupported = errors.New("import is not supported")
1919
var ErrExportNotSupported = errors.New("export is not supported")
2020

2121
const (
22-
KeyTarget = "target"
22+
KeyTarget = "target"
23+
KeyOrigin = "origin"
24+
2325
bridgeConfigKeyPrefix = "git-bug.bridge"
2426
)
2527

Diff for: bridge/gitlab/config.go

+11-15
Original file line numberDiff line numberDiff line change
@@ -61,26 +61,22 @@ func (*Gitlab) Configure(repo repository.RepoCommon, params core.BridgeParams) (
6161
}
6262
}
6363

64-
var ok bool
6564
// validate project url and get its ID
66-
ok, id, err := validateProjectURL(url, token)
65+
id, err := validateProjectURL(url, token)
6766
if err != nil {
6867
return nil, errors.Wrap(err, "project validation")
6968
}
70-
if !ok {
71-
return nil, fmt.Errorf("invalid project id or incorrect token scope")
72-
}
7369

7470
conf[keyProjectID] = strconv.Itoa(id)
7571
conf[keyToken] = token
76-
conf[keyTarget] = target
72+
conf[core.KeyTarget] = target
7773

7874
return conf, nil
7975
}
8076

8177
func (*Gitlab) ValidateConfig(conf core.Configuration) error {
82-
if v, ok := conf[keyTarget]; !ok {
83-
return fmt.Errorf("missing %s key", keyTarget)
78+
if v, ok := conf[core.KeyTarget]; !ok {
79+
return fmt.Errorf("missing %s key", core.KeyTarget)
8480
} else if v != target {
8581
return fmt.Errorf("unexpected target name: %v", v)
8682
}
@@ -147,7 +143,7 @@ func promptURL(remotes map[string]string) (string, error) {
147143
line = strings.TrimRight(line, "\n")
148144

149145
index, err := strconv.Atoi(line)
150-
if err != nil || (index < 0 && index > len(validRemotes)) {
146+
if err != nil || index < 0 || index > len(validRemotes) {
151147
fmt.Println("invalid input")
152148
continue
153149
}
@@ -205,18 +201,18 @@ func getValidGitlabRemoteURLs(remotes map[string]string) []string {
205201
return urls
206202
}
207203

208-
func validateProjectURL(url, token string) (bool, int, error) {
209-
client := buildClient(token)
210-
204+
func validateProjectURL(url, token string) (int, error) {
211205
projectPath, err := getProjectPath(url)
212206
if err != nil {
213-
return false, 0, err
207+
return 0, err
214208
}
215209

210+
client := buildClient(token)
211+
216212
project, _, err := client.Projects.GetProject(projectPath, &gitlab.GetProjectOptions{})
217213
if err != nil {
218-
return false, 0, err
214+
return 0, err
219215
}
220216

221-
return true, project.ID, nil
217+
return project.ID, nil
222218
}

Diff for: bridge/gitlab/export.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@ var (
1414
)
1515

1616
// gitlabExporter implement the Exporter interface
17-
type gitlabExporter struct {
18-
}
17+
type gitlabExporter struct{}
1918

2019
// Init .
2120
func (ge *gitlabExporter) Init(conf core.Configuration) error {

Diff for: bridge/gitlab/gitlab.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@ import (
1010
)
1111

1212
const (
13-
target = "gitlab"
14-
keyProjectID = "project-id"
13+
target = "gitlab"
14+
1515
keyGitlabId = "gitlab-id"
1616
keyGitlabUrl = "gitlab-url"
1717
keyGitlabLogin = "gitlab-login"
18-
keyToken = "token"
19-
keyTarget = "target"
20-
keyOrigin = "origin"
18+
19+
keyProjectID = "project-id"
20+
keyToken = "token"
2121

2222
defaultTimeout = 60 * time.Second
2323
)
@@ -37,7 +37,7 @@ func (*Gitlab) NewImporter() core.Importer {
3737
}
3838

3939
func (*Gitlab) NewExporter() core.Exporter {
40-
return &gitlabExporter{}
40+
return nil
4141
}
4242

4343
func buildClient(token string) *gitlab.Client {

Diff for: bridge/gitlab/import.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,9 @@ func (gi *gitlabImporter) ensureIssue(repo *cache.RepoCache, issue *gitlab.Issue
108108
cleanText,
109109
nil,
110110
map[string]string{
111-
keyOrigin: target,
112-
keyGitlabId: parseID(issue.ID),
113-
keyGitlabUrl: issue.WebURL,
111+
core.KeyOrigin: target,
112+
keyGitlabId: parseID(issue.ID),
113+
keyGitlabUrl: issue.WebURL,
114114
},
115115
)
116116

Diff for: bridge/gitlab/iterator.go

-8
Original file line numberDiff line numberDiff line change
@@ -243,11 +243,3 @@ func (i *iterator) NextLabelEvent() bool {
243243
func (i *iterator) LabelEventValue() *gitlab.LabelEvent {
244244
return i.labelEvent.cache[i.labelEvent.index]
245245
}
246-
247-
func min(a, b int) int {
248-
if a > b {
249-
return b
250-
}
251-
252-
return a
253-
}

0 commit comments

Comments
 (0)