2019-05-07 06:42:51 +05:30
|
|
|
// Copyright 2019 The Gitea Authors. All rights reserved.
|
|
|
|
// Copyright 2018 Jonas Franz. 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 (
|
2019-12-17 09:46:54 +05:30
|
|
|
"context"
|
2019-05-07 06:42:51 +05:30
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"code.gitea.io/gitea/models"
|
2021-09-19 17:19:59 +05:30
|
|
|
"code.gitea.io/gitea/models/db"
|
2021-11-19 19:09:57 +05:30
|
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
2021-11-24 15:19:20 +05:30
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2019-05-07 06:42:51 +05:30
|
|
|
"code.gitea.io/gitea/modules/git"
|
|
|
|
"code.gitea.io/gitea/modules/log"
|
2021-11-16 20:55:33 +05:30
|
|
|
base "code.gitea.io/gitea/modules/migration"
|
2020-01-12 17:41:17 +05:30
|
|
|
repo_module "code.gitea.io/gitea/modules/repository"
|
2019-05-07 06:42:51 +05:30
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2020-08-18 09:53:45 +05:30
|
|
|
"code.gitea.io/gitea/modules/storage"
|
2019-10-13 18:53:14 +05:30
|
|
|
"code.gitea.io/gitea/modules/structs"
|
2019-08-15 20:16:21 +05:30
|
|
|
"code.gitea.io/gitea/modules/timeutil"
|
2020-12-27 09:04:19 +05:30
|
|
|
"code.gitea.io/gitea/modules/uri"
|
2020-10-28 03:04:56 +05:30
|
|
|
"code.gitea.io/gitea/services/pull"
|
2019-05-07 06:42:51 +05:30
|
|
|
|
2020-06-18 14:48:44 +05:30
|
|
|
gouuid "github.com/google/uuid"
|
2019-05-07 06:42:51 +05:30
|
|
|
)
|
|
|
|
|
2022-01-20 23:16:10 +05:30
|
|
|
var _ base.Uploader = &GiteaLocalUploader{}
|
2019-05-07 06:42:51 +05:30
|
|
|
|
|
|
|
// GiteaLocalUploader implements an Uploader to gitea sites
|
|
|
|
type GiteaLocalUploader struct {
|
2019-12-17 09:46:54 +05:30
|
|
|
ctx context.Context
|
2021-11-24 15:19:20 +05:30
|
|
|
doer *user_model.User
|
2019-10-14 11:40:42 +05:30
|
|
|
repoOwner string
|
|
|
|
repoName string
|
2021-12-10 06:57:50 +05:30
|
|
|
repo *repo_model.Repository
|
2019-10-14 11:40:42 +05:30
|
|
|
labels sync.Map
|
|
|
|
milestones sync.Map
|
|
|
|
issues sync.Map
|
|
|
|
gitRepo *git.Repository
|
|
|
|
prHeadCache map[string]struct{}
|
|
|
|
userMap map[int64]int64 // external user id mapping to user id
|
2020-01-23 22:58:15 +05:30
|
|
|
prCache map[int64]*models.PullRequest
|
2019-10-14 11:40:42 +05:30
|
|
|
gitServiceType structs.GitServiceType
|
2019-05-07 06:42:51 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// NewGiteaLocalUploader creates an gitea Uploader via gitea API v1
|
2021-11-24 15:19:20 +05:30
|
|
|
func NewGiteaLocalUploader(ctx context.Context, doer *user_model.User, repoOwner, repoName string) *GiteaLocalUploader {
|
2019-05-07 06:42:51 +05:30
|
|
|
return &GiteaLocalUploader{
|
2019-12-17 09:46:54 +05:30
|
|
|
ctx: ctx,
|
2019-05-07 06:42:51 +05:30
|
|
|
doer: doer,
|
|
|
|
repoOwner: repoOwner,
|
|
|
|
repoName: repoName,
|
|
|
|
prHeadCache: make(map[string]struct{}),
|
2019-10-14 11:40:42 +05:30
|
|
|
userMap: make(map[int64]int64),
|
2020-01-23 22:58:15 +05:30
|
|
|
prCache: make(map[int64]*models.PullRequest),
|
2019-05-07 06:42:51 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-07 00:54:50 +05:30
|
|
|
// MaxBatchInsertSize returns the table's max batch insert size
|
|
|
|
func (g *GiteaLocalUploader) MaxBatchInsertSize(tp string) int {
|
|
|
|
switch tp {
|
|
|
|
case "issue":
|
2021-09-19 17:19:59 +05:30
|
|
|
return db.MaxBatchInsertSize(new(models.Issue))
|
2019-07-07 00:54:50 +05:30
|
|
|
case "comment":
|
2021-09-19 17:19:59 +05:30
|
|
|
return db.MaxBatchInsertSize(new(models.Comment))
|
2019-07-07 00:54:50 +05:30
|
|
|
case "milestone":
|
2021-09-19 17:19:59 +05:30
|
|
|
return db.MaxBatchInsertSize(new(models.Milestone))
|
2019-07-07 00:54:50 +05:30
|
|
|
case "label":
|
2021-09-19 17:19:59 +05:30
|
|
|
return db.MaxBatchInsertSize(new(models.Label))
|
2019-07-07 00:54:50 +05:30
|
|
|
case "release":
|
2021-09-19 17:19:59 +05:30
|
|
|
return db.MaxBatchInsertSize(new(models.Release))
|
2019-07-07 00:54:50 +05:30
|
|
|
case "pullrequest":
|
2021-09-19 17:19:59 +05:30
|
|
|
return db.MaxBatchInsertSize(new(models.PullRequest))
|
2019-07-07 00:54:50 +05:30
|
|
|
}
|
|
|
|
return 10
|
|
|
|
}
|
|
|
|
|
2020-12-27 09:04:19 +05:30
|
|
|
// CreateRepo creates a repository
|
|
|
|
func (g *GiteaLocalUploader) CreateRepo(repo *base.Repository, opts base.MigrateOptions) error {
|
2021-11-24 15:19:20 +05:30
|
|
|
owner, err := user_model.GetUserByName(g.repoOwner)
|
2020-12-27 09:04:19 +05:30
|
|
|
if err != nil {
|
|
|
|
return err
|
2019-08-21 01:51:07 +05:30
|
|
|
}
|
|
|
|
|
2021-12-10 06:57:50 +05:30
|
|
|
var r *repo_model.Repository
|
2019-10-13 18:53:14 +05:30
|
|
|
if opts.MigrateToRepoID <= 0 {
|
2020-01-12 17:41:17 +05:30
|
|
|
r, err = repo_module.CreateRepository(g.doer, owner, models.CreateRepoOptions{
|
2020-01-10 21:05:17 +05:30
|
|
|
Name: g.repoName,
|
|
|
|
Description: repo.Description,
|
|
|
|
OriginalURL: repo.OriginalURL,
|
|
|
|
GitServiceType: opts.GitServiceType,
|
|
|
|
IsPrivate: opts.Private,
|
|
|
|
IsMirror: opts.Mirror,
|
2021-12-10 06:57:50 +05:30
|
|
|
Status: repo_model.RepositoryBeingMigrated,
|
2019-10-13 18:53:14 +05:30
|
|
|
})
|
|
|
|
} else {
|
2021-12-10 06:57:50 +05:30
|
|
|
r, err = repo_model.GetRepositoryByID(opts.MigrateToRepoID)
|
2019-10-13 18:53:14 +05:30
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-09-15 20:07:44 +05:30
|
|
|
r.DefaultBranch = repo.DefaultBranch
|
2021-11-18 20:58:10 +05:30
|
|
|
r.Description = repo.Description
|
2019-10-13 18:53:14 +05:30
|
|
|
|
2021-11-08 12:34:13 +05:30
|
|
|
r, err = repo_module.MigrateRepositoryGitData(g.ctx, owner, r, base.MigrateOptions{
|
2019-10-14 11:40:42 +05:30
|
|
|
RepoName: g.repoName,
|
|
|
|
Description: repo.Description,
|
|
|
|
OriginalURL: repo.OriginalURL,
|
|
|
|
GitServiceType: opts.GitServiceType,
|
|
|
|
Mirror: repo.IsMirror,
|
2021-04-09 03:55:57 +05:30
|
|
|
LFS: opts.LFS,
|
|
|
|
LFSEndpoint: opts.LFSEndpoint,
|
2021-01-22 01:03:58 +05:30
|
|
|
CloneAddr: repo.CloneURL,
|
2019-10-14 11:40:42 +05:30
|
|
|
Private: repo.IsPrivate,
|
|
|
|
Wiki: opts.Wiki,
|
|
|
|
Releases: opts.Releases, // if didn't get releases, then sync them from tags
|
2021-01-03 05:17:47 +05:30
|
|
|
MirrorInterval: opts.MirrorInterval,
|
2021-11-20 15:04:05 +05:30
|
|
|
}, NewMigrationHTTPTransport())
|
2019-10-13 18:53:14 +05:30
|
|
|
|
2019-05-26 02:48:27 +05:30
|
|
|
g.repo = r
|
2019-05-07 06:42:51 +05:30
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-01-20 04:56:57 +05:30
|
|
|
g.gitRepo, err = git.OpenRepositoryCtx(g.ctx, r.RepoPath())
|
2019-05-07 06:42:51 +05:30
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-11-13 12:31:19 +05:30
|
|
|
// Close closes this uploader
|
|
|
|
func (g *GiteaLocalUploader) Close() {
|
|
|
|
if g.gitRepo != nil {
|
|
|
|
g.gitRepo.Close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-14 11:46:12 +05:30
|
|
|
// CreateTopics creates topics
|
|
|
|
func (g *GiteaLocalUploader) CreateTopics(topics ...string) error {
|
2020-12-27 06:53:57 +05:30
|
|
|
// ignore topics to long for the db
|
|
|
|
c := 0
|
|
|
|
for i := range topics {
|
|
|
|
if len(topics[i]) <= 50 {
|
|
|
|
topics[c] = topics[i]
|
|
|
|
c++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
topics = topics[:c]
|
2021-12-12 21:18:20 +05:30
|
|
|
return repo_model.SaveTopics(g.repo.ID, topics...)
|
2019-08-14 11:46:12 +05:30
|
|
|
}
|
|
|
|
|
2019-06-29 19:08:22 +05:30
|
|
|
// CreateMilestones creates milestones
|
|
|
|
func (g *GiteaLocalUploader) CreateMilestones(milestones ...*base.Milestone) error {
|
2022-01-20 23:16:10 +05:30
|
|
|
mss := make([]*models.Milestone, 0, len(milestones))
|
2019-06-29 19:08:22 +05:30
|
|
|
for _, milestone := range milestones {
|
2019-08-15 20:16:21 +05:30
|
|
|
var deadline timeutil.TimeStamp
|
2019-06-29 19:08:22 +05:30
|
|
|
if milestone.Deadline != nil {
|
2019-08-15 20:16:21 +05:30
|
|
|
deadline = timeutil.TimeStamp(milestone.Deadline.Unix())
|
2019-06-29 19:08:22 +05:30
|
|
|
}
|
|
|
|
if deadline == 0 {
|
2019-08-15 20:16:21 +05:30
|
|
|
deadline = timeutil.TimeStamp(time.Date(9999, 1, 1, 0, 0, 0, 0, setting.DefaultUILocation).Unix())
|
2019-06-29 19:08:22 +05:30
|
|
|
}
|
2021-08-18 06:17:18 +05:30
|
|
|
|
|
|
|
if milestone.Created.IsZero() {
|
|
|
|
if milestone.Updated != nil {
|
|
|
|
milestone.Created = *milestone.Updated
|
|
|
|
} else if milestone.Deadline != nil {
|
|
|
|
milestone.Created = *milestone.Deadline
|
|
|
|
} else {
|
|
|
|
milestone.Created = time.Now()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if milestone.Updated == nil || milestone.Updated.IsZero() {
|
|
|
|
milestone.Updated = &milestone.Created
|
|
|
|
}
|
|
|
|
|
2022-01-20 23:16:10 +05:30
|
|
|
ms := models.Milestone{
|
2019-06-29 19:08:22 +05:30
|
|
|
RepoID: g.repo.ID,
|
|
|
|
Name: milestone.Title,
|
|
|
|
Content: milestone.Description,
|
2019-07-29 21:11:22 +05:30
|
|
|
IsClosed: milestone.State == "closed",
|
2021-08-18 06:17:18 +05:30
|
|
|
CreatedUnix: timeutil.TimeStamp(milestone.Created.Unix()),
|
|
|
|
UpdatedUnix: timeutil.TimeStamp(milestone.Updated.Unix()),
|
2019-06-29 19:08:22 +05:30
|
|
|
DeadlineUnix: deadline,
|
|
|
|
}
|
|
|
|
if ms.IsClosed && milestone.Closed != nil {
|
2019-08-15 20:16:21 +05:30
|
|
|
ms.ClosedDateUnix = timeutil.TimeStamp(milestone.Closed.Unix())
|
2019-06-29 19:08:22 +05:30
|
|
|
}
|
|
|
|
mss = append(mss, &ms)
|
2019-05-07 06:42:51 +05:30
|
|
|
}
|
|
|
|
|
2019-06-29 19:08:22 +05:30
|
|
|
err := models.InsertMilestones(mss...)
|
2019-05-07 06:42:51 +05:30
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-06-29 19:08:22 +05:30
|
|
|
for _, ms := range mss {
|
|
|
|
g.milestones.Store(ms.Name, ms.ID)
|
2019-05-07 06:42:51 +05:30
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-06-29 19:08:22 +05:30
|
|
|
// CreateLabels creates labels
|
|
|
|
func (g *GiteaLocalUploader) CreateLabels(labels ...*base.Label) error {
|
2022-01-20 23:16:10 +05:30
|
|
|
lbs := make([]*models.Label, 0, len(labels))
|
2019-06-29 19:08:22 +05:30
|
|
|
for _, label := range labels {
|
|
|
|
lbs = append(lbs, &models.Label{
|
|
|
|
RepoID: g.repo.ID,
|
|
|
|
Name: label.Name,
|
|
|
|
Description: label.Description,
|
|
|
|
Color: fmt.Sprintf("#%s", label.Color),
|
|
|
|
})
|
2019-05-07 06:42:51 +05:30
|
|
|
}
|
|
|
|
|
2019-06-29 19:08:22 +05:30
|
|
|
err := models.NewLabels(lbs...)
|
2019-05-07 06:42:51 +05:30
|
|
|
if err != nil {
|
2019-06-29 19:08:22 +05:30
|
|
|
return err
|
2019-05-07 06:42:51 +05:30
|
|
|
}
|
2019-06-29 19:08:22 +05:30
|
|
|
for _, lb := range lbs {
|
|
|
|
g.labels.Store(lb.Name, lb)
|
2019-05-07 06:42:51 +05:30
|
|
|
}
|
2019-06-29 19:08:22 +05:30
|
|
|
return nil
|
|
|
|
}
|
2019-05-07 06:42:51 +05:30
|
|
|
|
2019-06-29 19:08:22 +05:30
|
|
|
// CreateReleases creates releases
|
2020-12-27 09:04:19 +05:30
|
|
|
func (g *GiteaLocalUploader) CreateReleases(releases ...*base.Release) error {
|
2022-01-20 23:16:10 +05:30
|
|
|
rels := make([]*models.Release, 0, len(releases))
|
2019-06-29 19:08:22 +05:30
|
|
|
for _, release := range releases {
|
2021-08-18 06:17:18 +05:30
|
|
|
if release.Created.IsZero() {
|
|
|
|
if !release.Published.IsZero() {
|
|
|
|
release.Created = release.Published
|
|
|
|
} else {
|
|
|
|
release.Created = time.Now()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-20 23:16:10 +05:30
|
|
|
rel := models.Release{
|
2019-10-14 11:40:42 +05:30
|
|
|
RepoID: g.repo.ID,
|
|
|
|
TagName: release.TagName,
|
|
|
|
LowerTagName: strings.ToLower(release.TagName),
|
|
|
|
Target: release.TargetCommitish,
|
|
|
|
Title: release.Name,
|
|
|
|
Sha1: release.TargetCommitish,
|
|
|
|
Note: release.Body,
|
|
|
|
IsDraft: release.Draft,
|
|
|
|
IsPrerelease: release.Prerelease,
|
|
|
|
IsTag: false,
|
|
|
|
CreatedUnix: timeutil.TimeStamp(release.Created.Unix()),
|
|
|
|
}
|
|
|
|
|
|
|
|
userid, ok := g.userMap[release.PublisherID]
|
|
|
|
tp := g.gitServiceType.Name()
|
|
|
|
if !ok && tp != "" {
|
|
|
|
var err error
|
2021-11-28 19:41:58 +05:30
|
|
|
userid, err = user_model.GetUserIDByExternalUserID(tp, fmt.Sprintf("%v", release.PublisherID))
|
2019-10-14 11:40:42 +05:30
|
|
|
if err != nil {
|
|
|
|
log.Error("GetUserIDByExternalUserID: %v", err)
|
|
|
|
}
|
|
|
|
if userid > 0 {
|
|
|
|
g.userMap[release.PublisherID] = userid
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if userid > 0 {
|
|
|
|
rel.PublisherID = userid
|
|
|
|
} else {
|
|
|
|
rel.PublisherID = g.doer.ID
|
|
|
|
rel.OriginalAuthor = release.PublisherName
|
|
|
|
rel.OriginalAuthorID = release.PublisherID
|
2019-05-07 06:42:51 +05:30
|
|
|
}
|
|
|
|
|
2021-05-16 04:07:17 +05:30
|
|
|
// calc NumCommits if no draft
|
|
|
|
if !release.Draft {
|
2021-05-30 01:34:58 +05:30
|
|
|
commit, err := g.gitRepo.GetTagCommit(rel.TagName)
|
2021-05-16 04:07:17 +05:30
|
|
|
if err != nil {
|
2021-08-31 06:48:15 +05:30
|
|
|
return fmt.Errorf("GetTagCommit[%v]: %v", rel.TagName, err)
|
2021-05-16 04:07:17 +05:30
|
|
|
}
|
|
|
|
rel.NumCommits, err = commit.CommitsCount()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("CommitsCount: %v", err)
|
|
|
|
}
|
2019-05-07 06:42:51 +05:30
|
|
|
}
|
|
|
|
|
2019-06-29 19:08:22 +05:30
|
|
|
for _, asset := range release.Assets {
|
2021-08-18 06:17:18 +05:30
|
|
|
if asset.Created.IsZero() {
|
|
|
|
if !asset.Updated.IsZero() {
|
|
|
|
asset.Created = asset.Updated
|
|
|
|
} else {
|
|
|
|
asset.Created = release.Created
|
|
|
|
}
|
|
|
|
}
|
2022-01-20 23:16:10 +05:30
|
|
|
attach := repo_model.Attachment{
|
2020-06-18 14:48:44 +05:30
|
|
|
UUID: gouuid.New().String(),
|
2019-06-29 19:08:22 +05:30
|
|
|
Name: asset.Name,
|
|
|
|
DownloadCount: int64(*asset.DownloadCount),
|
|
|
|
Size: int64(*asset.Size),
|
2019-08-15 20:16:21 +05:30
|
|
|
CreatedUnix: timeutil.TimeStamp(asset.Created.Unix()),
|
2019-06-29 19:08:22 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// download attachment
|
2021-05-16 04:07:17 +05:30
|
|
|
err := func() error {
|
2020-12-27 09:04:19 +05:30
|
|
|
// asset.DownloadURL maybe a local file
|
2020-10-14 09:36:00 +05:30
|
|
|
var rc io.ReadCloser
|
2021-05-16 04:07:17 +05:30
|
|
|
var err error
|
2021-06-04 18:44:20 +05:30
|
|
|
if asset.DownloadFunc != nil {
|
2020-12-27 09:04:19 +05:30
|
|
|
rc, err = asset.DownloadFunc()
|
2020-10-14 09:36:00 +05:30
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-06-04 18:44:20 +05:30
|
|
|
} else if asset.DownloadURL != nil {
|
2020-12-27 09:04:19 +05:30
|
|
|
rc, err = uri.Open(*asset.DownloadURL)
|
2020-10-14 09:36:00 +05:30
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-11-05 18:24:47 +05:30
|
|
|
}
|
2021-06-04 18:44:20 +05:30
|
|
|
if rc == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2021-04-03 21:49:59 +05:30
|
|
|
_, err = storage.Attachments.Save(attach.RelativePath(), rc, int64(*asset.Size))
|
2021-06-04 18:44:20 +05:30
|
|
|
rc.Close()
|
2019-11-05 18:24:47 +05:30
|
|
|
return err
|
|
|
|
}()
|
|
|
|
if err != nil {
|
2019-06-29 19:08:22 +05:30
|
|
|
return err
|
|
|
|
}
|
2020-12-27 09:04:19 +05:30
|
|
|
|
2019-06-29 19:08:22 +05:30
|
|
|
rel.Attachments = append(rel.Attachments, &attach)
|
2019-05-07 06:42:51 +05:30
|
|
|
}
|
|
|
|
|
2019-06-29 19:08:22 +05:30
|
|
|
rels = append(rels, &rel)
|
2019-05-07 06:42:51 +05:30
|
|
|
}
|
2019-07-02 02:47:16 +05:30
|
|
|
|
2019-12-12 05:50:11 +05:30
|
|
|
return models.InsertReleases(rels...)
|
|
|
|
}
|
2019-12-11 11:39:06 +05:30
|
|
|
|
2019-12-12 05:50:11 +05:30
|
|
|
// SyncTags syncs releases with tags in the database
|
|
|
|
func (g *GiteaLocalUploader) SyncTags() error {
|
2021-11-08 12:34:13 +05:30
|
|
|
return repo_module.SyncReleasesWithTags(g.repo, g.gitRepo)
|
2019-05-07 06:42:51 +05:30
|
|
|
}
|
|
|
|
|
2019-06-29 19:08:22 +05:30
|
|
|
// CreateIssues creates issues
|
|
|
|
func (g *GiteaLocalUploader) CreateIssues(issues ...*base.Issue) error {
|
2022-01-20 23:16:10 +05:30
|
|
|
iss := make([]*models.Issue, 0, len(issues))
|
2019-06-29 19:08:22 +05:30
|
|
|
for _, issue := range issues {
|
|
|
|
var labels []*models.Label
|
|
|
|
for _, label := range issue.Labels {
|
|
|
|
lb, ok := g.labels.Load(label.Name)
|
|
|
|
if ok {
|
|
|
|
labels = append(labels, lb.(*models.Label))
|
|
|
|
}
|
2019-05-07 06:42:51 +05:30
|
|
|
}
|
|
|
|
|
2019-06-29 19:08:22 +05:30
|
|
|
var milestoneID int64
|
|
|
|
if issue.Milestone != "" {
|
|
|
|
milestone, ok := g.milestones.Load(issue.Milestone)
|
|
|
|
if ok {
|
|
|
|
milestoneID = milestone.(int64)
|
|
|
|
}
|
2019-05-07 06:42:51 +05:30
|
|
|
}
|
|
|
|
|
2021-08-18 06:17:18 +05:30
|
|
|
if issue.Created.IsZero() {
|
|
|
|
if issue.Closed != nil {
|
|
|
|
issue.Created = *issue.Closed
|
|
|
|
} else {
|
|
|
|
issue.Created = time.Now()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if issue.Updated.IsZero() {
|
|
|
|
if issue.Closed != nil {
|
|
|
|
issue.Updated = *issue.Closed
|
|
|
|
} else {
|
|
|
|
issue.Updated = time.Now()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-20 23:16:10 +05:30
|
|
|
is := models.Issue{
|
2019-10-14 11:40:42 +05:30
|
|
|
RepoID: g.repo.ID,
|
|
|
|
Repo: g.repo,
|
|
|
|
Index: issue.Number,
|
|
|
|
Title: issue.Title,
|
|
|
|
Content: issue.Content,
|
2020-12-13 17:04:11 +05:30
|
|
|
Ref: issue.Ref,
|
2019-10-14 11:40:42 +05:30
|
|
|
IsClosed: issue.State == "closed",
|
|
|
|
IsLocked: issue.IsLocked,
|
|
|
|
MilestoneID: milestoneID,
|
|
|
|
Labels: labels,
|
|
|
|
CreatedUnix: timeutil.TimeStamp(issue.Created.Unix()),
|
2020-01-14 15:59:22 +05:30
|
|
|
UpdatedUnix: timeutil.TimeStamp(issue.Updated.Unix()),
|
2019-10-14 11:40:42 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
userid, ok := g.userMap[issue.PosterID]
|
|
|
|
tp := g.gitServiceType.Name()
|
|
|
|
if !ok && tp != "" {
|
|
|
|
var err error
|
2021-11-28 19:41:58 +05:30
|
|
|
userid, err = user_model.GetUserIDByExternalUserID(tp, fmt.Sprintf("%v", issue.PosterID))
|
2019-10-14 11:40:42 +05:30
|
|
|
if err != nil {
|
|
|
|
log.Error("GetUserIDByExternalUserID: %v", err)
|
|
|
|
}
|
|
|
|
if userid > 0 {
|
|
|
|
g.userMap[issue.PosterID] = userid
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if userid > 0 {
|
|
|
|
is.PosterID = userid
|
|
|
|
} else {
|
|
|
|
is.PosterID = g.doer.ID
|
|
|
|
is.OriginalAuthor = issue.PosterName
|
|
|
|
is.OriginalAuthorID = issue.PosterID
|
2019-06-29 19:08:22 +05:30
|
|
|
}
|
2019-10-14 11:40:42 +05:30
|
|
|
|
2019-06-29 19:08:22 +05:30
|
|
|
if issue.Closed != nil {
|
2019-08-15 20:16:21 +05:30
|
|
|
is.ClosedUnix = timeutil.TimeStamp(issue.Closed.Unix())
|
2019-06-29 19:08:22 +05:30
|
|
|
}
|
2020-01-15 16:44:07 +05:30
|
|
|
// add reactions
|
|
|
|
for _, reaction := range issue.Reactions {
|
|
|
|
userid, ok := g.userMap[reaction.UserID]
|
|
|
|
if !ok && tp != "" {
|
|
|
|
var err error
|
2021-11-28 19:41:58 +05:30
|
|
|
userid, err = user_model.GetUserIDByExternalUserID(tp, fmt.Sprintf("%v", reaction.UserID))
|
2020-01-15 16:44:07 +05:30
|
|
|
if err != nil {
|
|
|
|
log.Error("GetUserIDByExternalUserID: %v", err)
|
|
|
|
}
|
|
|
|
if userid > 0 {
|
|
|
|
g.userMap[reaction.UserID] = userid
|
|
|
|
}
|
|
|
|
}
|
2022-01-20 23:16:10 +05:30
|
|
|
res := models.Reaction{
|
2020-01-15 16:44:07 +05:30
|
|
|
Type: reaction.Content,
|
|
|
|
CreatedUnix: timeutil.TimeStampNow(),
|
|
|
|
}
|
|
|
|
if userid > 0 {
|
|
|
|
res.UserID = userid
|
|
|
|
} else {
|
|
|
|
res.UserID = g.doer.ID
|
|
|
|
res.OriginalAuthorID = reaction.UserID
|
|
|
|
res.OriginalAuthor = reaction.UserName
|
|
|
|
}
|
|
|
|
is.Reactions = append(is.Reactions, &res)
|
|
|
|
}
|
2019-06-29 19:08:22 +05:30
|
|
|
iss = append(iss, &is)
|
2019-05-07 06:42:51 +05:30
|
|
|
}
|
|
|
|
|
2020-04-17 23:12:57 +05:30
|
|
|
if len(iss) > 0 {
|
|
|
|
if err := models.InsertIssues(iss...); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, is := range iss {
|
2021-08-18 06:17:18 +05:30
|
|
|
g.issues.Store(is.Index, is)
|
2020-04-17 23:12:57 +05:30
|
|
|
}
|
2019-06-29 19:08:22 +05:30
|
|
|
}
|
2020-04-17 23:12:57 +05:30
|
|
|
|
2019-06-29 19:08:22 +05:30
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreateComments creates comments of issues
|
|
|
|
func (g *GiteaLocalUploader) CreateComments(comments ...*base.Comment) error {
|
2022-01-20 23:16:10 +05:30
|
|
|
cms := make([]*models.Comment, 0, len(comments))
|
2019-06-29 19:08:22 +05:30
|
|
|
for _, comment := range comments {
|
2021-08-18 06:17:18 +05:30
|
|
|
var issue *models.Issue
|
|
|
|
issueInter, ok := g.issues.Load(comment.IssueIndex)
|
|
|
|
if !ok {
|
|
|
|
var err error
|
|
|
|
issue, err = models.GetIssueByIndex(g.repo.ID, comment.IssueIndex)
|
2019-06-29 19:08:22 +05:30
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-08-18 06:17:18 +05:30
|
|
|
g.issues.Store(comment.IssueIndex, issue)
|
2019-06-29 19:08:22 +05:30
|
|
|
} else {
|
2021-08-18 06:17:18 +05:30
|
|
|
issue = issueInter.(*models.Issue)
|
2019-06-29 19:08:22 +05:30
|
|
|
}
|
|
|
|
|
2019-10-14 11:40:42 +05:30
|
|
|
userid, ok := g.userMap[comment.PosterID]
|
|
|
|
tp := g.gitServiceType.Name()
|
|
|
|
if !ok && tp != "" {
|
|
|
|
var err error
|
2021-11-28 19:41:58 +05:30
|
|
|
userid, err = user_model.GetUserIDByExternalUserID(tp, fmt.Sprintf("%v", comment.PosterID))
|
2019-10-14 11:40:42 +05:30
|
|
|
if err != nil {
|
|
|
|
log.Error("GetUserIDByExternalUserID: %v", err)
|
|
|
|
}
|
|
|
|
if userid > 0 {
|
|
|
|
g.userMap[comment.PosterID] = userid
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-18 06:17:18 +05:30
|
|
|
if comment.Created.IsZero() {
|
|
|
|
comment.Created = time.Unix(int64(issue.CreatedUnix), 0)
|
|
|
|
}
|
|
|
|
if comment.Updated.IsZero() {
|
|
|
|
comment.Updated = comment.Created
|
|
|
|
}
|
|
|
|
|
2019-10-14 11:40:42 +05:30
|
|
|
cm := models.Comment{
|
2021-08-18 06:17:18 +05:30
|
|
|
IssueID: issue.ID,
|
2019-10-14 11:40:42 +05:30
|
|
|
Type: models.CommentTypeComment,
|
|
|
|
Content: comment.Content,
|
|
|
|
CreatedUnix: timeutil.TimeStamp(comment.Created.Unix()),
|
2020-01-14 15:59:22 +05:30
|
|
|
UpdatedUnix: timeutil.TimeStamp(comment.Updated.Unix()),
|
2019-10-14 11:40:42 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
if userid > 0 {
|
|
|
|
cm.PosterID = userid
|
|
|
|
} else {
|
|
|
|
cm.PosterID = g.doer.ID
|
|
|
|
cm.OriginalAuthor = comment.PosterName
|
|
|
|
cm.OriginalAuthorID = comment.PosterID
|
|
|
|
}
|
|
|
|
|
2020-01-15 16:44:07 +05:30
|
|
|
// add reactions
|
|
|
|
for _, reaction := range comment.Reactions {
|
|
|
|
userid, ok := g.userMap[reaction.UserID]
|
|
|
|
if !ok && tp != "" {
|
|
|
|
var err error
|
2021-11-28 19:41:58 +05:30
|
|
|
userid, err = user_model.GetUserIDByExternalUserID(tp, fmt.Sprintf("%v", reaction.UserID))
|
2020-01-15 16:44:07 +05:30
|
|
|
if err != nil {
|
|
|
|
log.Error("GetUserIDByExternalUserID: %v", err)
|
|
|
|
}
|
|
|
|
if userid > 0 {
|
|
|
|
g.userMap[reaction.UserID] = userid
|
|
|
|
}
|
|
|
|
}
|
2022-01-20 23:16:10 +05:30
|
|
|
res := models.Reaction{
|
2020-01-15 16:44:07 +05:30
|
|
|
Type: reaction.Content,
|
|
|
|
CreatedUnix: timeutil.TimeStampNow(),
|
|
|
|
}
|
|
|
|
if userid > 0 {
|
|
|
|
res.UserID = userid
|
|
|
|
} else {
|
|
|
|
res.UserID = g.doer.ID
|
|
|
|
res.OriginalAuthorID = reaction.UserID
|
|
|
|
res.OriginalAuthor = reaction.UserName
|
|
|
|
}
|
|
|
|
cm.Reactions = append(cm.Reactions, &res)
|
|
|
|
}
|
2019-06-29 19:08:22 +05:30
|
|
|
|
2020-01-15 16:44:07 +05:30
|
|
|
cms = append(cms, &cm)
|
2019-06-29 19:08:22 +05:30
|
|
|
}
|
|
|
|
|
2020-04-17 23:12:57 +05:30
|
|
|
if len(cms) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
2019-06-29 19:08:22 +05:30
|
|
|
return models.InsertIssueComments(cms)
|
2019-05-07 06:42:51 +05:30
|
|
|
}
|
|
|
|
|
2019-06-29 19:08:22 +05:30
|
|
|
// CreatePullRequests creates pull requests
|
|
|
|
func (g *GiteaLocalUploader) CreatePullRequests(prs ...*base.PullRequest) error {
|
2022-01-20 23:16:10 +05:30
|
|
|
gprs := make([]*models.PullRequest, 0, len(prs))
|
2019-06-29 19:08:22 +05:30
|
|
|
for _, pr := range prs {
|
|
|
|
gpr, err := g.newPullRequest(pr)
|
2019-05-07 06:42:51 +05:30
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-10-14 11:40:42 +05:30
|
|
|
|
|
|
|
userid, ok := g.userMap[pr.PosterID]
|
|
|
|
tp := g.gitServiceType.Name()
|
|
|
|
if !ok && tp != "" {
|
|
|
|
var err error
|
2021-11-28 19:41:58 +05:30
|
|
|
userid, err = user_model.GetUserIDByExternalUserID(tp, fmt.Sprintf("%v", pr.PosterID))
|
2019-10-14 11:40:42 +05:30
|
|
|
if err != nil {
|
|
|
|
log.Error("GetUserIDByExternalUserID: %v", err)
|
|
|
|
}
|
|
|
|
if userid > 0 {
|
|
|
|
g.userMap[pr.PosterID] = userid
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if userid > 0 {
|
|
|
|
gpr.Issue.PosterID = userid
|
|
|
|
} else {
|
|
|
|
gpr.Issue.PosterID = g.doer.ID
|
|
|
|
gpr.Issue.OriginalAuthor = pr.PosterName
|
|
|
|
gpr.Issue.OriginalAuthorID = pr.PosterID
|
|
|
|
}
|
|
|
|
|
2019-06-29 19:08:22 +05:30
|
|
|
gprs = append(gprs, gpr)
|
2019-05-07 06:42:51 +05:30
|
|
|
}
|
2019-06-29 19:08:22 +05:30
|
|
|
if err := models.InsertPullRequests(gprs...); err != nil {
|
|
|
|
return err
|
2019-05-07 06:42:51 +05:30
|
|
|
}
|
2019-06-29 19:08:22 +05:30
|
|
|
for _, pr := range gprs {
|
2021-08-18 06:17:18 +05:30
|
|
|
g.issues.Store(pr.Issue.Index, pr.Issue)
|
2020-10-28 03:04:56 +05:30
|
|
|
pull.AddToTaskQueue(pr)
|
2019-06-29 19:08:22 +05:30
|
|
|
}
|
|
|
|
return nil
|
2019-05-07 06:42:51 +05:30
|
|
|
}
|
|
|
|
|
2019-06-29 19:08:22 +05:30
|
|
|
func (g *GiteaLocalUploader) newPullRequest(pr *base.PullRequest) (*models.PullRequest, error) {
|
|
|
|
var labels []*models.Label
|
2019-05-07 06:42:51 +05:30
|
|
|
for _, label := range pr.Labels {
|
2019-06-29 19:08:22 +05:30
|
|
|
lb, ok := g.labels.Load(label.Name)
|
|
|
|
if ok {
|
|
|
|
labels = append(labels, lb.(*models.Label))
|
2019-05-07 06:42:51 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var milestoneID int64
|
|
|
|
if pr.Milestone != "" {
|
|
|
|
milestone, ok := g.milestones.Load(pr.Milestone)
|
2019-06-29 19:08:22 +05:30
|
|
|
if ok {
|
|
|
|
milestoneID = milestone.(int64)
|
2019-05-07 06:42:51 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// download patch file
|
2019-11-05 18:24:47 +05:30
|
|
|
err := func() error {
|
2021-08-22 04:17:45 +05:30
|
|
|
if pr.PatchURL == "" {
|
|
|
|
return nil
|
|
|
|
}
|
2020-12-27 09:04:19 +05:30
|
|
|
// pr.PatchURL maybe a local file
|
|
|
|
ret, err := uri.Open(pr.PatchURL)
|
2019-11-05 18:24:47 +05:30
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-12-27 09:04:19 +05:30
|
|
|
defer ret.Close()
|
2019-11-05 18:24:47 +05:30
|
|
|
pullDir := filepath.Join(g.repo.RepoPath(), "pulls")
|
|
|
|
if err = os.MkdirAll(pullDir, os.ModePerm); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
f, err := os.Create(filepath.Join(pullDir, fmt.Sprintf("%d.patch", pr.Number)))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
2020-12-27 09:04:19 +05:30
|
|
|
_, err = io.Copy(f, ret)
|
2019-11-05 18:24:47 +05:30
|
|
|
return err
|
|
|
|
}()
|
2019-05-07 06:42:51 +05:30
|
|
|
if err != nil {
|
2019-06-29 19:08:22 +05:30
|
|
|
return nil, err
|
2019-05-07 06:42:51 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// set head information
|
|
|
|
pullHead := filepath.Join(g.repo.RepoPath(), "refs", "pull", fmt.Sprintf("%d", pr.Number))
|
|
|
|
if err := os.MkdirAll(pullHead, os.ModePerm); err != nil {
|
2019-06-29 19:08:22 +05:30
|
|
|
return nil, err
|
2019-05-07 06:42:51 +05:30
|
|
|
}
|
|
|
|
p, err := os.Create(filepath.Join(pullHead, "head"))
|
|
|
|
if err != nil {
|
2019-06-29 19:08:22 +05:30
|
|
|
return nil, err
|
2019-05-07 06:42:51 +05:30
|
|
|
}
|
|
|
|
_, err = p.WriteString(pr.Head.SHA)
|
2019-11-05 18:24:47 +05:30
|
|
|
p.Close()
|
2019-05-07 06:42:51 +05:30
|
|
|
if err != nil {
|
2019-06-29 19:08:22 +05:30
|
|
|
return nil, err
|
2019-05-07 06:42:51 +05:30
|
|
|
}
|
|
|
|
|
2022-01-20 23:16:10 +05:30
|
|
|
head := "unknown repository"
|
2019-07-14 14:46:15 +05:30
|
|
|
if pr.IsForkPullRequest() && pr.State != "closed" {
|
2019-05-07 06:42:51 +05:30
|
|
|
if pr.Head.OwnerName != "" {
|
|
|
|
remote := pr.Head.OwnerName
|
|
|
|
_, ok := g.prHeadCache[remote]
|
|
|
|
if !ok {
|
|
|
|
// git remote add
|
|
|
|
err := g.gitRepo.AddRemote(remote, pr.Head.CloneURL, true)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("AddRemote failed: %s", err)
|
|
|
|
} else {
|
|
|
|
g.prHeadCache[remote] = struct{}{}
|
|
|
|
ok = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ok {
|
2022-01-20 04:56:57 +05:30
|
|
|
_, err = git.NewCommandContext(g.ctx, "fetch", remote, pr.Head.Ref).RunInDir(g.repo.RepoPath())
|
2019-05-07 06:42:51 +05:30
|
|
|
if err != nil {
|
|
|
|
log.Error("Fetch branch from %s failed: %v", pr.Head.CloneURL, err)
|
|
|
|
} else {
|
|
|
|
headBranch := filepath.Join(g.repo.RepoPath(), "refs", "heads", pr.Head.OwnerName, pr.Head.Ref)
|
|
|
|
if err := os.MkdirAll(filepath.Dir(headBranch), os.ModePerm); err != nil {
|
2019-06-29 19:08:22 +05:30
|
|
|
return nil, err
|
2019-05-07 06:42:51 +05:30
|
|
|
}
|
|
|
|
b, err := os.Create(headBranch)
|
|
|
|
if err != nil {
|
2019-06-29 19:08:22 +05:30
|
|
|
return nil, err
|
2019-05-07 06:42:51 +05:30
|
|
|
}
|
|
|
|
_, err = b.WriteString(pr.Head.SHA)
|
2019-11-05 18:24:47 +05:30
|
|
|
b.Close()
|
2019-05-07 06:42:51 +05:30
|
|
|
if err != nil {
|
2019-06-29 19:08:22 +05:30
|
|
|
return nil, err
|
2019-05-07 06:42:51 +05:30
|
|
|
}
|
|
|
|
head = pr.Head.OwnerName + "/" + pr.Head.Ref
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
head = pr.Head.Ref
|
2021-10-08 15:29:35 +05:30
|
|
|
// Ensure the closed PR SHA still points to an existing ref
|
2022-01-20 04:56:57 +05:30
|
|
|
_, err = git.NewCommandContext(g.ctx, "rev-list", "--quiet", "-1", pr.Head.SHA).RunInDir(g.repo.RepoPath())
|
2021-10-08 15:29:35 +05:30
|
|
|
if err != nil {
|
|
|
|
if pr.Head.SHA != "" {
|
|
|
|
// Git update-ref remove bad references with a relative path
|
|
|
|
log.Warn("Deprecated local head, removing : %v", pr.Head.SHA)
|
2021-12-23 19:14:00 +05:30
|
|
|
err = g.gitRepo.RemoveReference(pr.GetGitRefName())
|
2021-10-08 15:29:35 +05:30
|
|
|
} else {
|
|
|
|
// The SHA is empty, remove the head file
|
|
|
|
log.Warn("Empty reference, removing : %v", pullHead)
|
|
|
|
err = os.Remove(filepath.Join(pullHead, "head"))
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
log.Error("Cannot remove local head ref, %v", err)
|
|
|
|
}
|
|
|
|
}
|
2019-05-07 06:42:51 +05:30
|
|
|
}
|
|
|
|
|
2021-08-18 06:17:18 +05:30
|
|
|
if pr.Created.IsZero() {
|
|
|
|
if pr.Closed != nil {
|
|
|
|
pr.Created = *pr.Closed
|
|
|
|
} else if pr.MergedTime != nil {
|
|
|
|
pr.Created = *pr.MergedTime
|
|
|
|
} else {
|
|
|
|
pr.Created = time.Now()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if pr.Updated.IsZero() {
|
|
|
|
pr.Updated = pr.Created
|
|
|
|
}
|
|
|
|
|
2022-01-20 23:16:10 +05:30
|
|
|
issue := models.Issue{
|
2019-10-14 11:40:42 +05:30
|
|
|
RepoID: g.repo.ID,
|
|
|
|
Repo: g.repo,
|
|
|
|
Title: pr.Title,
|
|
|
|
Index: pr.Number,
|
|
|
|
Content: pr.Content,
|
|
|
|
MilestoneID: milestoneID,
|
|
|
|
IsPull: true,
|
|
|
|
IsClosed: pr.State == "closed",
|
|
|
|
IsLocked: pr.IsLocked,
|
|
|
|
Labels: labels,
|
|
|
|
CreatedUnix: timeutil.TimeStamp(pr.Created.Unix()),
|
2020-01-14 15:59:22 +05:30
|
|
|
UpdatedUnix: timeutil.TimeStamp(pr.Updated.Unix()),
|
2019-10-14 11:40:42 +05:30
|
|
|
}
|
|
|
|
|
2020-01-15 16:44:07 +05:30
|
|
|
tp := g.gitServiceType.Name()
|
|
|
|
|
2019-10-14 11:40:42 +05:30
|
|
|
userid, ok := g.userMap[pr.PosterID]
|
2020-01-15 16:44:07 +05:30
|
|
|
if !ok && tp != "" {
|
2019-10-14 11:40:42 +05:30
|
|
|
var err error
|
2021-11-28 19:41:58 +05:30
|
|
|
userid, err = user_model.GetUserIDByExternalUserID(tp, fmt.Sprintf("%v", pr.PosterID))
|
2019-10-14 11:40:42 +05:30
|
|
|
if err != nil {
|
|
|
|
log.Error("GetUserIDByExternalUserID: %v", err)
|
|
|
|
}
|
|
|
|
if userid > 0 {
|
|
|
|
g.userMap[pr.PosterID] = userid
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if userid > 0 {
|
|
|
|
issue.PosterID = userid
|
|
|
|
} else {
|
|
|
|
issue.PosterID = g.doer.ID
|
|
|
|
issue.OriginalAuthor = pr.PosterName
|
|
|
|
issue.OriginalAuthorID = pr.PosterID
|
|
|
|
}
|
|
|
|
|
2020-01-15 16:44:07 +05:30
|
|
|
// add reactions
|
|
|
|
for _, reaction := range pr.Reactions {
|
|
|
|
userid, ok := g.userMap[reaction.UserID]
|
|
|
|
if !ok && tp != "" {
|
|
|
|
var err error
|
2021-11-28 19:41:58 +05:30
|
|
|
userid, err = user_model.GetUserIDByExternalUserID(tp, fmt.Sprintf("%v", reaction.UserID))
|
2020-01-15 16:44:07 +05:30
|
|
|
if err != nil {
|
|
|
|
log.Error("GetUserIDByExternalUserID: %v", err)
|
|
|
|
}
|
|
|
|
if userid > 0 {
|
|
|
|
g.userMap[reaction.UserID] = userid
|
|
|
|
}
|
|
|
|
}
|
2022-01-20 23:16:10 +05:30
|
|
|
res := models.Reaction{
|
2020-01-15 16:44:07 +05:30
|
|
|
Type: reaction.Content,
|
|
|
|
CreatedUnix: timeutil.TimeStampNow(),
|
|
|
|
}
|
|
|
|
if userid > 0 {
|
|
|
|
res.UserID = userid
|
|
|
|
} else {
|
|
|
|
res.UserID = g.doer.ID
|
|
|
|
res.OriginalAuthorID = reaction.UserID
|
|
|
|
res.OriginalAuthor = reaction.UserName
|
|
|
|
}
|
|
|
|
issue.Reactions = append(issue.Reactions, &res)
|
|
|
|
}
|
|
|
|
|
2022-01-20 23:16:10 +05:30
|
|
|
pullRequest := models.PullRequest{
|
2019-10-18 16:43:31 +05:30
|
|
|
HeadRepoID: g.repo.ID,
|
|
|
|
HeadBranch: head,
|
|
|
|
BaseRepoID: g.repo.ID,
|
|
|
|
BaseBranch: pr.Base.Ref,
|
|
|
|
MergeBase: pr.Base.SHA,
|
|
|
|
Index: pr.Number,
|
|
|
|
HasMerged: pr.Merged,
|
2019-05-07 06:42:51 +05:30
|
|
|
|
2019-10-14 11:40:42 +05:30
|
|
|
Issue: &issue,
|
2019-05-07 06:42:51 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
if pullRequest.Issue.IsClosed && pr.Closed != nil {
|
2019-08-15 20:16:21 +05:30
|
|
|
pullRequest.Issue.ClosedUnix = timeutil.TimeStamp(pr.Closed.Unix())
|
2019-05-07 06:42:51 +05:30
|
|
|
}
|
|
|
|
if pullRequest.HasMerged && pr.MergedTime != nil {
|
2019-08-15 20:16:21 +05:30
|
|
|
pullRequest.MergedUnix = timeutil.TimeStamp(pr.MergedTime.Unix())
|
2019-05-07 06:42:51 +05:30
|
|
|
pullRequest.MergedCommitID = pr.MergeCommitSHA
|
|
|
|
pullRequest.MergerID = g.doer.ID
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: assignees
|
|
|
|
|
2019-06-29 19:08:22 +05:30
|
|
|
return &pullRequest, nil
|
2019-05-07 06:42:51 +05:30
|
|
|
}
|
|
|
|
|
2020-01-23 22:58:15 +05:30
|
|
|
func convertReviewState(state string) models.ReviewType {
|
|
|
|
switch state {
|
|
|
|
case base.ReviewStatePending:
|
|
|
|
return models.ReviewTypePending
|
|
|
|
case base.ReviewStateApproved:
|
|
|
|
return models.ReviewTypeApprove
|
|
|
|
case base.ReviewStateChangesRequested:
|
|
|
|
return models.ReviewTypeReject
|
|
|
|
case base.ReviewStateCommented:
|
|
|
|
return models.ReviewTypeComment
|
|
|
|
default:
|
|
|
|
return models.ReviewTypePending
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreateReviews create pull request reviews
|
|
|
|
func (g *GiteaLocalUploader) CreateReviews(reviews ...*base.Review) error {
|
2022-01-20 23:16:10 +05:30
|
|
|
cms := make([]*models.Review, 0, len(reviews))
|
2020-01-23 22:58:15 +05:30
|
|
|
for _, review := range reviews {
|
2021-08-18 06:17:18 +05:30
|
|
|
var issue *models.Issue
|
|
|
|
issueInter, ok := g.issues.Load(review.IssueIndex)
|
|
|
|
if !ok {
|
|
|
|
var err error
|
|
|
|
issue, err = models.GetIssueByIndex(g.repo.ID, review.IssueIndex)
|
2020-01-23 22:58:15 +05:30
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-08-18 06:17:18 +05:30
|
|
|
g.issues.Store(review.IssueIndex, issue)
|
2020-01-23 22:58:15 +05:30
|
|
|
} else {
|
2021-08-18 06:17:18 +05:30
|
|
|
issue = issueInter.(*models.Issue)
|
2020-01-23 22:58:15 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
userid, ok := g.userMap[review.ReviewerID]
|
|
|
|
tp := g.gitServiceType.Name()
|
|
|
|
if !ok && tp != "" {
|
|
|
|
var err error
|
2021-11-28 19:41:58 +05:30
|
|
|
userid, err = user_model.GetUserIDByExternalUserID(tp, fmt.Sprintf("%v", review.ReviewerID))
|
2020-01-23 22:58:15 +05:30
|
|
|
if err != nil {
|
|
|
|
log.Error("GetUserIDByExternalUserID: %v", err)
|
|
|
|
}
|
|
|
|
if userid > 0 {
|
|
|
|
g.userMap[review.ReviewerID] = userid
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-18 06:17:18 +05:30
|
|
|
if review.CreatedAt.IsZero() {
|
|
|
|
review.CreatedAt = time.Unix(int64(issue.CreatedUnix), 0)
|
|
|
|
}
|
|
|
|
|
2022-01-20 23:16:10 +05:30
|
|
|
cm := models.Review{
|
2020-01-23 22:58:15 +05:30
|
|
|
Type: convertReviewState(review.State),
|
2021-08-18 06:17:18 +05:30
|
|
|
IssueID: issue.ID,
|
2020-01-23 22:58:15 +05:30
|
|
|
Content: review.Content,
|
|
|
|
Official: review.Official,
|
|
|
|
CreatedUnix: timeutil.TimeStamp(review.CreatedAt.Unix()),
|
|
|
|
UpdatedUnix: timeutil.TimeStamp(review.CreatedAt.Unix()),
|
|
|
|
}
|
|
|
|
|
|
|
|
if userid > 0 {
|
|
|
|
cm.ReviewerID = userid
|
|
|
|
} else {
|
|
|
|
cm.ReviewerID = g.doer.ID
|
|
|
|
cm.OriginalAuthor = review.ReviewerName
|
|
|
|
cm.OriginalAuthorID = review.ReviewerID
|
|
|
|
}
|
|
|
|
|
|
|
|
// get pr
|
2021-08-18 06:17:18 +05:30
|
|
|
pr, ok := g.prCache[issue.ID]
|
2020-01-23 22:58:15 +05:30
|
|
|
if !ok {
|
|
|
|
var err error
|
2021-08-18 06:17:18 +05:30
|
|
|
pr, err = models.GetPullRequestByIssueIDWithNoAttributes(issue.ID)
|
2020-01-23 22:58:15 +05:30
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-08-18 06:17:18 +05:30
|
|
|
g.prCache[issue.ID] = pr
|
2020-01-23 22:58:15 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
for _, comment := range review.Comments {
|
2020-10-14 09:36:00 +05:30
|
|
|
line := comment.Line
|
|
|
|
if line != 0 {
|
|
|
|
comment.Position = 1
|
|
|
|
} else {
|
|
|
|
_, _, line, _ = git.ParseDiffHunkString(comment.DiffHunk)
|
|
|
|
}
|
2020-01-23 22:58:15 +05:30
|
|
|
headCommitID, err := g.gitRepo.GetRefCommitID(pr.GetGitRefName())
|
|
|
|
if err != nil {
|
2021-09-01 17:03:07 +05:30
|
|
|
log.Warn("GetRefCommitID[%s]: %v, the review comment will be ignored", pr.GetGitRefName(), err)
|
|
|
|
continue
|
2020-01-23 22:58:15 +05:30
|
|
|
}
|
2020-01-28 13:32:03 +05:30
|
|
|
|
|
|
|
var patch string
|
2021-02-28 00:16:14 +05:30
|
|
|
reader, writer := io.Pipe()
|
|
|
|
defer func() {
|
|
|
|
_ = reader.Close()
|
|
|
|
_ = writer.Close()
|
|
|
|
}()
|
|
|
|
go func() {
|
|
|
|
if err := git.GetRepoRawDiffForFile(g.gitRepo, pr.MergeBase, headCommitID, git.RawDiffNormal, comment.TreePath, writer); err != nil {
|
|
|
|
// We should ignore the error since the commit maybe removed when force push to the pull request
|
|
|
|
log.Warn("GetRepoRawDiffForFile failed when migrating [%s, %s, %s, %s]: %v", g.gitRepo.Path, pr.MergeBase, headCommitID, comment.TreePath, err)
|
|
|
|
}
|
|
|
|
_ = writer.Close()
|
|
|
|
}()
|
|
|
|
|
|
|
|
patch, _ = git.CutDiffAroundLine(reader, int64((&models.Comment{Line: int64(line + comment.Position - 1)}).UnsignedLine()), line < 0, setting.UI.CodeCommentLines)
|
2020-01-23 22:58:15 +05:30
|
|
|
|
2021-08-18 06:17:18 +05:30
|
|
|
if comment.CreatedAt.IsZero() {
|
|
|
|
comment.CreatedAt = review.CreatedAt
|
|
|
|
}
|
|
|
|
if comment.UpdatedAt.IsZero() {
|
|
|
|
comment.UpdatedAt = comment.CreatedAt
|
|
|
|
}
|
|
|
|
|
2022-01-20 23:16:10 +05:30
|
|
|
c := models.Comment{
|
2020-01-23 22:58:15 +05:30
|
|
|
Type: models.CommentTypeCode,
|
|
|
|
PosterID: comment.PosterID,
|
2021-08-18 06:17:18 +05:30
|
|
|
IssueID: issue.ID,
|
2020-01-23 22:58:15 +05:30
|
|
|
Content: comment.Content,
|
|
|
|
Line: int64(line + comment.Position - 1),
|
|
|
|
TreePath: comment.TreePath,
|
|
|
|
CommitSHA: comment.CommitID,
|
|
|
|
Patch: patch,
|
|
|
|
CreatedUnix: timeutil.TimeStamp(comment.CreatedAt.Unix()),
|
|
|
|
UpdatedUnix: timeutil.TimeStamp(comment.UpdatedAt.Unix()),
|
|
|
|
}
|
|
|
|
|
|
|
|
if userid > 0 {
|
|
|
|
c.PosterID = userid
|
|
|
|
} else {
|
|
|
|
c.PosterID = g.doer.ID
|
|
|
|
c.OriginalAuthor = review.ReviewerName
|
|
|
|
c.OriginalAuthorID = review.ReviewerID
|
|
|
|
}
|
|
|
|
|
|
|
|
cm.Comments = append(cm.Comments, &c)
|
|
|
|
}
|
|
|
|
|
|
|
|
cms = append(cms, &cm)
|
|
|
|
}
|
|
|
|
|
|
|
|
return models.InsertReviews(cms)
|
|
|
|
}
|
|
|
|
|
2019-05-07 06:42:51 +05:30
|
|
|
// Rollback when migrating failed, this will rollback all the changes.
|
|
|
|
func (g *GiteaLocalUploader) Rollback() error {
|
|
|
|
if g.repo != nil && g.repo.ID > 0 {
|
2021-05-15 01:49:38 +05:30
|
|
|
g.gitRepo.Close()
|
2019-05-07 06:42:51 +05:30
|
|
|
if err := models.DeleteRepository(g.doer, g.repo.OwnerID, g.repo.ID); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2020-12-27 09:04:19 +05:30
|
|
|
|
|
|
|
// Finish when migrating success, this will do some status update things.
|
|
|
|
func (g *GiteaLocalUploader) Finish() error {
|
|
|
|
if g.repo == nil || g.repo.ID <= 0 {
|
|
|
|
return ErrRepoNotCreated
|
|
|
|
}
|
|
|
|
|
2021-08-13 18:36:18 +05:30
|
|
|
// update issue_index
|
|
|
|
if err := models.RecalculateIssueIndexForRepo(g.repo.ID); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-01-18 00:01:58 +05:30
|
|
|
if err := models.UpdateRepoStats(db.DefaultContext, g.repo.ID); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-12-10 06:57:50 +05:30
|
|
|
g.repo.Status = repo_model.RepositoryReady
|
2021-12-12 21:18:20 +05:30
|
|
|
return repo_model.UpdateRepositoryCols(g.repo, "status")
|
2020-12-27 09:04:19 +05:30
|
|
|
}
|