Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return error when creating gitlabdownloader failed #12790

Merged
merged 1 commit into from
Sep 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 6 additions & 22 deletions modules/migrations/gitlab.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func (f *GitlabDownloaderFactory) New(ctx context.Context, opts base.MigrateOpti

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

return NewGitlabDownloader(ctx, baseURL, repoNameSpace, opts.AuthUsername, opts.AuthPassword, opts.AuthToken), nil
return NewGitlabDownloader(ctx, baseURL, repoNameSpace, opts.AuthUsername, opts.AuthPassword, opts.AuthToken)
}

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

if err != nil {
log.Trace("Error logging into gitlab: %v", err)
return nil
return nil, err
}

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

if gr == nil {
log.Trace("Error getting project, project is nil")
return nil
return nil, errors.New("Error getting project, project is nil")
}

return &GitlabDownloader{
ctx: ctx,
client: gitlabClient,
repoID: gr.ID,
repoName: gr.Name,
}
}, nil
}

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

// GetRepoInfo returns a repository information
func (g *GitlabDownloader) GetRepoInfo() (*base.Repository, error) {
if g == nil {
return nil, errors.New("error: GitlabDownloader is nil")
}

gr, _, err := g.client.Projects.GetProject(g.repoID, nil, nil, gitlab.WithContext(g.ctx))
if err != nil {
return nil, err
Expand Down Expand Up @@ -154,10 +150,6 @@ func (g *GitlabDownloader) GetRepoInfo() (*base.Repository, error) {

// GetTopics return gitlab topics
func (g *GitlabDownloader) GetTopics() ([]string, error) {
if g == nil {
return nil, errors.New("error: GitlabDownloader is nil")
}

gr, _, err := g.client.Projects.GetProject(g.repoID, nil, nil, gitlab.WithContext(g.ctx))
if err != nil {
return nil, err
Expand All @@ -167,9 +159,6 @@ func (g *GitlabDownloader) GetTopics() ([]string, error) {

// GetMilestones returns milestones
func (g *GitlabDownloader) GetMilestones() ([]*base.Milestone, error) {
if g == nil {
return nil, errors.New("error: GitlabDownloader is nil")
}
var perPage = 100
var state = "all"
var milestones = make([]*base.Milestone, 0, perPage)
Expand Down Expand Up @@ -228,9 +217,6 @@ func (g *GitlabDownloader) GetMilestones() ([]*base.Milestone, error) {

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

// GetPullRequests returns pull requests according page and perPage
func (g *GitlabDownloader) GetPullRequests(page, perPage int) ([]*base.PullRequest, error) {

opt := &gitlab.ListProjectMergeRequestsOptions{
ListOptions: gitlab.ListOptions{
PerPage: perPage,
Expand Down Expand Up @@ -576,7 +561,6 @@ func (g *GitlabDownloader) GetPullRequests(page, perPage int) ([]*base.PullReque

// GetReviews returns pull requests review
func (g *GitlabDownloader) GetReviews(pullRequestNumber int64) ([]*base.Review, error) {

state, _, err := g.client.MergeRequestApprovals.GetApprovalState(g.repoID, int(pullRequestNumber), gitlab.WithContext(g.ctx))
if err != nil {
return nil, err
Expand Down
7 changes: 4 additions & 3 deletions modules/migrations/gitlab_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package migrations

import (
"context"
"fmt"
"net/http"
"os"
"testing"
Expand All @@ -28,9 +29,9 @@ func TestGitlabDownloadRepo(t *testing.T) {
t.Skipf("Can't access test repo, skipping %s", t.Name())
}

downloader := NewGitlabDownloader(context.Background(), "https://gitlab.com", "gitea/test_repo", "", "", gitlabPersonalAccessToken)
if downloader == nil {
t.Fatal("NewGitlabDownloader is nil")
downloader, err := NewGitlabDownloader(context.Background(), "https://gitlab.com", "gitea/test_repo", "", "", gitlabPersonalAccessToken)
if err != nil {
t.Fatal(fmt.Sprintf("NewGitlabDownloader is nil: %v", err))
}
repo, err := downloader.GetRepoInfo()
assert.NoError(t, err)
Expand Down