mirror of
https://github.com/go-gitea/gitea.git
synced 2026-10-26 06:16:56 +09:00
Supports migrating from Gitea-compatible forges like Forgejo and fixes many migration bugs. 1. Pre-release and `+gitea-` suffixed server versions no longer drop reviews, reactions and pagination. 2. Review states, dismissals, scoped and archived labels and merge bases are migrated. 3. Disabled units, external trackers, unavailable release assets, and GitLab confidential issues and internal notes are skipped. 4. Credentials are only sent to the source host, and `Retry-After` rate limits are waited out. 5. GitHub migrations of only issues or pull requests, basic auth clones and dismissed reviews work again. 6. The API returns 404 for releases of a disabled unit and shows migrated authors as mannequins, replacing the never populated `original_author` and `original_author_id` fields. Fixes: https://github.com/go-gitea/gitea/issues/19533 Fixes: https://github.com/go-gitea/gitea/issues/35885 Fixes: https://github.com/go-gitea/gitea/issues/18275 Fixes: https://github.com/go-gitea/gitea/issues/35069 Fixes: https://github.com/go-gitea/gitea/issues/17763 Fixes: https://github.com/go-gitea/gitea/issues/39548 Co-authored-by: bircni <bircni@icloud.com>
748 lines
21 KiB
Go
748 lines
21 KiB
Go
// Copyright 2020 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package migrations
|
|
|
|
import (
|
|
"cmp"
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"net/url"
|
|
"strings"
|
|
"time"
|
|
|
|
"gitea.dev/modules/container"
|
|
"gitea.dev/modules/log"
|
|
base "gitea.dev/modules/migration"
|
|
"gitea.dev/modules/structs"
|
|
"gitea.dev/modules/util"
|
|
gitea_sdk "gitea.dev/sdk"
|
|
|
|
"github.com/hashicorp/go-version"
|
|
)
|
|
|
|
var (
|
|
_ base.Downloader = &GiteaDownloader{}
|
|
_ base.DownloaderFactory = &GiteaDownloaderFactory{}
|
|
)
|
|
|
|
func init() {
|
|
RegisterDownloaderFactory(&GiteaDownloaderFactory{})
|
|
}
|
|
|
|
// GiteaDownloaderFactory defines a gitea downloader factory
|
|
type GiteaDownloaderFactory struct{}
|
|
|
|
// New returns a Downloader related to this factory according MigrateOptions
|
|
func (f *GiteaDownloaderFactory) New(ctx context.Context, opts base.MigrateOptions) (base.Downloader, error) {
|
|
u, err := url.Parse(opts.CloneAddr)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
baseURL := u.Scheme + "://" + u.Host
|
|
repoNameSpace := strings.TrimPrefix(u.Path, "/")
|
|
repoNameSpace = strings.TrimSuffix(repoNameSpace, ".git")
|
|
|
|
path := strings.Split(repoNameSpace, "/")
|
|
if len(path) < 2 {
|
|
return nil, fmt.Errorf("invalid path: %s", repoNameSpace)
|
|
}
|
|
|
|
repoPath := strings.Join(path[len(path)-2:], "/")
|
|
if len(path) > 2 {
|
|
subPath := strings.Join(path[:len(path)-2], "/")
|
|
baseURL += "/" + subPath
|
|
}
|
|
|
|
log.Trace("Create gitea downloader. BaseURL: %s RepoName: %s", baseURL, repoNameSpace)
|
|
|
|
return NewGiteaDownloader(ctx, baseURL, repoPath, opts.AuthUsername, opts.AuthPassword, opts.AuthToken)
|
|
}
|
|
|
|
// GitServiceType returns the type of git service
|
|
func (f *GiteaDownloaderFactory) GitServiceType() structs.GitServiceType {
|
|
return structs.GiteaService
|
|
}
|
|
|
|
// GiteaDownloader implements a Downloader interface to get repository information's
|
|
type GiteaDownloader struct {
|
|
base.NullDownloader
|
|
ctx context.Context
|
|
client *gitea_sdk.Client
|
|
baseURL string
|
|
repoOwner string
|
|
repoName string
|
|
httpClient *http.Client
|
|
pagination bool
|
|
maxPerPage int
|
|
}
|
|
|
|
// NewGiteaDownloader creates a gitea Downloader via gitea API
|
|
//
|
|
// Use either a username/password or personal token. token is preferred
|
|
// Note: Public access only allows very basic access
|
|
func NewGiteaDownloader(ctx context.Context, baseURL, repoPath, username, password, token string) (*GiteaDownloader, error) {
|
|
httpClient := newMigrationHTTPClient(baseURL, util.Iif(token != "", "token "+token, basicAuthorization(username, password)))
|
|
giteaClient, err := gitea_sdk.NewClient(baseURL, gitea_sdk.SetHTTPClient(httpClient))
|
|
if err != nil {
|
|
log.Error(fmt.Sprintf("Failed to create NewGiteaDownloader for: %s. Error: %v", baseURL, err))
|
|
return nil, err
|
|
}
|
|
if err = pinGiteaVersion(ctx, giteaClient); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
path := strings.Split(repoPath, "/")
|
|
|
|
paginationSupport := true
|
|
if err = giteaClient.CheckServerVersionConstraint(ctx, ">=1.12"); err != nil {
|
|
paginationSupport = false
|
|
}
|
|
|
|
// set small maxPerPage since we can only guess
|
|
// (default would be 50 but this can differ)
|
|
maxPerPage := 10
|
|
// gitea instances >=1.13 can tell us what maximum they have
|
|
apiConf, _, err := giteaClient.Settings.GetGlobalAPISettings(ctx)
|
|
if err != nil {
|
|
log.Info("Unable to get global API settings. Ignoring these.")
|
|
log.Debug("giteaClient.GetGlobalAPISettings. Error: %v", err)
|
|
} else if apiConf != nil && apiConf.MaxResponseItems > 0 {
|
|
maxPerPage = min(apiConf.MaxResponseItems, 100)
|
|
}
|
|
|
|
return &GiteaDownloader{
|
|
ctx: ctx,
|
|
client: giteaClient,
|
|
baseURL: baseURL,
|
|
repoOwner: path[0],
|
|
repoName: path[1],
|
|
httpClient: httpClient,
|
|
pagination: paginationSupport,
|
|
maxPerPage: maxPerPage,
|
|
}, nil
|
|
}
|
|
|
|
func pinGiteaVersion(ctx context.Context, client *gitea_sdk.Client) error { // the SDK's checks reject pre-releases
|
|
rawVersion, _, err := client.Meta.ServerVersion(ctx)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if serverVersion, err := version.NewVersion(rawVersion); err == nil {
|
|
if giteaVersion, ok := strings.CutPrefix(serverVersion.Metadata(), "gitea-"); ok { // Forgejo
|
|
if serverVersion, err = version.NewVersion(giteaVersion); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return gitea_sdk.SetGiteaVersion(serverVersion.Core().String())(client)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (g *GiteaDownloader) unitDisabled(resp *gitea_sdk.Response, hasUnit func(*gitea_sdk.Repository) bool) bool { // Gitea answers a disabled unit and missing access alike with 403 or 404, Forgejo with 404
|
|
if resp == nil || (resp.StatusCode != http.StatusNotFound && resp.StatusCode != http.StatusForbidden) {
|
|
return false
|
|
}
|
|
repo, _, err := g.client.Repositories.GetRepo(g.ctx, g.repoOwner, g.repoName)
|
|
return err == nil && !hasUnit(repo)
|
|
}
|
|
|
|
// String implements Stringer
|
|
func (g *GiteaDownloader) String() string {
|
|
return fmt.Sprintf("migration from gitea server %s %s/%s", g.baseURL, g.repoOwner, g.repoName)
|
|
}
|
|
|
|
func (g *GiteaDownloader) LogString() string {
|
|
if g == nil {
|
|
return "<GiteaDownloader nil>"
|
|
}
|
|
return fmt.Sprintf("<GiteaDownloader %s %s/%s>", g.baseURL, g.repoOwner, g.repoName)
|
|
}
|
|
|
|
// GetRepoInfo returns a repository information
|
|
func (g *GiteaDownloader) GetRepoInfo(_ context.Context) (*base.Repository, error) {
|
|
if g == nil {
|
|
return nil, errors.New("error: GiteaDownloader is nil")
|
|
}
|
|
|
|
repo, _, err := g.client.Repositories.GetRepo(g.ctx, g.repoOwner, g.repoName)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &base.Repository{
|
|
Name: repo.Name,
|
|
Owner: repo.Owner.UserName,
|
|
IsPrivate: repo.Private,
|
|
Description: repo.Description,
|
|
Website: repo.Website,
|
|
CloneURL: repo.CloneURL,
|
|
OriginalURL: repo.HTMLURL,
|
|
DefaultBranch: repo.DefaultBranch,
|
|
}, nil
|
|
}
|
|
|
|
// GetTopics return gitea topics
|
|
func (g *GiteaDownloader) GetTopics(_ context.Context) ([]string, error) {
|
|
topics, _, err := g.client.Repositories.ListRepoTopics(g.ctx, g.repoOwner, g.repoName, gitea_sdk.ListRepoTopicsOptions{})
|
|
return topics, err
|
|
}
|
|
|
|
// GetMilestones returns milestones
|
|
func (g *GiteaDownloader) GetMilestones(ctx context.Context) ([]*base.Milestone, error) {
|
|
milestones := make([]*base.Milestone, 0, g.maxPerPage)
|
|
|
|
for i := 1; ; i++ {
|
|
// make sure gitea can shutdown gracefully
|
|
select {
|
|
case <-ctx.Done():
|
|
return nil, nil
|
|
default:
|
|
}
|
|
|
|
ms, _, err := g.client.Repositories.ListMilestones(g.ctx, g.repoOwner, g.repoName, gitea_sdk.ListMilestoneOption{
|
|
ListOptions: gitea_sdk.ListOptions{
|
|
PageSize: g.maxPerPage,
|
|
Page: i,
|
|
},
|
|
State: gitea_sdk.StateAll,
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
for i := range ms {
|
|
// old gitea instances don't have this information
|
|
createdAT := time.Time{}
|
|
var updatedAT *time.Time
|
|
if ms[i].Closed != nil {
|
|
createdAT = *ms[i].Closed
|
|
updatedAT = ms[i].Closed
|
|
}
|
|
|
|
// new gitea instances (>=1.13) do
|
|
if !ms[i].Created.IsZero() {
|
|
createdAT = ms[i].Created
|
|
}
|
|
if ms[i].Updated != nil && !ms[i].Updated.IsZero() {
|
|
updatedAT = ms[i].Updated
|
|
}
|
|
|
|
milestones = append(milestones, &base.Milestone{
|
|
Title: ms[i].Title,
|
|
Description: ms[i].Description,
|
|
Deadline: ms[i].Deadline,
|
|
Created: createdAT,
|
|
Updated: updatedAT,
|
|
Closed: ms[i].Closed,
|
|
State: string(ms[i].State),
|
|
})
|
|
}
|
|
if !g.pagination || len(ms) < g.maxPerPage {
|
|
break
|
|
}
|
|
}
|
|
return milestones, nil
|
|
}
|
|
|
|
func (g *GiteaDownloader) convertGiteaLabel(label *gitea_sdk.Label) *base.Label {
|
|
return &base.Label{
|
|
Name: label.Name,
|
|
Color: label.Color,
|
|
Description: label.Description,
|
|
Exclusive: label.Exclusive,
|
|
Archived: label.IsArchived,
|
|
}
|
|
}
|
|
|
|
// GetLabels returns labels
|
|
func (g *GiteaDownloader) GetLabels(ctx context.Context) ([]*base.Label, error) {
|
|
labels := make([]*base.Label, 0, g.maxPerPage)
|
|
|
|
for i := 1; ; i++ {
|
|
// make sure gitea can shutdown gracefully
|
|
select {
|
|
case <-ctx.Done():
|
|
return nil, nil
|
|
default:
|
|
}
|
|
|
|
ls, _, err := g.client.Repositories.ListRepoLabels(g.ctx, g.repoOwner, g.repoName, gitea_sdk.ListLabelsOptions{ListOptions: gitea_sdk.ListOptions{
|
|
PageSize: g.maxPerPage,
|
|
Page: i,
|
|
}})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
for i := range ls {
|
|
labels = append(labels, g.convertGiteaLabel(ls[i]))
|
|
}
|
|
if !g.pagination || len(ls) < g.maxPerPage {
|
|
break
|
|
}
|
|
}
|
|
return labels, nil
|
|
}
|
|
|
|
func (g *GiteaDownloader) convertGiteaRelease(rel *gitea_sdk.Release) *base.Release {
|
|
r := &base.Release{
|
|
TagName: rel.TagName,
|
|
TargetCommitish: rel.Target,
|
|
Name: rel.Title,
|
|
Body: rel.Note,
|
|
Draft: rel.IsDraft,
|
|
Prerelease: rel.IsPrerelease,
|
|
PublisherID: rel.Publisher.ID,
|
|
PublisherName: rel.Publisher.UserName,
|
|
PublisherEmail: rel.Publisher.Email,
|
|
Published: rel.PublishedAt,
|
|
Created: rel.CreatedAt,
|
|
}
|
|
|
|
for _, asset := range rel.Attachments {
|
|
size := int(asset.Size)
|
|
dlCount := int(asset.DownloadCount)
|
|
r.Assets = append(r.Assets, &base.ReleaseAsset{
|
|
ID: asset.ID,
|
|
Name: asset.Name,
|
|
Size: &size,
|
|
DownloadCount: &dlCount,
|
|
Created: asset.Created,
|
|
DownloadFunc: func() (io.ReadCloser, error) {
|
|
return downloadAsset(g.ctx, g.httpClient, g.baseURL+"/attachments/"+url.PathEscape(asset.UUID))
|
|
},
|
|
})
|
|
}
|
|
return r
|
|
}
|
|
|
|
// GetReleases returns releases
|
|
func (g *GiteaDownloader) GetReleases(ctx context.Context) ([]*base.Release, error) {
|
|
releases := make([]*base.Release, 0, g.maxPerPage)
|
|
|
|
for i := 1; ; i++ {
|
|
// make sure gitea can shutdown gracefully
|
|
select {
|
|
case <-ctx.Done():
|
|
return nil, nil
|
|
default:
|
|
}
|
|
|
|
rl, resp, err := g.client.Releases.ListReleases(g.ctx, g.repoOwner, g.repoName, gitea_sdk.ListReleasesOptions{ListOptions: gitea_sdk.ListOptions{
|
|
PageSize: g.maxPerPage,
|
|
Page: i,
|
|
}})
|
|
if g.unitDisabled(resp, func(repo *gitea_sdk.Repository) bool { return repo.HasReleases }) {
|
|
return releases, nil
|
|
}
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
for i := range rl {
|
|
releases = append(releases, g.convertGiteaRelease(rl[i]))
|
|
}
|
|
if !g.pagination || len(rl) < g.maxPerPage {
|
|
break
|
|
}
|
|
}
|
|
return releases, nil
|
|
}
|
|
|
|
func (g *GiteaDownloader) getIssueReactions(ctx context.Context, index int64) ([]*base.Reaction, error) {
|
|
if err := g.client.CheckServerVersionConstraint(g.ctx, ">=1.11"); err != nil {
|
|
log.Info("GiteaDownloader: instance to old, skip getIssueReactions")
|
|
return nil, nil
|
|
}
|
|
|
|
allReactions := make([]*base.Reaction, 0, g.maxPerPage)
|
|
|
|
for i := 1; ; i++ {
|
|
// make sure gitea can shutdown gracefully
|
|
select {
|
|
case <-ctx.Done():
|
|
return nil, nil
|
|
default:
|
|
}
|
|
|
|
reactions, _, err := g.client.Issues.ListIssueReactions(g.ctx, g.repoOwner, g.repoName, index, gitea_sdk.ListIssueReactionsOptions{ListOptions: gitea_sdk.ListOptions{
|
|
PageSize: g.maxPerPage,
|
|
Page: i,
|
|
}})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
for _, reaction := range reactions {
|
|
allReactions = append(allReactions, &base.Reaction{
|
|
UserID: reaction.User.ID,
|
|
UserName: reaction.User.UserName,
|
|
Content: reaction.Reaction,
|
|
})
|
|
}
|
|
|
|
if !g.pagination || len(reactions) < g.maxPerPage {
|
|
break
|
|
}
|
|
}
|
|
return allReactions, nil
|
|
}
|
|
|
|
func (g *GiteaDownloader) getCommentReactions(commentID int64) ([]*base.Reaction, error) {
|
|
var reactions []*base.Reaction
|
|
if err := g.client.CheckServerVersionConstraint(g.ctx, ">=1.11"); err != nil {
|
|
log.Info("GiteaDownloader: instance to old, skip getCommentReactions")
|
|
return reactions, nil
|
|
}
|
|
rl, _, err := g.client.Issues.GetIssueCommentReactions(g.ctx, g.repoOwner, g.repoName, commentID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
for i := range rl {
|
|
reactions = append(reactions, &base.Reaction{
|
|
UserID: rl[i].User.ID,
|
|
UserName: rl[i].User.UserName,
|
|
Content: rl[i].Reaction,
|
|
})
|
|
}
|
|
return reactions, nil
|
|
}
|
|
|
|
// GetIssues returns issues according start and limit
|
|
func (g *GiteaDownloader) GetIssues(ctx context.Context, page, perPage int) ([]*base.Issue, bool, error) {
|
|
if perPage > g.maxPerPage {
|
|
perPage = g.maxPerPage
|
|
}
|
|
allIssues := make([]*base.Issue, 0, perPage)
|
|
|
|
issues, resp, err := g.client.Issues.ListRepoIssues(g.ctx, g.repoOwner, g.repoName, gitea_sdk.ListIssueOption{
|
|
ListOptions: gitea_sdk.ListOptions{Page: page, PageSize: perPage},
|
|
State: gitea_sdk.StateAll,
|
|
Type: gitea_sdk.IssueTypeIssue,
|
|
})
|
|
if g.unitDisabled(resp, func(repo *gitea_sdk.Repository) bool { return repo.HasIssues && repo.ExternalTracker == nil }) {
|
|
return nil, true, nil
|
|
}
|
|
if err != nil {
|
|
return nil, false, fmt.Errorf("error while listing issues: %w", err)
|
|
}
|
|
for _, issue := range issues {
|
|
labels := make([]*base.Label, 0, len(issue.Labels))
|
|
for i := range issue.Labels {
|
|
labels = append(labels, g.convertGiteaLabel(issue.Labels[i]))
|
|
}
|
|
|
|
var milestone string
|
|
if issue.Milestone != nil {
|
|
milestone = issue.Milestone.Title
|
|
}
|
|
|
|
reactions, err := g.getIssueReactions(ctx, issue.Index)
|
|
if err != nil {
|
|
WarnAndNotice("Unable to load reactions during migrating issue #%d in %s. Error: %v", issue.Index, g, err)
|
|
}
|
|
|
|
var assignees []string
|
|
for i := range issue.Assignees {
|
|
assignees = append(assignees, issue.Assignees[i].UserName)
|
|
}
|
|
|
|
allIssues = append(allIssues, &base.Issue{
|
|
Title: issue.Title,
|
|
Number: issue.Index,
|
|
PosterID: issue.Poster.ID,
|
|
PosterName: issue.Poster.UserName,
|
|
PosterEmail: issue.Poster.Email,
|
|
Content: issue.Body,
|
|
Milestone: milestone,
|
|
State: string(issue.State),
|
|
Created: issue.Created,
|
|
Updated: issue.Updated,
|
|
Closed: issue.Closed,
|
|
Reactions: reactions,
|
|
Labels: labels,
|
|
Assignees: assignees,
|
|
IsLocked: issue.IsLocked,
|
|
ForeignIndex: issue.Index,
|
|
Context: issue.Comments,
|
|
})
|
|
}
|
|
|
|
isEnd := len(issues) < perPage
|
|
if !g.pagination {
|
|
isEnd = len(issues) == 0
|
|
}
|
|
return allIssues, isEnd, nil
|
|
}
|
|
|
|
// GetComments returns comments according issueNumber
|
|
func (g *GiteaDownloader) GetComments(ctx context.Context, commentable base.Commentable) ([]*base.Comment, bool, error) {
|
|
if commentable.GetContext() == 0 {
|
|
return nil, true, nil
|
|
}
|
|
allComments := make([]*base.Comment, 0, g.maxPerPage)
|
|
seenIDs := container.Set[int64]{}
|
|
|
|
for i := 1; ; i++ {
|
|
// make sure gitea can shutdown gracefully
|
|
select {
|
|
case <-ctx.Done():
|
|
return nil, false, nil
|
|
default:
|
|
}
|
|
|
|
comments, _, err := g.client.Issues.ListIssueComments(g.ctx, g.repoOwner, g.repoName, commentable.GetForeignIndex(), gitea_sdk.ListIssueCommentOptions{ListOptions: gitea_sdk.ListOptions{
|
|
PageSize: g.maxPerPage,
|
|
Page: i,
|
|
}})
|
|
if err != nil {
|
|
return nil, false, fmt.Errorf("error while listing comments for issue #%d. Error: %w", commentable.GetForeignIndex(), err)
|
|
}
|
|
if len(comments) == 0 || seenIDs.Contains(comments[0].ID) {
|
|
break // the endpoint ignores page and limit, so page 2 repeats page 1
|
|
}
|
|
|
|
for _, comment := range comments {
|
|
seenIDs.Add(comment.ID)
|
|
reactions, err := g.getCommentReactions(comment.ID)
|
|
if err != nil {
|
|
WarnAndNotice("Unable to load comment reactions during migrating issue #%d for comment %d in %s. Error: %v", commentable.GetForeignIndex(), comment.ID, g, err)
|
|
}
|
|
|
|
allComments = append(allComments, &base.Comment{
|
|
IssueIndex: commentable.GetLocalIndex(),
|
|
Index: comment.ID,
|
|
PosterID: comment.Poster.ID,
|
|
PosterName: comment.Poster.UserName,
|
|
PosterEmail: comment.Poster.Email,
|
|
Content: comment.Body,
|
|
Created: comment.Created,
|
|
Updated: comment.Updated,
|
|
Reactions: reactions,
|
|
})
|
|
}
|
|
|
|
if !g.pagination || len(comments) != g.maxPerPage {
|
|
break
|
|
}
|
|
}
|
|
return allComments, true, nil
|
|
}
|
|
|
|
// GetPullRequests returns pull requests according page and perPage
|
|
func (g *GiteaDownloader) GetPullRequests(ctx context.Context, page, perPage int) ([]*base.PullRequest, bool, error) {
|
|
if perPage > g.maxPerPage {
|
|
perPage = g.maxPerPage
|
|
}
|
|
allPRs := make([]*base.PullRequest, 0, perPage)
|
|
|
|
prs, resp, err := g.client.PullRequests.ListRepoPullRequests(g.ctx, g.repoOwner, g.repoName, gitea_sdk.ListPullRequestsOptions{
|
|
ListOptions: gitea_sdk.ListOptions{
|
|
Page: page,
|
|
PageSize: perPage,
|
|
},
|
|
State: gitea_sdk.StateAll,
|
|
})
|
|
if g.unitDisabled(resp, func(repo *gitea_sdk.Repository) bool { return repo.HasPullRequests }) {
|
|
return nil, true, nil
|
|
}
|
|
if err != nil {
|
|
return nil, false, fmt.Errorf("error while listing pull requests (page: %d, pagesize: %d). Error: %w", page, perPage, err)
|
|
}
|
|
for _, pr := range prs {
|
|
var milestone string
|
|
if pr.Milestone != nil {
|
|
milestone = pr.Milestone.Title
|
|
}
|
|
|
|
labels := make([]*base.Label, 0, len(pr.Labels))
|
|
for i := range pr.Labels {
|
|
labels = append(labels, g.convertGiteaLabel(pr.Labels[i]))
|
|
}
|
|
|
|
var (
|
|
headUserName string
|
|
headRepoName string
|
|
headCloneURL string
|
|
headRef string
|
|
headSHA string
|
|
)
|
|
if pr.Head != nil {
|
|
if pr.Head.Repository != nil {
|
|
headUserName = pr.Head.Repository.Owner.UserName
|
|
headRepoName = pr.Head.Repository.Name
|
|
headCloneURL = pr.Head.Repository.CloneURL
|
|
}
|
|
headSHA = pr.Head.Sha
|
|
headRef = pr.Head.Ref
|
|
}
|
|
|
|
var mergeCommitSHA string
|
|
if pr.MergedCommitID != nil {
|
|
mergeCommitSHA = *pr.MergedCommitID
|
|
}
|
|
|
|
reactions, err := g.getIssueReactions(ctx, pr.Index)
|
|
if err != nil {
|
|
WarnAndNotice("Unable to load reactions during migrating pull #%d in %s. Error: %v", pr.Index, g, err)
|
|
}
|
|
|
|
var assignees []string
|
|
for i := range pr.Assignees {
|
|
assignees = append(assignees, pr.Assignees[i].UserName)
|
|
}
|
|
|
|
createdAt := time.Time{}
|
|
if pr.Created != nil {
|
|
createdAt = *pr.Created
|
|
}
|
|
updatedAt := time.Time{}
|
|
if pr.Updated != nil {
|
|
updatedAt = *pr.Updated
|
|
}
|
|
|
|
closedAt := pr.Closed
|
|
if pr.Merged != nil && closedAt == nil {
|
|
closedAt = pr.Merged
|
|
}
|
|
|
|
allPRs = append(allPRs, &base.PullRequest{
|
|
Title: pr.Title,
|
|
Number: pr.Index,
|
|
PosterID: pr.Poster.ID,
|
|
PosterName: pr.Poster.UserName,
|
|
PosterEmail: pr.Poster.Email,
|
|
Content: pr.Body,
|
|
State: string(pr.State),
|
|
Created: createdAt,
|
|
Updated: updatedAt,
|
|
Closed: closedAt,
|
|
Labels: labels,
|
|
Milestone: milestone,
|
|
Reactions: reactions,
|
|
Assignees: assignees,
|
|
Merged: pr.HasMerged,
|
|
MergedTime: pr.Merged,
|
|
MergeCommitSHA: mergeCommitSHA,
|
|
IsLocked: pr.IsLocked,
|
|
Head: base.PullRequestBranch{
|
|
Ref: headRef,
|
|
SHA: headSHA,
|
|
RepoName: headRepoName,
|
|
OwnerName: headUserName,
|
|
CloneURL: headCloneURL,
|
|
},
|
|
Base: base.PullRequestBranch{
|
|
Ref: pr.Base.Ref,
|
|
SHA: cmp.Or(pr.MergeBase, pr.Base.Sha), // base.sha is the base branch head, which contains the changes once merged
|
|
RepoName: g.repoName,
|
|
OwnerName: g.repoOwner,
|
|
},
|
|
ForeignIndex: pr.Index,
|
|
Context: pr.Comments,
|
|
})
|
|
// SECURITY: Ensure that the PR is safe
|
|
_ = CheckAndEnsureSafePR(allPRs[len(allPRs)-1], g.baseURL, g)
|
|
}
|
|
|
|
isEnd := len(prs) < perPage
|
|
if !g.pagination {
|
|
isEnd = len(prs) == 0
|
|
}
|
|
return allPRs, isEnd, nil
|
|
}
|
|
|
|
// GetReviews returns pull requests review
|
|
func (g *GiteaDownloader) GetReviews(ctx context.Context, reviewable base.Reviewable) ([]*base.Review, error) {
|
|
if err := g.client.CheckServerVersionConstraint(g.ctx, ">=1.12"); err != nil {
|
|
log.Info("GiteaDownloader: instance to old, skip GetReviews")
|
|
return nil, nil
|
|
}
|
|
|
|
allReviews := make([]*base.Review, 0, g.maxPerPage)
|
|
|
|
for i := 1; ; i++ {
|
|
// make sure gitea can shutdown gracefully
|
|
select {
|
|
case <-ctx.Done():
|
|
return nil, nil
|
|
default:
|
|
}
|
|
|
|
prl, _, err := g.client.PullRequests.ListPullReviews(g.ctx, g.repoOwner, g.repoName, reviewable.GetForeignIndex(), gitea_sdk.ListPullReviewsOptions{ListOptions: gitea_sdk.ListOptions{
|
|
Page: i,
|
|
PageSize: g.maxPerPage,
|
|
}})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
for _, pr := range prl {
|
|
if pr.Reviewer == nil {
|
|
// Presumably this is a team review which we cannot migrate at present but we have to skip this review as otherwise the review will be mapped on to an incorrect user.
|
|
// TODO: handle team reviews
|
|
continue
|
|
}
|
|
|
|
var rcl []*gitea_sdk.PullReviewComment
|
|
if pr.CodeCommentsCount > 0 {
|
|
if rcl, _, err = g.client.PullRequests.ListPullReviewComments(g.ctx, g.repoOwner, g.repoName, reviewable.GetForeignIndex(), pr.ID); err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
var reviewComments []*base.ReviewComment
|
|
for i := range rcl {
|
|
line := int(rcl[i].LineNum)
|
|
if rcl[i].OldLineNum > 0 {
|
|
line = int(rcl[i].OldLineNum) * -1
|
|
}
|
|
|
|
reviewComments = append(reviewComments, &base.ReviewComment{
|
|
ID: rcl[i].ID,
|
|
Content: rcl[i].Body,
|
|
TreePath: rcl[i].Path,
|
|
DiffHunk: rcl[i].DiffHunk,
|
|
Line: line,
|
|
CommitID: rcl[i].CommitID,
|
|
PosterID: rcl[i].Reviewer.ID,
|
|
CreatedAt: rcl[i].Created,
|
|
UpdatedAt: rcl[i].Updated,
|
|
})
|
|
}
|
|
|
|
review := &base.Review{
|
|
ID: pr.ID,
|
|
IssueIndex: reviewable.GetLocalIndex(),
|
|
ReviewerID: pr.Reviewer.ID,
|
|
ReviewerName: pr.Reviewer.UserName,
|
|
Official: pr.Official,
|
|
CommitID: pr.CommitID,
|
|
Content: pr.Body,
|
|
CreatedAt: pr.Submitted,
|
|
State: string(pr.State),
|
|
Dismissed: pr.Dismissed,
|
|
Comments: reviewComments,
|
|
}
|
|
switch pr.State {
|
|
case gitea_sdk.ReviewStateComment:
|
|
review.State = base.ReviewStateCommented
|
|
case gitea_sdk.ReviewStateRequestChanges:
|
|
review.State = base.ReviewStateChangesRequested
|
|
}
|
|
|
|
allReviews = append(allReviews, review)
|
|
}
|
|
|
|
if len(prl) < g.maxPerPage {
|
|
break
|
|
}
|
|
}
|
|
return allReviews, nil
|
|
}
|