Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support to migrate from gogs #14342

Merged
merged 54 commits into from
Jan 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
54 commits
Select commit Hold shift + click to select a range
5f3196f
add support to migrate from gogs
lunny Jun 11, 2019
21533a4
remove test since it depends on try.gogs.io
lunny Jul 8, 2019
78d75c5
handle unsupported error
lunny Jul 8, 2019
7417afd
fix vendor
lunny Jul 9, 2019
efcf78d
fix go.sum
lunny Nov 3, 2019
9fe0b4a
ignore gogs test since it depends on try.gogs.io
lunny Nov 3, 2019
64b371a
Merge branch 'master' into lunny/downloader_gogs
6543 Jan 14, 2021
93a97ad
adapt: 'migrate service type switch page'
6543 Jan 14, 2021
e34e6c2
refactor & fix & update
6543 Jan 14, 2021
2da5710
fix
6543 Jan 14, 2021
2b890ff
optimize svg
6543 Jan 14, 2021
dc5e3ba
refactor IsErrNotSupported
6543 Jan 14, 2021
f509a1d
gogs dont support oauth2
6543 Jan 14, 2021
0dda8a9
Update modules/migrations/migrate.go
6543 Jan 14, 2021
8845c23
Merge branch 'master' into lunny/downloader_gogs
6543 Jan 14, 2021
9225b0b
fix lint
6543 Jan 14, 2021
f8635eb
refactor migrations downloader interface implementation(s)
6543 Jan 15, 2021
7518582
fix lint
6543 Jan 15, 2021
bb6512e
Merge branch 'master' into lunny/downloader_gogs
6543 Jan 15, 2021
bce51d1
handle ErrNotSupported on all downloader functions ...
6543 Jan 15, 2021
cf30f4f
more specific error messages
6543 Jan 15, 2021
07d3691
RetryDownloader: dont retry if not supported
6543 Jan 15, 2021
41be205
Merge branch 'master' into lunny/downloader_gogs
6543 Jan 16, 2021
5d7dc8d
Merge branch 'master' into lunny/downloader_gogs
6543 Jan 16, 2021
f8159e7
Merge branch 'master' into lunny/downloader_gogs
6543 Jan 16, 2021
4bbb5e2
Merge branch 'master' into lunny/downloader_gogs
6543 Jan 16, 2021
98fa152
fix generate CloneURL
6543 Jan 16, 2021
53f40f9
gogs: migrate closed issues too
6543 Jan 16, 2021
67f9344
optimize
6543 Jan 16, 2021
9a3e37d
sdk bug detected :/
6543 Jan 16, 2021
d70c62e
resolve TODO
6543 Jan 16, 2021
15f3307
fix out of slice issue
6543 Jan 17, 2021
aa6b837
Merge branch 'master' into lunny/downloader_gogs
6543 Jan 17, 2021
4099e17
fix pagination
6543 Jan 17, 2021
b6559cf
Merge branch 'master' into lunny/downloader_gogs
6543 Jan 17, 2021
08b9e41
Resolve GetTopics() TODO
6543 Jan 17, 2021
c177145
Merge branch 'master' into lunny/downloader_gogs
6543 Jan 17, 2021
72bd6de
impruve more
6543 Jan 17, 2021
df0e55a
Merge branch 'master' into lunny/downloader_gogs
6543 Jan 18, 2021
7ecc980
Merge branch 'master' into lunny/downloader_gogs
6543 Jan 18, 2021
f033f5e
Merge branch 'master' into lunny/downloader_gogs
6543 Jan 18, 2021
16dc498
nits
6543 Jan 19, 2021
6cd3cde
Merge branch 'master' into lunny/downloader_gogs
6543 Jan 19, 2021
802b532
fix get Comments
6543 Jan 19, 2021
c024e5d
Merge branch 'master' into lunny/downloader_gogs
6543 Jan 19, 2021
b6761e6
Merge branch 'master' into lunny/downloader_gogs
6543 Jan 19, 2021
270cabf
Merge branch 'master' into lunny/downloader_gogs
6543 Jan 19, 2021
1ecb88c
Merge branch 'master' into lunny/downloader_gogs
6543 Jan 20, 2021
5a5134d
Merge branch 'master' into lunny/downloader_gogs
6543 Jan 20, 2021
9b7ff34
Merge branch 'master' into lunny/downloader_gogs
6543 Jan 21, 2021
f1cbea2
Merge branch 'master' into lunny/downloader_gogs
6543 Jan 21, 2021
e6e7752
Merge branch 'master' into lunny/downloader_gogs
6543 Jan 21, 2021
e9109dd
Retrofit context cancellation
zeripath Jan 21, 2021
19e2ed8
Update modules/migrations/gogs.go
zeripath Jan 21, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ require (
github.com/gobwas/glob v0.2.3
github.com/gogs/chardet v0.0.0-20191104214054-4b6791f73a28
github.com/gogs/cron v0.0.0-20171120032916-9f6c956d3e14
github.com/gogs/go-gogs-client v0.0.0-20200905025246-8bb8a50cb355
github.com/google/go-github/v32 v32.1.0
github.com/google/uuid v1.1.2
github.com/gorilla/context v1.1.1
Expand Down Expand Up @@ -128,3 +129,5 @@ require (
replace github.com/hashicorp/go-version => github.com/6543/go-version v1.2.4

replace github.com/microcosm-cc/bluemonday => github.com/lunny/bluemonday v1.0.5-0.20201227154428-ca34796141e8

replace github.com/gogs/go-gogs-client => github.com/6543-forks/go-gogs-client v0.0.0-20210116182316-f2f8bc0ea9cc
59 changes: 4 additions & 55 deletions go.sum

Large diffs are not rendered by default.

212 changes: 1 addition & 211 deletions modules/migrations/base/downloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ package base

import (
"context"
"time"

"code.gitea.io/gitea/modules/structs"
)
Expand All @@ -24,220 +23,11 @@ type Downloader interface {
GetComments(issueNumber int64) ([]*Comment, error)
GetPullRequests(page, perPage int) ([]*PullRequest, bool, error)
GetReviews(pullRequestNumber int64) ([]*Review, error)
FormatCloneURL(opts MigrateOptions, remoteAddr string) (string, error)
}

// DownloaderFactory defines an interface to match a downloader implementation and create a downloader
type DownloaderFactory interface {
New(ctx context.Context, opts MigrateOptions) (Downloader, error)
GitServiceType() structs.GitServiceType
}

var (
_ Downloader = &RetryDownloader{}
)

// RetryDownloader retry the downloads
type RetryDownloader struct {
Downloader
ctx context.Context
RetryTimes int // the total execute times
RetryDelay int // time to delay seconds
}

// NewRetryDownloader creates a retry downloader
func NewRetryDownloader(ctx context.Context, downloader Downloader, retryTimes, retryDelay int) *RetryDownloader {
return &RetryDownloader{
Downloader: downloader,
ctx: ctx,
RetryTimes: retryTimes,
RetryDelay: retryDelay,
}
}

// SetContext set context
func (d *RetryDownloader) SetContext(ctx context.Context) {
d.ctx = ctx
d.Downloader.SetContext(ctx)
}

// GetRepoInfo returns a repository information with retry
func (d *RetryDownloader) GetRepoInfo() (*Repository, error) {
var (
times = d.RetryTimes
repo *Repository
err error
)
for ; times > 0; times-- {
if repo, err = d.Downloader.GetRepoInfo(); err == nil {
return repo, nil
}
select {
case <-d.ctx.Done():
return nil, d.ctx.Err()
case <-time.After(time.Second * time.Duration(d.RetryDelay)):
}
}
return nil, err
}

// GetTopics returns a repository's topics with retry
func (d *RetryDownloader) GetTopics() ([]string, error) {
var (
times = d.RetryTimes
topics []string
err error
)
for ; times > 0; times-- {
if topics, err = d.Downloader.GetTopics(); err == nil {
return topics, nil
}
select {
case <-d.ctx.Done():
return nil, d.ctx.Err()
case <-time.After(time.Second * time.Duration(d.RetryDelay)):
}
}
return nil, err
}

// GetMilestones returns a repository's milestones with retry
func (d *RetryDownloader) GetMilestones() ([]*Milestone, error) {
var (
times = d.RetryTimes
milestones []*Milestone
err error
)
for ; times > 0; times-- {
if milestones, err = d.Downloader.GetMilestones(); err == nil {
return milestones, nil
}
select {
case <-d.ctx.Done():
return nil, d.ctx.Err()
case <-time.After(time.Second * time.Duration(d.RetryDelay)):
}
}
return nil, err
}

// GetReleases returns a repository's releases with retry
func (d *RetryDownloader) GetReleases() ([]*Release, error) {
var (
times = d.RetryTimes
releases []*Release
err error
)
for ; times > 0; times-- {
if releases, err = d.Downloader.GetReleases(); err == nil {
return releases, nil
}
select {
case <-d.ctx.Done():
return nil, d.ctx.Err()
case <-time.After(time.Second * time.Duration(d.RetryDelay)):
}
}
return nil, err
}

// GetLabels returns a repository's labels with retry
func (d *RetryDownloader) GetLabels() ([]*Label, error) {
var (
times = d.RetryTimes
labels []*Label
err error
)
for ; times > 0; times-- {
if labels, err = d.Downloader.GetLabels(); err == nil {
return labels, nil
}
select {
case <-d.ctx.Done():
return nil, d.ctx.Err()
case <-time.After(time.Second * time.Duration(d.RetryDelay)):
}
}
return nil, err
}

// GetIssues returns a repository's issues with retry
func (d *RetryDownloader) GetIssues(page, perPage int) ([]*Issue, bool, error) {
var (
times = d.RetryTimes
issues []*Issue
isEnd bool
err error
)
for ; times > 0; times-- {
if issues, isEnd, err = d.Downloader.GetIssues(page, perPage); err == nil {
return issues, isEnd, nil
}
select {
case <-d.ctx.Done():
return nil, false, d.ctx.Err()
case <-time.After(time.Second * time.Duration(d.RetryDelay)):
}
}
return nil, false, err
}

// GetComments returns a repository's comments with retry
func (d *RetryDownloader) GetComments(issueNumber int64) ([]*Comment, error) {
var (
times = d.RetryTimes
comments []*Comment
err error
)
for ; times > 0; times-- {
if comments, err = d.Downloader.GetComments(issueNumber); err == nil {
return comments, nil
}
select {
case <-d.ctx.Done():
return nil, d.ctx.Err()
case <-time.After(time.Second * time.Duration(d.RetryDelay)):
}
}
return nil, err
}

// GetPullRequests returns a repository's pull requests with retry
func (d *RetryDownloader) GetPullRequests(page, perPage int) ([]*PullRequest, bool, error) {
var (
times = d.RetryTimes
prs []*PullRequest
err error
isEnd bool
)
for ; times > 0; times-- {
if prs, isEnd, err = d.Downloader.GetPullRequests(page, perPage); err == nil {
return prs, isEnd, nil
}
select {
case <-d.ctx.Done():
return nil, false, d.ctx.Err()
case <-time.After(time.Second * time.Duration(d.RetryDelay)):
}
}
return nil, false, err
}

// GetReviews returns pull requests reviews
func (d *RetryDownloader) GetReviews(pullRequestNumber int64) ([]*Review, error) {
var (
times = d.RetryTimes
reviews []*Review
err error
)
for ; times > 0; times-- {
if reviews, err = d.Downloader.GetReviews(pullRequestNumber); err == nil {
return reviews, nil
}
select {
case <-d.ctx.Done():
return nil, d.ctx.Err()
case <-time.After(time.Second * time.Duration(d.RetryDelay)):
}
}
return nil, err
}
26 changes: 26 additions & 0 deletions modules/migrations/base/error.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// 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 base

import "fmt"

// ErrNotSupported represents status if a downloader do not supported something.
type ErrNotSupported struct {
Entity string
}

// IsErrNotSupported checks if an error is an ErrNotSupported
func IsErrNotSupported(err error) bool {
_, ok := err.(ErrNotSupported)
Copy link
Contributor

@wxiaoguang wxiaoguang Jun 30, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems incorrect here, because the NullDownloader code returns pointer: return nil, &ErrNotSupported{}

Or here is correct but NullDownloader does wrong 😂

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return ok
}

// Error return error message
func (err ErrNotSupported) Error() string {
if len(err.Entity) != 0 {
return fmt.Sprintf("'%s' not supported", err.Entity)
}
return "not supported"
}
2 changes: 1 addition & 1 deletion modules/migrations/base/milestone.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ type Milestone struct {
Created time.Time
Updated *time.Time
Closed *time.Time
State string
State string // open, closed
}
82 changes: 82 additions & 0 deletions modules/migrations/base/null_downloader.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
// 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 base

import (
"context"
"net/url"
)

// NullDownloader implements a blank downloader
type NullDownloader struct {
}

var (
_ Downloader = &NullDownloader{}
)

// SetContext set context
func (n NullDownloader) SetContext(_ context.Context) {}

// GetRepoInfo returns a repository information
func (n NullDownloader) GetRepoInfo() (*Repository, error) {
return nil, &ErrNotSupported{Entity: "RepoInfo"}
}

// GetTopics return repository topics
func (n NullDownloader) GetTopics() ([]string, error) {
return nil, &ErrNotSupported{Entity: "Topics"}
}

// GetMilestones returns milestones
func (n NullDownloader) GetMilestones() ([]*Milestone, error) {
return nil, &ErrNotSupported{Entity: "Milestones"}
}

// GetReleases returns releases
func (n NullDownloader) GetReleases() ([]*Release, error) {
return nil, &ErrNotSupported{Entity: "Releases"}
}

// GetLabels returns labels
func (n NullDownloader) GetLabels() ([]*Label, error) {
return nil, &ErrNotSupported{Entity: "Labels"}
}

// GetIssues returns issues according start and limit
func (n NullDownloader) GetIssues(page, perPage int) ([]*Issue, bool, error) {
return nil, false, &ErrNotSupported{Entity: "Issues"}
}

// GetComments returns comments according issueNumber
func (n NullDownloader) GetComments(issueNumber int64) ([]*Comment, error) {
return nil, &ErrNotSupported{Entity: "Comments"}
}

// GetPullRequests returns pull requests according page and perPage
func (n NullDownloader) GetPullRequests(page, perPage int) ([]*PullRequest, bool, error) {
return nil, false, &ErrNotSupported{Entity: "PullRequests"}
}

// GetReviews returns pull requests review
func (n NullDownloader) GetReviews(pullRequestNumber int64) ([]*Review, error) {
return nil, &ErrNotSupported{Entity: "Reviews"}
}

// FormatCloneURL add authentification into remote URLs
func (n NullDownloader) FormatCloneURL(opts MigrateOptions, remoteAddr string) (string, error) {
if len(opts.AuthToken) > 0 || len(opts.AuthUsername) > 0 {
u, err := url.Parse(remoteAddr)
if err != nil {
return "", err
}
u.User = url.UserPassword(opts.AuthUsername, opts.AuthPassword)
if len(opts.AuthToken) > 0 {
u.User = url.UserPassword("oauth2", opts.AuthToken)
}
return u.String(), nil
}
return remoteAddr, nil
}
Loading