66package migrations
77
88import (
9+ "bytes"
910 "context"
1011 "fmt"
12+ "io"
13+ "io/ioutil"
1114 "net/http"
1215 "net/url"
1316 "strings"
@@ -37,16 +40,6 @@ func init() {
3740type GithubDownloaderV3Factory struct {
3841}
3942
40- // Match returns ture if the migration remote URL matched this downloader factory
41- func (f * GithubDownloaderV3Factory ) Match (opts base.MigrateOptions ) (bool , error ) {
42- u , err := url .Parse (opts .CloneAddr )
43- if err != nil {
44- return false , err
45- }
46-
47- return strings .EqualFold (u .Host , "github.com" ) && opts .AuthUsername != "" , nil
48- }
49-
5043// New returns a Downloader related to this factory according MigrateOptions
5144func (f * GithubDownloaderV3Factory ) New (opts base.MigrateOptions ) (base.Downloader , error ) {
5245 u , err := url .Parse (opts .CloneAddr )
@@ -60,7 +53,7 @@ func (f *GithubDownloaderV3Factory) New(opts base.MigrateOptions) (base.Download
6053
6154 log .Trace ("Create github downloader: %s/%s" , oldOwner , oldName )
6255
63- return NewGithubDownloaderV3 (opts .AuthUsername , opts .AuthPassword , oldOwner , oldName ), nil
56+ return NewGithubDownloaderV3 (opts .AuthUsername , opts .AuthPassword , opts . AuthToken , oldOwner , oldName ), nil
6457}
6558
6659// GitServiceType returns the type of git service
@@ -81,7 +74,7 @@ type GithubDownloaderV3 struct {
8174}
8275
8376// NewGithubDownloaderV3 creates a github Downloader via github v3 API
84- func NewGithubDownloaderV3 (userName , password , repoOwner , repoName string ) * GithubDownloaderV3 {
77+ func NewGithubDownloaderV3 (userName , password , token , repoOwner , repoName string ) * GithubDownloaderV3 {
8578 var downloader = GithubDownloaderV3 {
8679 userName : userName ,
8780 password : password ,
@@ -90,23 +83,19 @@ func NewGithubDownloaderV3(userName, password, repoOwner, repoName string) *Gith
9083 repoName : repoName ,
9184 }
9285
93- var client * http.Client
94- if userName != "" {
95- if password == "" {
96- ts := oauth2 .StaticTokenSource (
97- & oauth2.Token {AccessToken : userName },
98- )
99- client = oauth2 .NewClient (downloader .ctx , ts )
100- } else {
101- client = & http.Client {
102- Transport : & http.Transport {
103- Proxy : func (req * http.Request ) (* url.URL , error ) {
104- req .SetBasicAuth (userName , password )
105- return nil , nil
106- },
107- },
108- }
109- }
86+ client := & http.Client {
87+ Transport : & http.Transport {
88+ Proxy : func (req * http.Request ) (* url.URL , error ) {
89+ req .SetBasicAuth (userName , password )
90+ return nil , nil
91+ },
92+ },
93+ }
94+ if token != "" {
95+ ts := oauth2 .StaticTokenSource (
96+ & oauth2.Token {AccessToken : token },
97+ )
98+ client = oauth2 .NewClient (downloader .ctx , ts )
11099 }
111100 downloader .client = github .NewClient (client )
112101 return & downloader
@@ -290,10 +279,8 @@ func (g *GithubDownloaderV3) convertGithubRelease(rel *github.RepositoryRelease)
290279 }
291280
292281 for _ , asset := range rel .Assets {
293- u , _ := url .Parse (* asset .BrowserDownloadURL )
294- u .User = url .UserPassword (g .userName , g .password )
295282 r .Assets = append (r .Assets , base.ReleaseAsset {
296- URL : u . String () ,
283+ ID : * asset . ID ,
297284 Name : * asset .Name ,
298285 ContentType : asset .ContentType ,
299286 Size : asset .Size ,
@@ -331,6 +318,18 @@ func (g *GithubDownloaderV3) GetReleases() ([]*base.Release, error) {
331318 return releases , nil
332319}
333320
321+ // GetAsset returns an asset
322+ func (g * GithubDownloaderV3 ) GetAsset (_ string , id int64 ) (io.ReadCloser , error ) {
323+ asset , redir , err := g .client .Repositories .DownloadReleaseAsset (g .ctx , g .repoOwner , g .repoName , id , http .DefaultClient )
324+ if err != nil {
325+ return nil , err
326+ }
327+ if asset == nil {
328+ return ioutil .NopCloser (bytes .NewBufferString (redir )), nil
329+ }
330+ return asset , nil
331+ }
332+
334333// GetIssues returns issues according start and limit
335334func (g * GithubDownloaderV3 ) GetIssues (page , perPage int ) ([]* base.Issue , bool , error ) {
336335 opt := & github.IssueListByRepoOptions {
0 commit comments