2020-04-19 23:44:11 +08:00
|
|
|
// Copyright 2019 The Gitea Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package migrations
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2020-08-28 09:36:37 +08:00
|
|
|
"io"
|
|
|
|
"net/http"
|
2020-04-19 23:44:11 +08:00
|
|
|
"net/url"
|
2020-11-17 03:22:49 +08:00
|
|
|
"path"
|
2020-04-19 23:44:11 +08:00
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"code.gitea.io/gitea/modules/log"
|
2021-11-16 23:25:33 +08:00
|
|
|
base "code.gitea.io/gitea/modules/migration"
|
2020-04-19 23:44:11 +08:00
|
|
|
"code.gitea.io/gitea/modules/structs"
|
|
|
|
|
|
|
|
"github.com/xanzy/go-gitlab"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
_ base.Downloader = &GitlabDownloader{}
|
|
|
|
_ base.DownloaderFactory = &GitlabDownloaderFactory{}
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
RegisterDownloaderFactory(&GitlabDownloaderFactory{})
|
|
|
|
}
|
|
|
|
|
|
|
|
// GitlabDownloaderFactory defines a gitlab downloader factory
|
|
|
|
type GitlabDownloaderFactory struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
// New returns a Downloader related to this factory according MigrateOptions
|
2020-09-03 01:49:25 +08:00
|
|
|
func (f *GitlabDownloaderFactory) New(ctx context.Context, opts base.MigrateOptions) (base.Downloader, error) {
|
2020-04-19 23:44:11 +08:00
|
|
|
u, err := url.Parse(opts.CloneAddr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
baseURL := u.Scheme + "://" + u.Host
|
|
|
|
repoNameSpace := strings.TrimPrefix(u.Path, "/")
|
2020-08-28 09:36:37 +08:00
|
|
|
repoNameSpace = strings.TrimSuffix(repoNameSpace, ".git")
|
2020-04-19 23:44:11 +08:00
|
|
|
|
|
|
|
log.Trace("Create gitlab downloader. BaseURL: %s RepoName: %s", baseURL, repoNameSpace)
|
|
|
|
|
2020-09-10 16:27:49 +08:00
|
|
|
return NewGitlabDownloader(ctx, baseURL, repoNameSpace, opts.AuthUsername, opts.AuthPassword, opts.AuthToken)
|
2020-04-19 23:44:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// GitServiceType returns the type of git service
|
|
|
|
func (f *GitlabDownloaderFactory) GitServiceType() structs.GitServiceType {
|
|
|
|
return structs.GitlabService
|
|
|
|
}
|
|
|
|
|
2021-07-08 19:38:13 +08:00
|
|
|
// GitlabDownloader implements a Downloader interface to get repository information
|
2020-04-19 23:44:11 +08:00
|
|
|
// from gitlab via go-gitlab
|
|
|
|
// - issueCount is incremented in GetIssues() to ensure PR and Issue numbers do not overlap,
|
|
|
|
// because Gitlab has individual Issue and Pull Request numbers.
|
|
|
|
type GitlabDownloader struct {
|
2021-01-22 03:33:58 +08:00
|
|
|
base.NullDownloader
|
2021-08-22 06:47:45 +08:00
|
|
|
ctx context.Context
|
|
|
|
client *gitlab.Client
|
|
|
|
repoID int
|
|
|
|
repoName string
|
|
|
|
issueCount int64
|
|
|
|
maxPerPage int
|
2020-04-19 23:44:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
2020-09-10 16:27:49 +08:00
|
|
|
func NewGitlabDownloader(ctx context.Context, baseURL, repoPath, username, password, token string) (*GitlabDownloader, error) {
|
2021-11-20 17:34:05 +08:00
|
|
|
gitlabClient, err := gitlab.NewClient(token, gitlab.WithBaseURL(baseURL), gitlab.WithHTTPClient(NewMigrationHTTPClient()))
|
2020-09-16 03:32:14 +08:00
|
|
|
// Only use basic auth if token is blank and password is NOT
|
|
|
|
// Basic auth will fail with empty strings, but empty token will allow anonymous public API usage
|
|
|
|
if token == "" && password != "" {
|
2021-11-20 17:34:05 +08:00
|
|
|
gitlabClient, err = gitlab.NewBasicAuthClient(username, password, gitlab.WithBaseURL(baseURL), gitlab.WithHTTPClient(NewMigrationHTTPClient()))
|
2020-04-19 23:44:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Trace("Error logging into gitlab: %v", err)
|
2020-09-10 16:27:49 +08:00
|
|
|
return nil, err
|
2020-04-19 23:44:11 +08:00
|
|
|
}
|
|
|
|
|
2020-11-17 03:22:49 +08:00
|
|
|
// split namespace and subdirectory
|
|
|
|
pathParts := strings.Split(strings.Trim(repoPath, "/"), "/")
|
2020-11-19 13:17:56 +08:00
|
|
|
var resp *gitlab.Response
|
|
|
|
u, _ := url.Parse(baseURL)
|
2020-11-20 07:18:34 +08:00
|
|
|
for len(pathParts) >= 2 {
|
2020-11-19 13:17:56 +08:00
|
|
|
_, resp, err = gitlabClient.Version.GetVersion()
|
|
|
|
if err == nil || resp != nil && resp.StatusCode == 401 {
|
|
|
|
err = nil // if no authentication given, this still should work
|
2020-11-17 03:22:49 +08:00
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2020-11-19 13:17:56 +08:00
|
|
|
u.Path = path.Join(u.Path, pathParts[0])
|
|
|
|
baseURL = u.String()
|
2020-11-17 03:22:49 +08:00
|
|
|
pathParts = pathParts[1:]
|
|
|
|
_ = gitlab.WithBaseURL(baseURL)(gitlabClient)
|
|
|
|
repoPath = strings.Join(pathParts, "/")
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
log.Trace("Error could not get gitlab version: %v", err)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-11-19 13:17:56 +08:00
|
|
|
log.Trace("gitlab downloader: use BaseURL: '%s' and RepoPath: '%s'", baseURL, repoPath)
|
|
|
|
|
2020-04-19 23:44:11 +08:00
|
|
|
// Grab and store project/repo ID here, due to issues using the URL escaped path
|
2020-09-03 01:49:25 +08:00
|
|
|
gr, _, err := gitlabClient.Projects.GetProject(repoPath, nil, nil, gitlab.WithContext(ctx))
|
2020-04-19 23:44:11 +08:00
|
|
|
if err != nil {
|
|
|
|
log.Trace("Error retrieving project: %v", err)
|
2020-09-10 16:27:49 +08:00
|
|
|
return nil, err
|
2020-04-19 23:44:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if gr == nil {
|
|
|
|
log.Trace("Error getting project, project is nil")
|
2020-09-10 16:27:49 +08:00
|
|
|
return nil, errors.New("Error getting project, project is nil")
|
2020-04-19 23:44:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return &GitlabDownloader{
|
2020-10-25 13:11:03 +08:00
|
|
|
ctx: ctx,
|
|
|
|
client: gitlabClient,
|
|
|
|
repoID: gr.ID,
|
|
|
|
repoName: gr.Name,
|
|
|
|
maxPerPage: 100,
|
2020-09-10 16:27:49 +08:00
|
|
|
}, nil
|
2020-04-19 23:44:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetContext set context
|
|
|
|
func (g *GitlabDownloader) SetContext(ctx context.Context) {
|
|
|
|
g.ctx = ctx
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetRepoInfo returns a repository information
|
|
|
|
func (g *GitlabDownloader) GetRepoInfo() (*base.Repository, error) {
|
2020-09-03 01:49:25 +08:00
|
|
|
gr, _, err := g.client.Projects.GetProject(g.repoID, nil, nil, gitlab.WithContext(g.ctx))
|
2020-04-19 23:44:11 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var private bool
|
|
|
|
switch gr.Visibility {
|
|
|
|
case gitlab.InternalVisibility:
|
|
|
|
private = true
|
|
|
|
case gitlab.PrivateVisibility:
|
|
|
|
private = true
|
|
|
|
}
|
|
|
|
|
|
|
|
var owner string
|
|
|
|
if gr.Owner == nil {
|
|
|
|
log.Trace("gr.Owner is nil, trying to get owner from Namespace")
|
|
|
|
if gr.Namespace != nil && gr.Namespace.Kind == "user" {
|
|
|
|
owner = gr.Namespace.Path
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
owner = gr.Owner.Username
|
|
|
|
}
|
|
|
|
|
|
|
|
// convert gitlab repo to stand Repo
|
|
|
|
return &base.Repository{
|
2020-09-15 22:37:44 +08:00
|
|
|
Owner: owner,
|
|
|
|
Name: gr.Name,
|
|
|
|
IsPrivate: private,
|
|
|
|
Description: gr.Description,
|
|
|
|
OriginalURL: gr.WebURL,
|
|
|
|
CloneURL: gr.HTTPURLToRepo,
|
|
|
|
DefaultBranch: gr.DefaultBranch,
|
2020-04-19 23:44:11 +08:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetTopics return gitlab topics
|
|
|
|
func (g *GitlabDownloader) GetTopics() ([]string, error) {
|
2020-09-03 01:49:25 +08:00
|
|
|
gr, _, err := g.client.Projects.GetProject(g.repoID, nil, nil, gitlab.WithContext(g.ctx))
|
2020-04-19 23:44:11 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return gr.TagList, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetMilestones returns milestones
|
|
|
|
func (g *GitlabDownloader) GetMilestones() ([]*base.Milestone, error) {
|
2020-10-25 13:11:03 +08:00
|
|
|
var perPage = g.maxPerPage
|
2020-04-19 23:44:11 +08:00
|
|
|
var state = "all"
|
|
|
|
var milestones = make([]*base.Milestone, 0, perPage)
|
|
|
|
for i := 1; ; i++ {
|
|
|
|
ms, _, err := g.client.Milestones.ListMilestones(g.repoID, &gitlab.ListMilestonesOptions{
|
|
|
|
State: &state,
|
|
|
|
ListOptions: gitlab.ListOptions{
|
|
|
|
Page: i,
|
|
|
|
PerPage: perPage,
|
2020-09-03 01:49:25 +08:00
|
|
|
}}, nil, gitlab.WithContext(g.ctx))
|
2020-04-19 23:44:11 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, m := range ms {
|
|
|
|
var desc string
|
|
|
|
if m.Description != "" {
|
|
|
|
desc = m.Description
|
|
|
|
}
|
|
|
|
var state = "open"
|
|
|
|
var closedAt *time.Time
|
|
|
|
if m.State != "" {
|
|
|
|
state = m.State
|
|
|
|
if state == "closed" {
|
|
|
|
closedAt = m.UpdatedAt
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var deadline *time.Time
|
|
|
|
if m.DueDate != nil {
|
|
|
|
deadlineParsed, err := time.Parse("2006-01-02", m.DueDate.String())
|
|
|
|
if err != nil {
|
|
|
|
log.Trace("Error parsing Milestone DueDate time")
|
|
|
|
deadline = nil
|
|
|
|
} else {
|
|
|
|
deadline = &deadlineParsed
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
milestones = append(milestones, &base.Milestone{
|
|
|
|
Title: m.Title,
|
|
|
|
Description: desc,
|
|
|
|
Deadline: deadline,
|
|
|
|
State: state,
|
|
|
|
Created: *m.CreatedAt,
|
|
|
|
Updated: m.UpdatedAt,
|
|
|
|
Closed: closedAt,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
if len(ms) < perPage {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return milestones, nil
|
|
|
|
}
|
|
|
|
|
2020-09-10 21:04:30 +08:00
|
|
|
func (g *GitlabDownloader) normalizeColor(val string) string {
|
|
|
|
val = strings.TrimLeft(val, "#")
|
|
|
|
val = strings.ToLower(val)
|
|
|
|
if len(val) == 3 {
|
|
|
|
c := []rune(val)
|
|
|
|
val = fmt.Sprintf("%c%c%c%c%c%c", c[0], c[0], c[1], c[1], c[2], c[2])
|
|
|
|
}
|
|
|
|
if len(val) != 6 {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return val
|
|
|
|
}
|
|
|
|
|
2020-04-19 23:44:11 +08:00
|
|
|
// GetLabels returns labels
|
|
|
|
func (g *GitlabDownloader) GetLabels() ([]*base.Label, error) {
|
2020-10-25 13:11:03 +08:00
|
|
|
var perPage = g.maxPerPage
|
2020-04-19 23:44:11 +08:00
|
|
|
var labels = make([]*base.Label, 0, perPage)
|
|
|
|
for i := 1; ; i++ {
|
2020-09-06 23:37:53 +08:00
|
|
|
ls, _, err := g.client.Labels.ListLabels(g.repoID, &gitlab.ListLabelsOptions{ListOptions: gitlab.ListOptions{
|
2020-04-19 23:44:11 +08:00
|
|
|
Page: i,
|
|
|
|
PerPage: perPage,
|
2020-09-06 23:37:53 +08:00
|
|
|
}}, nil, gitlab.WithContext(g.ctx))
|
2020-04-19 23:44:11 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
for _, label := range ls {
|
|
|
|
baseLabel := &base.Label{
|
|
|
|
Name: label.Name,
|
2020-09-10 21:04:30 +08:00
|
|
|
Color: g.normalizeColor(label.Color),
|
2020-04-19 23:44:11 +08:00
|
|
|
Description: label.Description,
|
|
|
|
}
|
|
|
|
labels = append(labels, baseLabel)
|
|
|
|
}
|
|
|
|
if len(ls) < perPage {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return labels, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *GitlabDownloader) convertGitlabRelease(rel *gitlab.Release) *base.Release {
|
2020-08-28 09:36:37 +08:00
|
|
|
var zero int
|
2020-04-19 23:44:11 +08:00
|
|
|
r := &base.Release{
|
|
|
|
TagName: rel.TagName,
|
|
|
|
TargetCommitish: rel.Commit.ID,
|
|
|
|
Name: rel.Name,
|
|
|
|
Body: rel.Description,
|
|
|
|
Created: *rel.CreatedAt,
|
|
|
|
PublisherID: int64(rel.Author.ID),
|
|
|
|
PublisherName: rel.Author.Username,
|
|
|
|
}
|
|
|
|
|
2021-11-20 17:34:05 +08:00
|
|
|
httpClient := NewMigrationHTTPClient()
|
2021-08-18 21:10:39 +08:00
|
|
|
|
2020-04-19 23:44:11 +08:00
|
|
|
for k, asset := range rel.Assets.Links {
|
2020-12-27 11:34:19 +08:00
|
|
|
r.Assets = append(r.Assets, &base.ReleaseAsset{
|
2020-08-28 09:36:37 +08:00
|
|
|
ID: int64(asset.ID),
|
|
|
|
Name: asset.Name,
|
|
|
|
ContentType: &rel.Assets.Sources[k].Format,
|
|
|
|
Size: &zero,
|
|
|
|
DownloadCount: &zero,
|
2020-12-27 11:34:19 +08:00
|
|
|
DownloadFunc: func() (io.ReadCloser, error) {
|
|
|
|
link, _, err := g.client.ReleaseLinks.GetReleaseLink(g.repoID, rel.TagName, asset.ID, gitlab.WithContext(g.ctx))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
req, err := http.NewRequest("GET", link.URL, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
req = req.WithContext(g.ctx)
|
2021-08-18 21:10:39 +08:00
|
|
|
resp, err := httpClient.Do(req)
|
2020-12-27 11:34:19 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// resp.Body is closed by the uploader
|
|
|
|
return resp.Body, nil
|
|
|
|
},
|
2020-04-19 23:44:11 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetReleases returns releases
|
|
|
|
func (g *GitlabDownloader) GetReleases() ([]*base.Release, error) {
|
2020-10-25 13:11:03 +08:00
|
|
|
var perPage = g.maxPerPage
|
2020-04-19 23:44:11 +08:00
|
|
|
var releases = make([]*base.Release, 0, perPage)
|
|
|
|
for i := 1; ; i++ {
|
|
|
|
ls, _, err := g.client.Releases.ListReleases(g.repoID, &gitlab.ListReleasesOptions{
|
|
|
|
Page: i,
|
|
|
|
PerPage: perPage,
|
2020-09-03 01:49:25 +08:00
|
|
|
}, nil, gitlab.WithContext(g.ctx))
|
2020-04-19 23:44:11 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, release := range ls {
|
|
|
|
releases = append(releases, g.convertGitlabRelease(release))
|
|
|
|
}
|
|
|
|
if len(ls) < perPage {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return releases, nil
|
|
|
|
}
|
|
|
|
|
2021-08-22 06:47:45 +08:00
|
|
|
type gitlabIssueContext struct {
|
|
|
|
foreignID int64
|
|
|
|
localID int64
|
|
|
|
IsMergeRequest bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c gitlabIssueContext) LocalID() int64 {
|
|
|
|
return c.localID
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c gitlabIssueContext) ForeignID() int64 {
|
|
|
|
return c.foreignID
|
|
|
|
}
|
|
|
|
|
2020-04-19 23:44:11 +08:00
|
|
|
// GetIssues returns issues according start and limit
|
|
|
|
// Note: issue label description and colors are not supported by the go-gitlab library at this time
|
|
|
|
func (g *GitlabDownloader) GetIssues(page, perPage int) ([]*base.Issue, bool, error) {
|
|
|
|
state := "all"
|
|
|
|
sort := "asc"
|
|
|
|
|
2020-10-25 13:11:03 +08:00
|
|
|
if perPage > g.maxPerPage {
|
|
|
|
perPage = g.maxPerPage
|
|
|
|
}
|
|
|
|
|
2020-04-19 23:44:11 +08:00
|
|
|
opt := &gitlab.ListProjectIssuesOptions{
|
|
|
|
State: &state,
|
|
|
|
Sort: &sort,
|
|
|
|
ListOptions: gitlab.ListOptions{
|
|
|
|
PerPage: perPage,
|
|
|
|
Page: page,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var allIssues = make([]*base.Issue, 0, perPage)
|
|
|
|
|
2020-09-03 01:49:25 +08:00
|
|
|
issues, _, err := g.client.Issues.ListProjectIssues(g.repoID, opt, nil, gitlab.WithContext(g.ctx))
|
2020-04-19 23:44:11 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, false, fmt.Errorf("error while listing issues: %v", err)
|
|
|
|
}
|
|
|
|
for _, issue := range issues {
|
|
|
|
|
|
|
|
var labels = make([]*base.Label, 0, len(issue.Labels))
|
|
|
|
for _, l := range issue.Labels {
|
|
|
|
labels = append(labels, &base.Label{
|
|
|
|
Name: l,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
var milestone string
|
|
|
|
if issue.Milestone != nil {
|
|
|
|
milestone = issue.Milestone.Title
|
|
|
|
}
|
|
|
|
|
2020-09-03 15:35:17 +08:00
|
|
|
var reactions []*base.Reaction
|
|
|
|
var awardPage = 1
|
|
|
|
for {
|
|
|
|
awards, _, err := g.client.AwardEmoji.ListIssueAwardEmoji(g.repoID, issue.IID, &gitlab.ListAwardEmojiOptions{Page: awardPage, PerPage: perPage}, gitlab.WithContext(g.ctx))
|
|
|
|
if err != nil {
|
|
|
|
return nil, false, fmt.Errorf("error while listing issue awards: %v", err)
|
|
|
|
}
|
2021-09-01 18:20:19 +08:00
|
|
|
|
2020-09-03 15:35:17 +08:00
|
|
|
for i := range awards {
|
|
|
|
reactions = append(reactions, g.awardToReaction(awards[i]))
|
|
|
|
}
|
2021-09-01 18:20:19 +08:00
|
|
|
|
|
|
|
if len(awards) < perPage {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2020-09-03 15:35:17 +08:00
|
|
|
awardPage++
|
|
|
|
}
|
|
|
|
|
2020-04-19 23:44:11 +08:00
|
|
|
allIssues = append(allIssues, &base.Issue{
|
|
|
|
Title: issue.Title,
|
|
|
|
Number: int64(issue.IID),
|
|
|
|
PosterID: int64(issue.Author.ID),
|
|
|
|
PosterName: issue.Author.Username,
|
|
|
|
Content: issue.Description,
|
|
|
|
Milestone: milestone,
|
|
|
|
State: issue.State,
|
|
|
|
Created: *issue.CreatedAt,
|
|
|
|
Labels: labels,
|
2020-09-03 15:35:17 +08:00
|
|
|
Reactions: reactions,
|
2020-04-19 23:44:11 +08:00
|
|
|
Closed: issue.ClosedAt,
|
|
|
|
IsLocked: issue.DiscussionLocked,
|
|
|
|
Updated: *issue.UpdatedAt,
|
2021-08-22 06:47:45 +08:00
|
|
|
Context: gitlabIssueContext{
|
|
|
|
foreignID: int64(issue.IID),
|
|
|
|
localID: int64(issue.IID),
|
|
|
|
IsMergeRequest: false,
|
|
|
|
},
|
2020-04-19 23:44:11 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
// increment issueCount, to be used in GetPullRequests()
|
|
|
|
g.issueCount++
|
|
|
|
}
|
|
|
|
|
|
|
|
return allIssues, len(issues) < perPage, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetComments returns comments according issueNumber
|
2020-09-03 15:35:17 +08:00
|
|
|
// TODO: figure out how to transfer comment reactions
|
2021-06-30 15:23:49 +08:00
|
|
|
func (g *GitlabDownloader) GetComments(opts base.GetCommentOptions) ([]*base.Comment, bool, error) {
|
2021-08-22 06:47:45 +08:00
|
|
|
context, ok := opts.Context.(gitlabIssueContext)
|
|
|
|
if !ok {
|
|
|
|
return nil, false, fmt.Errorf("unexpected context: %+v", opts.Context)
|
|
|
|
}
|
|
|
|
|
2020-10-25 13:11:03 +08:00
|
|
|
var allComments = make([]*base.Comment, 0, g.maxPerPage)
|
2020-04-19 23:44:11 +08:00
|
|
|
|
|
|
|
var page = 1
|
|
|
|
|
|
|
|
for {
|
|
|
|
var comments []*gitlab.Discussion
|
|
|
|
var resp *gitlab.Response
|
|
|
|
var err error
|
2021-08-22 06:47:45 +08:00
|
|
|
if !context.IsMergeRequest {
|
|
|
|
comments, resp, err = g.client.Discussions.ListIssueDiscussions(g.repoID, int(context.ForeignID()), &gitlab.ListIssueDiscussionsOptions{
|
2020-04-19 23:44:11 +08:00
|
|
|
Page: page,
|
2020-10-25 13:11:03 +08:00
|
|
|
PerPage: g.maxPerPage,
|
2020-09-03 01:49:25 +08:00
|
|
|
}, nil, gitlab.WithContext(g.ctx))
|
2020-04-19 23:44:11 +08:00
|
|
|
} else {
|
2021-08-22 06:47:45 +08:00
|
|
|
comments, resp, err = g.client.Discussions.ListMergeRequestDiscussions(g.repoID, int(context.ForeignID()), &gitlab.ListMergeRequestDiscussionsOptions{
|
2020-04-19 23:44:11 +08:00
|
|
|
Page: page,
|
2020-10-25 13:11:03 +08:00
|
|
|
PerPage: g.maxPerPage,
|
2020-09-03 01:49:25 +08:00
|
|
|
}, nil, gitlab.WithContext(g.ctx))
|
2020-04-19 23:44:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
2021-06-30 15:23:49 +08:00
|
|
|
return nil, false, fmt.Errorf("error while listing comments: %v %v", g.repoID, err)
|
2020-04-19 23:44:11 +08:00
|
|
|
}
|
|
|
|
for _, comment := range comments {
|
|
|
|
// Flatten comment threads
|
|
|
|
if !comment.IndividualNote {
|
|
|
|
for _, note := range comment.Notes {
|
|
|
|
allComments = append(allComments, &base.Comment{
|
2021-08-22 06:47:45 +08:00
|
|
|
IssueIndex: context.LocalID(),
|
2020-04-19 23:44:11 +08:00
|
|
|
PosterID: int64(note.Author.ID),
|
|
|
|
PosterName: note.Author.Username,
|
|
|
|
PosterEmail: note.Author.Email,
|
|
|
|
Content: note.Body,
|
|
|
|
Created: *note.CreatedAt,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
c := comment.Notes[0]
|
|
|
|
allComments = append(allComments, &base.Comment{
|
2021-08-22 06:47:45 +08:00
|
|
|
IssueIndex: context.LocalID(),
|
2020-04-19 23:44:11 +08:00
|
|
|
PosterID: int64(c.Author.ID),
|
|
|
|
PosterName: c.Author.Username,
|
|
|
|
PosterEmail: c.Author.Email,
|
|
|
|
Content: c.Body,
|
|
|
|
Created: *c.CreatedAt,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
if resp.NextPage == 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
page = resp.NextPage
|
|
|
|
}
|
2021-06-30 15:23:49 +08:00
|
|
|
return allComments, true, nil
|
2020-04-19 23:44:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetPullRequests returns pull requests according page and perPage
|
2020-10-14 12:06:00 +08:00
|
|
|
func (g *GitlabDownloader) GetPullRequests(page, perPage int) ([]*base.PullRequest, bool, error) {
|
2020-10-25 13:11:03 +08:00
|
|
|
if perPage > g.maxPerPage {
|
|
|
|
perPage = g.maxPerPage
|
|
|
|
}
|
|
|
|
|
2020-04-19 23:44:11 +08:00
|
|
|
opt := &gitlab.ListProjectMergeRequestsOptions{
|
|
|
|
ListOptions: gitlab.ListOptions{
|
|
|
|
PerPage: perPage,
|
|
|
|
Page: page,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var allPRs = make([]*base.PullRequest, 0, perPage)
|
|
|
|
|
2020-09-03 01:49:25 +08:00
|
|
|
prs, _, err := g.client.MergeRequests.ListProjectMergeRequests(g.repoID, opt, nil, gitlab.WithContext(g.ctx))
|
2020-04-19 23:44:11 +08:00
|
|
|
if err != nil {
|
2020-10-14 12:06:00 +08:00
|
|
|
return nil, false, fmt.Errorf("error while listing merge requests: %v", err)
|
2020-04-19 23:44:11 +08:00
|
|
|
}
|
|
|
|
for _, pr := range prs {
|
|
|
|
|
|
|
|
var labels = make([]*base.Label, 0, len(pr.Labels))
|
|
|
|
for _, l := range pr.Labels {
|
|
|
|
labels = append(labels, &base.Label{
|
|
|
|
Name: l,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
var merged bool
|
|
|
|
if pr.State == "merged" {
|
|
|
|
merged = true
|
|
|
|
pr.State = "closed"
|
|
|
|
}
|
|
|
|
|
|
|
|
var mergeTime = pr.MergedAt
|
|
|
|
if merged && pr.MergedAt == nil {
|
|
|
|
mergeTime = pr.UpdatedAt
|
|
|
|
}
|
|
|
|
|
|
|
|
var closeTime = pr.ClosedAt
|
|
|
|
if merged && pr.ClosedAt == nil {
|
|
|
|
closeTime = pr.UpdatedAt
|
|
|
|
}
|
|
|
|
|
|
|
|
var locked bool
|
|
|
|
if pr.State == "locked" {
|
|
|
|
locked = true
|
|
|
|
}
|
|
|
|
|
|
|
|
var milestone string
|
|
|
|
if pr.Milestone != nil {
|
|
|
|
milestone = pr.Milestone.Title
|
|
|
|
}
|
|
|
|
|
2020-09-03 15:35:17 +08:00
|
|
|
var reactions []*base.Reaction
|
|
|
|
var awardPage = 1
|
|
|
|
for {
|
|
|
|
awards, _, err := g.client.AwardEmoji.ListMergeRequestAwardEmoji(g.repoID, pr.IID, &gitlab.ListAwardEmojiOptions{Page: awardPage, PerPage: perPage}, gitlab.WithContext(g.ctx))
|
|
|
|
if err != nil {
|
2020-10-14 12:06:00 +08:00
|
|
|
return nil, false, fmt.Errorf("error while listing merge requests awards: %v", err)
|
2020-09-03 15:35:17 +08:00
|
|
|
}
|
2021-09-01 18:20:19 +08:00
|
|
|
|
2020-09-03 15:35:17 +08:00
|
|
|
for i := range awards {
|
|
|
|
reactions = append(reactions, g.awardToReaction(awards[i]))
|
|
|
|
}
|
2021-09-01 18:20:19 +08:00
|
|
|
|
|
|
|
if len(awards) < perPage {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2020-09-03 15:35:17 +08:00
|
|
|
awardPage++
|
|
|
|
}
|
|
|
|
|
2020-04-19 23:44:11 +08:00
|
|
|
// Add the PR ID to the Issue Count because PR and Issues share ID space in Gitea
|
2020-04-20 20:30:46 +08:00
|
|
|
newPRNumber := g.issueCount + int64(pr.IID)
|
2020-04-19 23:44:11 +08:00
|
|
|
|
|
|
|
allPRs = append(allPRs, &base.PullRequest{
|
|
|
|
Title: pr.Title,
|
2020-04-20 20:30:46 +08:00
|
|
|
Number: newPRNumber,
|
2020-04-19 23:44:11 +08:00
|
|
|
PosterName: pr.Author.Username,
|
|
|
|
PosterID: int64(pr.Author.ID),
|
|
|
|
Content: pr.Description,
|
|
|
|
Milestone: milestone,
|
|
|
|
State: pr.State,
|
|
|
|
Created: *pr.CreatedAt,
|
|
|
|
Closed: closeTime,
|
|
|
|
Labels: labels,
|
|
|
|
Merged: merged,
|
|
|
|
MergeCommitSHA: pr.MergeCommitSHA,
|
|
|
|
MergedTime: mergeTime,
|
|
|
|
IsLocked: locked,
|
2020-09-03 15:35:17 +08:00
|
|
|
Reactions: reactions,
|
2020-04-19 23:44:11 +08:00
|
|
|
Head: base.PullRequestBranch{
|
|
|
|
Ref: pr.SourceBranch,
|
|
|
|
SHA: pr.SHA,
|
|
|
|
RepoName: g.repoName,
|
|
|
|
OwnerName: pr.Author.Username,
|
|
|
|
CloneURL: pr.WebURL,
|
|
|
|
},
|
|
|
|
Base: base.PullRequestBranch{
|
|
|
|
Ref: pr.TargetBranch,
|
|
|
|
SHA: pr.DiffRefs.BaseSha,
|
|
|
|
RepoName: g.repoName,
|
|
|
|
OwnerName: pr.Author.Username,
|
|
|
|
},
|
|
|
|
PatchURL: pr.WebURL + ".patch",
|
2021-08-22 06:47:45 +08:00
|
|
|
Context: gitlabIssueContext{
|
|
|
|
foreignID: int64(pr.IID),
|
|
|
|
localID: newPRNumber,
|
|
|
|
IsMergeRequest: true,
|
|
|
|
},
|
2020-04-19 23:44:11 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-10-14 12:06:00 +08:00
|
|
|
return allPRs, len(prs) < perPage, nil
|
2020-04-19 23:44:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetReviews returns pull requests review
|
2021-08-22 06:47:45 +08:00
|
|
|
func (g *GitlabDownloader) GetReviews(context base.IssueContext) ([]*base.Review, error) {
|
|
|
|
approvals, resp, err := g.client.MergeRequestApprovals.GetConfiguration(g.repoID, int(context.ForeignID()), gitlab.WithContext(g.ctx))
|
2020-04-20 20:30:46 +08:00
|
|
|
if err != nil {
|
2020-11-20 07:18:34 +08:00
|
|
|
if resp != nil && resp.StatusCode == 404 {
|
|
|
|
log.Error(fmt.Sprintf("GitlabDownloader: while migrating a error occurred: '%s'", err.Error()))
|
|
|
|
return []*base.Review{}, nil
|
|
|
|
}
|
2020-04-20 20:30:46 +08:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-08-18 08:47:18 +08:00
|
|
|
var reviews = make([]*base.Review, 0, len(approvals.ApprovedBy))
|
|
|
|
for _, user := range approvals.ApprovedBy {
|
2020-04-20 20:30:46 +08:00
|
|
|
reviews = append(reviews, &base.Review{
|
2021-08-22 06:47:45 +08:00
|
|
|
IssueIndex: context.LocalID(),
|
2021-08-18 08:47:18 +08:00
|
|
|
ReviewerID: int64(user.User.ID),
|
|
|
|
ReviewerName: user.User.Username,
|
|
|
|
CreatedAt: *approvals.UpdatedAt,
|
2020-04-20 20:30:46 +08:00
|
|
|
// All we get are approvals
|
|
|
|
State: base.ReviewStateApproved,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return reviews, nil
|
2020-04-19 23:44:11 +08:00
|
|
|
}
|
2020-09-03 15:35:17 +08:00
|
|
|
|
|
|
|
func (g *GitlabDownloader) awardToReaction(award *gitlab.AwardEmoji) *base.Reaction {
|
|
|
|
return &base.Reaction{
|
|
|
|
UserID: int64(award.User.ID),
|
|
|
|
UserName: award.User.Username,
|
|
|
|
Content: award.Name,
|
|
|
|
}
|
|
|
|
}
|