diff options
author | John Olheiser <john.olheiser@gmail.com> | 2020-08-27 20:36:37 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-08-28 09:36:37 +0800 |
commit | 211321fb936683815c4033fdfb9ac46af8a3b357 (patch) | |
tree | 13f356084062e1827bda3ba0d1c9f4b4b5c2c799 /modules/migrations/github.go | |
parent | ed2f6e137be8fe3c85292e8344b5bb5cfb56891d (diff) | |
download | gitea-211321fb936683815c4033fdfb9ac46af8a3b357.tar.gz gitea-211321fb936683815c4033fdfb9ac46af8a3b357.zip |
Git migration UX (#12619)
* Initial work
Signed-off-by: jolheiser <john.olheiser@gmail.com>
* Implementation
Signed-off-by: jolheiser <john.olheiser@gmail.com>
* Fix gitlab and token cloning
Signed-off-by: jolheiser <john.olheiser@gmail.com>
* Imports and JS
Signed-off-by: jolheiser <john.olheiser@gmail.com>
* Fix test
Signed-off-by: jolheiser <john.olheiser@gmail.com>
* Linting
Signed-off-by: jolheiser <john.olheiser@gmail.com>
* Generate swagger
Signed-off-by: jolheiser <john.olheiser@gmail.com>
* Move mirror toggle and rename options
Signed-off-by: jolheiser <john.olheiser@gmail.com>
Co-authored-by: Lauris BH <lauris@nix.lv>
Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
Diffstat (limited to 'modules/migrations/github.go')
-rw-r--r-- | modules/migrations/github.go | 63 |
1 files changed, 31 insertions, 32 deletions
diff --git a/modules/migrations/github.go b/modules/migrations/github.go index 97d62b994f..eb73a7e0d4 100644 --- a/modules/migrations/github.go +++ b/modules/migrations/github.go @@ -6,8 +6,11 @@ package migrations import ( + "bytes" "context" "fmt" + "io" + "io/ioutil" "net/http" "net/url" "strings" @@ -37,16 +40,6 @@ func init() { type GithubDownloaderV3Factory struct { } -// Match returns ture if the migration remote URL matched this downloader factory -func (f *GithubDownloaderV3Factory) Match(opts base.MigrateOptions) (bool, error) { - u, err := url.Parse(opts.CloneAddr) - if err != nil { - return false, err - } - - return strings.EqualFold(u.Host, "github.com") && opts.AuthUsername != "", nil -} - // New returns a Downloader related to this factory according MigrateOptions func (f *GithubDownloaderV3Factory) New(opts base.MigrateOptions) (base.Downloader, error) { u, err := url.Parse(opts.CloneAddr) @@ -60,7 +53,7 @@ func (f *GithubDownloaderV3Factory) New(opts base.MigrateOptions) (base.Download log.Trace("Create github downloader: %s/%s", oldOwner, oldName) - return NewGithubDownloaderV3(opts.AuthUsername, opts.AuthPassword, oldOwner, oldName), nil + return NewGithubDownloaderV3(opts.AuthUsername, opts.AuthPassword, opts.AuthToken, oldOwner, oldName), nil } // GitServiceType returns the type of git service @@ -81,7 +74,7 @@ type GithubDownloaderV3 struct { } // NewGithubDownloaderV3 creates a github Downloader via github v3 API -func NewGithubDownloaderV3(userName, password, repoOwner, repoName string) *GithubDownloaderV3 { +func NewGithubDownloaderV3(userName, password, token, repoOwner, repoName string) *GithubDownloaderV3 { var downloader = GithubDownloaderV3{ userName: userName, password: password, @@ -90,23 +83,19 @@ func NewGithubDownloaderV3(userName, password, repoOwner, repoName string) *Gith repoName: repoName, } - var client *http.Client - if userName != "" { - if password == "" { - ts := oauth2.StaticTokenSource( - &oauth2.Token{AccessToken: userName}, - ) - client = oauth2.NewClient(downloader.ctx, ts) - } else { - client = &http.Client{ - Transport: &http.Transport{ - Proxy: func(req *http.Request) (*url.URL, error) { - req.SetBasicAuth(userName, password) - return nil, nil - }, - }, - } - } + client := &http.Client{ + Transport: &http.Transport{ + Proxy: func(req *http.Request) (*url.URL, error) { + req.SetBasicAuth(userName, password) + return nil, nil + }, + }, + } + if token != "" { + ts := oauth2.StaticTokenSource( + &oauth2.Token{AccessToken: token}, + ) + client = oauth2.NewClient(downloader.ctx, ts) } downloader.client = github.NewClient(client) return &downloader @@ -290,10 +279,8 @@ func (g *GithubDownloaderV3) convertGithubRelease(rel *github.RepositoryRelease) } for _, asset := range rel.Assets { - u, _ := url.Parse(*asset.BrowserDownloadURL) - u.User = url.UserPassword(g.userName, g.password) r.Assets = append(r.Assets, base.ReleaseAsset{ - URL: u.String(), + ID: *asset.ID, Name: *asset.Name, ContentType: asset.ContentType, Size: asset.Size, @@ -331,6 +318,18 @@ func (g *GithubDownloaderV3) GetReleases() ([]*base.Release, error) { return releases, nil } +// GetAsset returns an asset +func (g *GithubDownloaderV3) GetAsset(_ string, id int64) (io.ReadCloser, error) { + asset, redir, err := g.client.Repositories.DownloadReleaseAsset(g.ctx, g.repoOwner, g.repoName, id, http.DefaultClient) + if err != nil { + return nil, err + } + if asset == nil { + return ioutil.NopCloser(bytes.NewBufferString(redir)), nil + } + return asset, nil +} + // GetIssues returns issues according start and limit func (g *GithubDownloaderV3) GetIssues(page, perPage int) ([]*base.Issue, bool, error) { opt := &github.IssueListByRepoOptions{ |