return error when create gitlabdownloader (#12790)

This commit is contained in:
Lunny Xiao 2020-09-10 16:27:49 +08:00 committed by GitHub
parent b333aa7db4
commit 0db9043aa7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 25 deletions

View File

@ -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) 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 // GitServiceType returns the type of git service
@ -73,7 +73,7 @@ type GitlabDownloader struct {
// NewGitlabDownloader creates a gitlab Downloader via gitlab API // NewGitlabDownloader creates a gitlab Downloader via gitlab API
// Use either a username/password, personal token entered into the username field, or anonymous/public access // Use either a username/password, personal token entered into the username field, or anonymous/public access
// Note: Public access only allows very basic 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 gitlabClient *gitlab.Client
var err error var err error
if token != "" { if token != "" {
@ -84,19 +84,19 @@ func NewGitlabDownloader(ctx context.Context, baseURL, repoPath, username, passw
if err != nil { if err != nil {
log.Trace("Error logging into gitlab: %v", err) 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 // 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)) gr, _, err := gitlabClient.Projects.GetProject(repoPath, nil, nil, gitlab.WithContext(ctx))
if err != nil { if err != nil {
log.Trace("Error retrieving project: %v", err) log.Trace("Error retrieving project: %v", err)
return nil return nil, err
} }
if gr == nil { if gr == nil {
log.Trace("Error getting project, project is nil") log.Trace("Error getting project, project is nil")
return nil return nil, errors.New("Error getting project, project is nil")
} }
return &GitlabDownloader{ return &GitlabDownloader{
@ -104,7 +104,7 @@ func NewGitlabDownloader(ctx context.Context, baseURL, repoPath, username, passw
client: gitlabClient, client: gitlabClient,
repoID: gr.ID, repoID: gr.ID,
repoName: gr.Name, repoName: gr.Name,
} }, nil
} }
// SetContext set context // SetContext set context
@ -114,10 +114,6 @@ func (g *GitlabDownloader) SetContext(ctx context.Context) {
// GetRepoInfo returns a repository information // GetRepoInfo returns a repository information
func (g *GitlabDownloader) GetRepoInfo() (*base.Repository, error) { 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)) gr, _, err := g.client.Projects.GetProject(g.repoID, nil, nil, gitlab.WithContext(g.ctx))
if err != nil { if err != nil {
return nil, err return nil, err
@ -154,10 +150,6 @@ func (g *GitlabDownloader) GetRepoInfo() (*base.Repository, error) {
// GetTopics return gitlab topics // GetTopics return gitlab topics
func (g *GitlabDownloader) GetTopics() ([]string, error) { 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)) gr, _, err := g.client.Projects.GetProject(g.repoID, nil, nil, gitlab.WithContext(g.ctx))
if err != nil { if err != nil {
return nil, err return nil, err
@ -167,9 +159,6 @@ func (g *GitlabDownloader) GetTopics() ([]string, error) {
// GetMilestones returns milestones // GetMilestones returns milestones
func (g *GitlabDownloader) GetMilestones() ([]*base.Milestone, error) { func (g *GitlabDownloader) GetMilestones() ([]*base.Milestone, error) {
if g == nil {
return nil, errors.New("error: GitlabDownloader is nil")
}
var perPage = 100 var perPage = 100
var state = "all" var state = "all"
var milestones = make([]*base.Milestone, 0, perPage) var milestones = make([]*base.Milestone, 0, perPage)
@ -228,9 +217,6 @@ func (g *GitlabDownloader) GetMilestones() ([]*base.Milestone, error) {
// GetLabels returns labels // GetLabels returns labels
func (g *GitlabDownloader) GetLabels() ([]*base.Label, error) { func (g *GitlabDownloader) GetLabels() ([]*base.Label, error) {
if g == nil {
return nil, errors.New("error: GitlabDownloader is nil")
}
var perPage = 100 var perPage = 100
var labels = make([]*base.Label, 0, perPage) var labels = make([]*base.Label, 0, perPage)
for i := 1; ; i++ { for i := 1; ; i++ {
@ -466,7 +452,6 @@ func (g *GitlabDownloader) GetComments(issueNumber int64) ([]*base.Comment, erro
// GetPullRequests returns pull requests according page and perPage // GetPullRequests returns pull requests according page and perPage
func (g *GitlabDownloader) GetPullRequests(page, perPage int) ([]*base.PullRequest, error) { func (g *GitlabDownloader) GetPullRequests(page, perPage int) ([]*base.PullRequest, error) {
opt := &gitlab.ListProjectMergeRequestsOptions{ opt := &gitlab.ListProjectMergeRequestsOptions{
ListOptions: gitlab.ListOptions{ ListOptions: gitlab.ListOptions{
PerPage: perPage, PerPage: perPage,
@ -576,7 +561,6 @@ func (g *GitlabDownloader) GetPullRequests(page, perPage int) ([]*base.PullReque
// GetReviews returns pull requests review // GetReviews returns pull requests review
func (g *GitlabDownloader) GetReviews(pullRequestNumber int64) ([]*base.Review, error) { func (g *GitlabDownloader) GetReviews(pullRequestNumber int64) ([]*base.Review, error) {
state, _, err := g.client.MergeRequestApprovals.GetApprovalState(g.repoID, int(pullRequestNumber), gitlab.WithContext(g.ctx)) state, _, err := g.client.MergeRequestApprovals.GetApprovalState(g.repoID, int(pullRequestNumber), gitlab.WithContext(g.ctx))
if err != nil { if err != nil {
return nil, err return nil, err

View File

@ -6,6 +6,7 @@ package migrations
import ( import (
"context" "context"
"fmt"
"net/http" "net/http"
"os" "os"
"testing" "testing"
@ -28,9 +29,9 @@ func TestGitlabDownloadRepo(t *testing.T) {
t.Skipf("Can't access test repo, skipping %s", t.Name()) t.Skipf("Can't access test repo, skipping %s", t.Name())
} }
downloader := NewGitlabDownloader(context.Background(), "https://gitlab.com", "gitea/test_repo", "", "", gitlabPersonalAccessToken) downloader, err := NewGitlabDownloader(context.Background(), "https://gitlab.com", "gitea/test_repo", "", "", gitlabPersonalAccessToken)
if downloader == nil { if err != nil {
t.Fatal("NewGitlabDownloader is nil") t.Fatal(fmt.Sprintf("NewGitlabDownloader is nil: %v", err))
} }
repo, err := downloader.GetRepoInfo() repo, err := downloader.GetRepoInfo()
assert.NoError(t, err) assert.NoError(t, err)