From f058899b9eb20caef2b675084cbd4065ce6ed876 Mon Sep 17 00:00:00 2001 From: KN4CK3R Date: Tue, 17 Aug 2021 20:06:59 +0000 Subject: [PATCH 1/5] Added migration from GitBucket. --- modules/convert/utils.go | 2 + modules/migrations/gitbucket.go | 67 +++++++++ modules/migrations/github.go | 197 ++++++++++++++------------ modules/structs/repo.go | 4 + options/locale/locale_en-US.ini | 1 + public/img/svg/gitea-gitbucket.svg | 1 + templates/repo/migrate/gitbucket.tmpl | 129 +++++++++++++++++ web_src/svg/gitea-gitbucket.svg | 39 +++++ 8 files changed, 349 insertions(+), 91 deletions(-) create mode 100644 modules/migrations/gitbucket.go create mode 100644 public/img/svg/gitea-gitbucket.svg create mode 100644 templates/repo/migrate/gitbucket.tmpl create mode 100644 web_src/svg/gitea-gitbucket.svg diff --git a/modules/convert/utils.go b/modules/convert/utils.go index a0463d7b1006..52fbcf547f72 100644 --- a/modules/convert/utils.go +++ b/modules/convert/utils.go @@ -35,6 +35,8 @@ func ToGitServiceType(value string) structs.GitServiceType { return structs.GogsService case "onedev": return structs.OneDevService + case "gitbucket": + return structs.GitBucketService default: return structs.PlainGitService } diff --git a/modules/migrations/gitbucket.go b/modules/migrations/gitbucket.go new file mode 100644 index 000000000000..138e3e62341f --- /dev/null +++ b/modules/migrations/gitbucket.go @@ -0,0 +1,67 @@ +// Copyright 2021 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" + "net/url" + "strings" + + "code.gitea.io/gitea/modules/migrations/base" + "code.gitea.io/gitea/modules/structs" +) + +var ( + _ base.Downloader = &GitBucketDownloader{} + _ base.DownloaderFactory = &GitBucketDownloaderFactory{} +) + +func init() { + RegisterDownloaderFactory(&GitBucketDownloaderFactory{}) +} + +// GitBucketDownloaderFactory defines a GitBucket downloader factory +type GitBucketDownloaderFactory struct { +} + +// New returns a Downloader related to this factory according MigrateOptions +func (f *GitBucketDownloaderFactory) 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 + fields := strings.Split(u.Path, "/") + oldOwner := fields[1] + oldName := strings.TrimSuffix(fields[2], ".git") + + return NewGitBucketDownloader(ctx, baseURL, opts.AuthUsername, opts.AuthPassword, opts.AuthToken, oldOwner, oldName), nil +} + +// GitServiceType returns the type of git service +func (f *GitBucketDownloaderFactory) GitServiceType() structs.GitServiceType { + return structs.GitBucketService +} + +// GitBucketDownloader implements a Downloader interface to get repository information +// from GitBucket via GithubDownloader +type GitBucketDownloader struct { + *GithubDownloaderV3 +} + +// NewGitBucketDownloader creates a GitBucket downloader +func NewGitBucketDownloader(ctx context.Context, baseURL, userName, password, token, repoOwner, repoName string) *GitBucketDownloader { + githubDownloader := NewGithubDownloaderV3(ctx, baseURL, userName, password, token, repoOwner, repoName) + githubDownloader.SkipReactions = true + return &GitBucketDownloader{ + githubDownloader, + } +} + +// SupportGetRepoComments return true if it supports get repo comments +func (g *GitBucketDownloader) SupportGetRepoComments() bool { + return false +} diff --git a/modules/migrations/github.go b/modules/migrations/github.go index 54af10d116aa..7393f9f570f4 100644 --- a/modules/migrations/github.go +++ b/modules/migrations/github.go @@ -76,6 +76,8 @@ type GithubDownloaderV3 struct { password string rate *github.Rate maxPerPage int + + SkipReactions bool } // NewGithubDownloaderV3 creates a github Downloader via github v3 API @@ -99,7 +101,10 @@ func NewGithubDownloaderV3(ctx context.Context, baseURL, userName, password, tok } if token != "" { ts := oauth2.StaticTokenSource( - &oauth2.Token{AccessToken: token}, + &oauth2.Token{ + TokenType: "token", + AccessToken: token, + }, ) client = oauth2.NewClient(downloader.ctx, ts) } @@ -385,25 +390,27 @@ func (g *GithubDownloaderV3) GetIssues(page, perPage int) ([]*base.Issue, bool, // get reactions var reactions []*base.Reaction - for i := 1; ; i++ { - g.sleep() - res, resp, err := g.client.Reactions.ListIssueReactions(g.ctx, g.repoOwner, g.repoName, issue.GetNumber(), &github.ListOptions{ - Page: i, - PerPage: perPage, - }) - if err != nil { - return nil, false, err - } - g.rate = &resp.Rate - if len(res) == 0 { - break - } - for _, reaction := range res { - reactions = append(reactions, &base.Reaction{ - UserID: reaction.User.GetID(), - UserName: reaction.User.GetLogin(), - Content: reaction.GetContent(), + if !g.SkipReactions { + for i := 1; ; i++ { + g.sleep() + res, resp, err := g.client.Reactions.ListIssueReactions(g.ctx, g.repoOwner, g.repoName, issue.GetNumber(), &github.ListOptions{ + Page: i, + PerPage: perPage, }) + if err != nil { + return nil, false, err + } + g.rate = &resp.Rate + if len(res) == 0 { + break + } + for _, reaction := range res { + reactions = append(reactions, &base.Reaction{ + UserID: reaction.User.GetID(), + UserName: reaction.User.GetLogin(), + Content: reaction.GetContent(), + }) + } } } @@ -473,25 +480,27 @@ func (g *GithubDownloaderV3) getComments(issueContext base.IssueContext) ([]*bas for _, comment := range comments { // get reactions var reactions []*base.Reaction - for i := 1; ; i++ { - g.sleep() - res, resp, err := g.client.Reactions.ListIssueCommentReactions(g.ctx, g.repoOwner, g.repoName, comment.GetID(), &github.ListOptions{ - Page: i, - PerPage: g.maxPerPage, - }) - if err != nil { - return nil, err - } - g.rate = &resp.Rate - if len(res) == 0 { - break - } - for _, reaction := range res { - reactions = append(reactions, &base.Reaction{ - UserID: reaction.User.GetID(), - UserName: reaction.User.GetLogin(), - Content: reaction.GetContent(), + if !g.SkipReactions { + for i := 1; ; i++ { + g.sleep() + res, resp, err := g.client.Reactions.ListIssueCommentReactions(g.ctx, g.repoOwner, g.repoName, comment.GetID(), &github.ListOptions{ + Page: i, + PerPage: g.maxPerPage, }) + if err != nil { + return nil, err + } + g.rate = &resp.Rate + if len(res) == 0 { + break + } + for _, reaction := range res { + reactions = append(reactions, &base.Reaction{ + UserID: reaction.User.GetID(), + UserName: reaction.User.GetLogin(), + Content: reaction.GetContent(), + }) + } } } @@ -540,25 +549,27 @@ func (g *GithubDownloaderV3) GetAllComments(page, perPage int) ([]*base.Comment, for _, comment := range comments { // get reactions var reactions []*base.Reaction - for i := 1; ; i++ { - g.sleep() - res, resp, err := g.client.Reactions.ListIssueCommentReactions(g.ctx, g.repoOwner, g.repoName, comment.GetID(), &github.ListOptions{ - Page: i, - PerPage: g.maxPerPage, - }) - if err != nil { - return nil, false, err - } - g.rate = &resp.Rate - if len(res) == 0 { - break - } - for _, reaction := range res { - reactions = append(reactions, &base.Reaction{ - UserID: reaction.User.GetID(), - UserName: reaction.User.GetLogin(), - Content: reaction.GetContent(), + if !g.SkipReactions { + for i := 1; ; i++ { + g.sleep() + res, resp, err := g.client.Reactions.ListIssueCommentReactions(g.ctx, g.repoOwner, g.repoName, comment.GetID(), &github.ListOptions{ + Page: i, + PerPage: g.maxPerPage, }) + if err != nil { + return nil, false, err + } + g.rate = &resp.Rate + if len(res) == 0 { + break + } + for _, reaction := range res { + reactions = append(reactions, &base.Reaction{ + UserID: reaction.User.GetID(), + UserName: reaction.User.GetLogin(), + Content: reaction.GetContent(), + }) + } } } idx := strings.LastIndex(*comment.IssueURL, "/") @@ -608,25 +619,27 @@ func (g *GithubDownloaderV3) GetPullRequests(page, perPage int) ([]*base.PullReq // get reactions var reactions []*base.Reaction - for i := 1; ; i++ { - g.sleep() - res, resp, err := g.client.Reactions.ListIssueReactions(g.ctx, g.repoOwner, g.repoName, pr.GetNumber(), &github.ListOptions{ - Page: i, - PerPage: perPage, - }) - if err != nil { - return nil, false, err - } - g.rate = &resp.Rate - if len(res) == 0 { - break - } - for _, reaction := range res { - reactions = append(reactions, &base.Reaction{ - UserID: reaction.User.GetID(), - UserName: reaction.User.GetLogin(), - Content: reaction.GetContent(), + if !g.SkipReactions { + for i := 1; ; i++ { + g.sleep() + res, resp, err := g.client.Reactions.ListIssueReactions(g.ctx, g.repoOwner, g.repoName, pr.GetNumber(), &github.ListOptions{ + Page: i, + PerPage: perPage, }) + if err != nil { + return nil, false, err + } + g.rate = &resp.Rate + if len(res) == 0 { + break + } + for _, reaction := range res { + reactions = append(reactions, &base.Reaction{ + UserID: reaction.User.GetID(), + UserName: reaction.User.GetLogin(), + Content: reaction.GetContent(), + }) + } } } @@ -686,25 +699,27 @@ func (g *GithubDownloaderV3) convertGithubReviewComments(cs []*github.PullReques for _, c := range cs { // get reactions var reactions []*base.Reaction - for i := 1; ; i++ { - g.sleep() - res, resp, err := g.client.Reactions.ListPullRequestCommentReactions(g.ctx, g.repoOwner, g.repoName, c.GetID(), &github.ListOptions{ - Page: i, - PerPage: g.maxPerPage, - }) - if err != nil { - return nil, err - } - g.rate = &resp.Rate - if len(res) == 0 { - break - } - for _, reaction := range res { - reactions = append(reactions, &base.Reaction{ - UserID: reaction.User.GetID(), - UserName: reaction.User.GetLogin(), - Content: reaction.GetContent(), + if !g.SkipReactions { + for i := 1; ; i++ { + g.sleep() + res, resp, err := g.client.Reactions.ListPullRequestCommentReactions(g.ctx, g.repoOwner, g.repoName, c.GetID(), &github.ListOptions{ + Page: i, + PerPage: g.maxPerPage, }) + if err != nil { + return nil, err + } + g.rate = &resp.Rate + if len(res) == 0 { + break + } + for _, reaction := range res { + reactions = append(reactions, &base.Reaction{ + UserID: reaction.User.GetID(), + UserName: reaction.User.GetLogin(), + Content: reaction.GetContent(), + }) + } } } diff --git a/modules/structs/repo.go b/modules/structs/repo.go index 313a982f43d6..8c951ec4a52c 100644 --- a/modules/structs/repo.go +++ b/modules/structs/repo.go @@ -249,6 +249,7 @@ const ( GitlabService // 4 gitlab service GogsService // 5 gogs service OneDevService // 6 onedev service + GitBucketService // 7 gitbucket service ) // Name represents the service type's name @@ -270,6 +271,8 @@ func (gt GitServiceType) Title() string { return "Gogs" case OneDevService: return "OneDev" + case GitBucketService: + return "GitBucket" case PlainGitService: return "Git" } @@ -326,5 +329,6 @@ var ( GiteaService, GogsService, OneDevService, + GitBucketService, } ) diff --git a/options/locale/locale_en-US.ini b/options/locale/locale_en-US.ini index 3eb38257768a..44ad00334c6e 100644 --- a/options/locale/locale_en-US.ini +++ b/options/locale/locale_en-US.ini @@ -905,6 +905,7 @@ migrate.gitlab.description = Migrating data from GitLab.com or Self-Hosted gitla migrate.gitea.description = Migrating data from Gitea.com or Self-Hosted Gitea server. migrate.gogs.description = Migrating data from notabug.org or other Self-Hosted Gogs server. migrate.onedev.description = Migrating data from code.onedev.io or Self-Hosted OneDev server. +migrate.gitbucket.description = Migrating data from Self-Hosted GitBucket server. migrate.migrating_git = Migrating Git Data migrate.migrating_topics = Migrating Topics migrate.migrating_milestones = Migrating Milestones diff --git a/public/img/svg/gitea-gitbucket.svg b/public/img/svg/gitea-gitbucket.svg new file mode 100644 index 000000000000..50ddd44e1b7c --- /dev/null +++ b/public/img/svg/gitea-gitbucket.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/templates/repo/migrate/gitbucket.tmpl b/templates/repo/migrate/gitbucket.tmpl new file mode 100644 index 000000000000..b554a3a75606 --- /dev/null +++ b/templates/repo/migrate/gitbucket.tmpl @@ -0,0 +1,129 @@ +{{template "base/head" .}} +
+
+
+
+ {{.CsrfTokenHtml}} +

+ {{.i18n.Tr "repo.migrate.migrate" .service.Title}} + +

+
+ {{template "base/alert" .}} +
+ + + + {{.i18n.Tr "repo.migrate.clone_address_desc"}}{{if .ContextUser.CanImportLocal}} {{.i18n.Tr "repo.migrate.clone_local_path"}}{{end}} + +
+ +
+ + +
+ +
+ + +
+ + {{template "repo/migrate/options" .}} + + {{.i18n.Tr "repo.migrate.migrate_items_options"}} +
+
+ +
+ + +
+
+ + +
+
+
+ +
+ + +
+
+ + +
+
+
+ +
+ + +
+
+ + +
+
+
+ +
+ +
+ + +
+ +
+ + +
+
+ +
+ {{if .IsForcedPrivate}} + + + {{else}} + + + {{end}} +
+
+
+ + +
+ +
+ + + {{.i18n.Tr "cancel"}} +
+
+
+
+
+
+{{template "base/footer" .}} diff --git a/web_src/svg/gitea-gitbucket.svg b/web_src/svg/gitea-gitbucket.svg new file mode 100644 index 000000000000..24da15496843 --- /dev/null +++ b/web_src/svg/gitea-gitbucket.svg @@ -0,0 +1,39 @@ + + + + +Created by potrace 1.14, written by Peter Selinger 2001-2017 + + + + + + + + + + \ No newline at end of file From aa1a786e987445962738341c944175e07f97650d Mon Sep 17 00:00:00 2001 From: KN4CK3R Date: Sat, 21 Aug 2021 23:58:33 +0000 Subject: [PATCH 2/5] Fix lint. --- modules/structs/repo.go | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/modules/structs/repo.go b/modules/structs/repo.go index 8c951ec4a52c..8482a2128d7f 100644 --- a/modules/structs/repo.go +++ b/modules/structs/repo.go @@ -242,14 +242,14 @@ type GitServiceType int // enumerate all GitServiceType const ( - NotMigrated GitServiceType = iota // 0 not migrated from external sites - PlainGitService // 1 plain git service - GithubService // 2 github.com - GiteaService // 3 gitea service - GitlabService // 4 gitlab service - GogsService // 5 gogs service - OneDevService // 6 onedev service - GitBucketService // 7 gitbucket service + NotMigrated GitServiceType = iota // 0 not migrated from external sites + PlainGitService // 1 plain git service + GithubService // 2 github.com + GiteaService // 3 gitea service + GitlabService // 4 gitlab service + GogsService // 5 gogs service + OneDevService // 6 onedev service + GitBucketService // 7 gitbucket service ) // Name represents the service type's name From 0d01303f5db875bdbcbd64b45000151bdf69af56 Mon Sep 17 00:00:00 2001 From: KN4CK3R Date: Sun, 29 Aug 2021 19:51:47 +0000 Subject: [PATCH 3/5] GetReview is not supported. --- modules/migrations/gitbucket.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/modules/migrations/gitbucket.go b/modules/migrations/gitbucket.go index 138e3e62341f..72090c24900c 100644 --- a/modules/migrations/gitbucket.go +++ b/modules/migrations/gitbucket.go @@ -65,3 +65,8 @@ func NewGitBucketDownloader(ctx context.Context, baseURL, userName, password, to func (g *GitBucketDownloader) SupportGetRepoComments() bool { return false } + +// GetReviews is not supported +func (g *GitBucketDownloader) GetReviews(context base.IssueContext) ([]*base.Review, error) { + return nil, &base.ErrNotSupported{Entity: "Reviews"} +} From 20fdff6bbc262c725dcd94463bc3db58e5849aba Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Mon, 18 Oct 2021 16:51:41 +0200 Subject: [PATCH 4/5] adopt refactors & new things --- modules/migrations/github.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/modules/migrations/github.go b/modules/migrations/github.go index c38615257459..bd455e05df19 100644 --- a/modules/migrations/github.go +++ b/modules/migrations/github.go @@ -452,7 +452,7 @@ func (g *GithubDownloaderV3) GetIssues(page, perPage int) ([]*base.Issue, bool, if err != nil { return nil, false, err } - g.rate = &resp.Rate + g.setRate(&resp.Rate) if len(res) == 0 { break } @@ -556,7 +556,7 @@ func (g *GithubDownloaderV3) getComments(issueContext base.IssueContext) ([]*bas if err != nil { return nil, err } - g.rate = &resp.Rate + g.setRate(&resp.Rate) if len(res) == 0 { break } @@ -644,7 +644,7 @@ func (g *GithubDownloaderV3) GetAllComments(page, perPage int) ([]*base.Comment, if err != nil { return nil, false, err } - g.rate = &resp.Rate + g.setRate(&resp.Rate) if len(res) == 0 { break } @@ -728,7 +728,7 @@ func (g *GithubDownloaderV3) GetPullRequests(page, perPage int) ([]*base.PullReq if err != nil { return nil, false, err } - g.rate = &resp.Rate + g.setRate(&resp.Rate) if len(res) == 0 { break } @@ -825,7 +825,7 @@ func (g *GithubDownloaderV3) convertGithubReviewComments(cs []*github.PullReques if err != nil { return nil, err } - g.rate = &resp.Rate + g.setRate(&resp.Rate) if len(res) == 0 { break } From 2980bec1e905fc117e2db16833b9b808613e688a Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Mon, 18 Oct 2021 17:00:58 +0200 Subject: [PATCH 5/5] fix --- modules/migrations/github.go | 70 ------------------------------------ 1 file changed, 70 deletions(-) diff --git a/modules/migrations/github.go b/modules/migrations/github.go index bd455e05df19..86a95c27ee44 100644 --- a/modules/migrations/github.go +++ b/modules/migrations/github.go @@ -449,20 +449,6 @@ func (g *GithubDownloaderV3) GetIssues(page, perPage int) ([]*base.Issue, bool, UserName: reaction.User.GetLogin(), Content: reaction.GetContent(), }) - if err != nil { - return nil, false, err - } - g.setRate(&resp.Rate) - if len(res) == 0 { - break - } - for _, reaction := range res { - reactions = append(reactions, &base.Reaction{ - UserID: reaction.User.GetID(), - UserName: reaction.User.GetLogin(), - Content: reaction.GetContent(), - }) - } } } } @@ -553,20 +539,6 @@ func (g *GithubDownloaderV3) getComments(issueContext base.IssueContext) ([]*bas UserName: reaction.User.GetLogin(), Content: reaction.GetContent(), }) - if err != nil { - return nil, err - } - g.setRate(&resp.Rate) - if len(res) == 0 { - break - } - for _, reaction := range res { - reactions = append(reactions, &base.Reaction{ - UserID: reaction.User.GetID(), - UserName: reaction.User.GetLogin(), - Content: reaction.GetContent(), - }) - } } } } @@ -641,20 +613,6 @@ func (g *GithubDownloaderV3) GetAllComments(page, perPage int) ([]*base.Comment, UserName: reaction.User.GetLogin(), Content: reaction.GetContent(), }) - if err != nil { - return nil, false, err - } - g.setRate(&resp.Rate) - if len(res) == 0 { - break - } - for _, reaction := range res { - reactions = append(reactions, &base.Reaction{ - UserID: reaction.User.GetID(), - UserName: reaction.User.GetLogin(), - Content: reaction.GetContent(), - }) - } } } } @@ -725,20 +683,6 @@ func (g *GithubDownloaderV3) GetPullRequests(page, perPage int) ([]*base.PullReq UserName: reaction.User.GetLogin(), Content: reaction.GetContent(), }) - if err != nil { - return nil, false, err - } - g.setRate(&resp.Rate) - if len(res) == 0 { - break - } - for _, reaction := range res { - reactions = append(reactions, &base.Reaction{ - UserID: reaction.User.GetID(), - UserName: reaction.User.GetLogin(), - Content: reaction.GetContent(), - }) - } } } } @@ -822,20 +766,6 @@ func (g *GithubDownloaderV3) convertGithubReviewComments(cs []*github.PullReques UserName: reaction.User.GetLogin(), Content: reaction.GetContent(), }) - if err != nil { - return nil, err - } - g.setRate(&resp.Rate) - if len(res) == 0 { - break - } - for _, reaction := range res { - reactions = append(reactions, &base.Reaction{ - UserID: reaction.User.GetID(), - UserName: reaction.User.GetLogin(), - Content: reaction.GetContent(), - }) - } } } }