Skip to content

Commit 0db9043

Browse files
authored
return error when create gitlabdownloader (#12790)
1 parent b333aa7 commit 0db9043

File tree

2 files changed

+10
-25
lines changed

2 files changed

+10
-25
lines changed

modules/migrations/gitlab.go

+6-22
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ func (f *GitlabDownloaderFactory) New(ctx context.Context, opts base.MigrateOpti
4747

4848
log.Trace("Create gitlab downloader. BaseURL: %s RepoName: %s", baseURL, repoNameSpace)
4949

50-
return NewGitlabDownloader(ctx, baseURL, repoNameSpace, opts.AuthUsername, opts.AuthPassword, opts.AuthToken), nil
50+
return NewGitlabDownloader(ctx, baseURL, repoNameSpace, opts.AuthUsername, opts.AuthPassword, opts.AuthToken)
5151
}
5252

5353
// GitServiceType returns the type of git service
@@ -73,7 +73,7 @@ type GitlabDownloader struct {
7373
// NewGitlabDownloader creates a gitlab Downloader via gitlab API
7474
// Use either a username/password, personal token entered into the username field, or anonymous/public access
7575
// Note: Public access only allows very basic access
76-
func NewGitlabDownloader(ctx context.Context, baseURL, repoPath, username, password, token string) *GitlabDownloader {
76+
func NewGitlabDownloader(ctx context.Context, baseURL, repoPath, username, password, token string) (*GitlabDownloader, error) {
7777
var gitlabClient *gitlab.Client
7878
var err error
7979
if token != "" {
@@ -84,27 +84,27 @@ func NewGitlabDownloader(ctx context.Context, baseURL, repoPath, username, passw
8484

8585
if err != nil {
8686
log.Trace("Error logging into gitlab: %v", err)
87-
return nil
87+
return nil, err
8888
}
8989

9090
// Grab and store project/repo ID here, due to issues using the URL escaped path
9191
gr, _, err := gitlabClient.Projects.GetProject(repoPath, nil, nil, gitlab.WithContext(ctx))
9292
if err != nil {
9393
log.Trace("Error retrieving project: %v", err)
94-
return nil
94+
return nil, err
9595
}
9696

9797
if gr == nil {
9898
log.Trace("Error getting project, project is nil")
99-
return nil
99+
return nil, errors.New("Error getting project, project is nil")
100100
}
101101

102102
return &GitlabDownloader{
103103
ctx: ctx,
104104
client: gitlabClient,
105105
repoID: gr.ID,
106106
repoName: gr.Name,
107-
}
107+
}, nil
108108
}
109109

110110
// SetContext set context
@@ -114,10 +114,6 @@ func (g *GitlabDownloader) SetContext(ctx context.Context) {
114114

115115
// GetRepoInfo returns a repository information
116116
func (g *GitlabDownloader) GetRepoInfo() (*base.Repository, error) {
117-
if g == nil {
118-
return nil, errors.New("error: GitlabDownloader is nil")
119-
}
120-
121117
gr, _, err := g.client.Projects.GetProject(g.repoID, nil, nil, gitlab.WithContext(g.ctx))
122118
if err != nil {
123119
return nil, err
@@ -154,10 +150,6 @@ func (g *GitlabDownloader) GetRepoInfo() (*base.Repository, error) {
154150

155151
// GetTopics return gitlab topics
156152
func (g *GitlabDownloader) GetTopics() ([]string, error) {
157-
if g == nil {
158-
return nil, errors.New("error: GitlabDownloader is nil")
159-
}
160-
161153
gr, _, err := g.client.Projects.GetProject(g.repoID, nil, nil, gitlab.WithContext(g.ctx))
162154
if err != nil {
163155
return nil, err
@@ -167,9 +159,6 @@ func (g *GitlabDownloader) GetTopics() ([]string, error) {
167159

168160
// GetMilestones returns milestones
169161
func (g *GitlabDownloader) GetMilestones() ([]*base.Milestone, error) {
170-
if g == nil {
171-
return nil, errors.New("error: GitlabDownloader is nil")
172-
}
173162
var perPage = 100
174163
var state = "all"
175164
var milestones = make([]*base.Milestone, 0, perPage)
@@ -228,9 +217,6 @@ func (g *GitlabDownloader) GetMilestones() ([]*base.Milestone, error) {
228217

229218
// GetLabels returns labels
230219
func (g *GitlabDownloader) GetLabels() ([]*base.Label, error) {
231-
if g == nil {
232-
return nil, errors.New("error: GitlabDownloader is nil")
233-
}
234220
var perPage = 100
235221
var labels = make([]*base.Label, 0, perPage)
236222
for i := 1; ; i++ {
@@ -466,7 +452,6 @@ func (g *GitlabDownloader) GetComments(issueNumber int64) ([]*base.Comment, erro
466452

467453
// GetPullRequests returns pull requests according page and perPage
468454
func (g *GitlabDownloader) GetPullRequests(page, perPage int) ([]*base.PullRequest, error) {
469-
470455
opt := &gitlab.ListProjectMergeRequestsOptions{
471456
ListOptions: gitlab.ListOptions{
472457
PerPage: perPage,
@@ -576,7 +561,6 @@ func (g *GitlabDownloader) GetPullRequests(page, perPage int) ([]*base.PullReque
576561

577562
// GetReviews returns pull requests review
578563
func (g *GitlabDownloader) GetReviews(pullRequestNumber int64) ([]*base.Review, error) {
579-
580564
state, _, err := g.client.MergeRequestApprovals.GetApprovalState(g.repoID, int(pullRequestNumber), gitlab.WithContext(g.ctx))
581565
if err != nil {
582566
return nil, err

modules/migrations/gitlab_test.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package migrations
66

77
import (
88
"context"
9+
"fmt"
910
"net/http"
1011
"os"
1112
"testing"
@@ -28,9 +29,9 @@ func TestGitlabDownloadRepo(t *testing.T) {
2829
t.Skipf("Can't access test repo, skipping %s", t.Name())
2930
}
3031

31-
downloader := NewGitlabDownloader(context.Background(), "https://gitlab.com", "gitea/test_repo", "", "", gitlabPersonalAccessToken)
32-
if downloader == nil {
33-
t.Fatal("NewGitlabDownloader is nil")
32+
downloader, err := NewGitlabDownloader(context.Background(), "https://gitlab.com", "gitea/test_repo", "", "", gitlabPersonalAccessToken)
33+
if err != nil {
34+
t.Fatal(fmt.Sprintf("NewGitlabDownloader is nil: %v", err))
3435
}
3536
repo, err := downloader.GetRepoInfo()
3637
assert.NoError(t, err)

0 commit comments

Comments
 (0)