6
6
package migrations
7
7
8
8
import (
9
+ "bytes"
9
10
"context"
10
11
"fmt"
12
+ "io"
13
+ "io/ioutil"
11
14
"net/http"
12
15
"net/url"
13
16
"strings"
@@ -37,16 +40,6 @@ func init() {
37
40
type GithubDownloaderV3Factory struct {
38
41
}
39
42
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
-
50
43
// New returns a Downloader related to this factory according MigrateOptions
51
44
func (f * GithubDownloaderV3Factory ) New (opts base.MigrateOptions ) (base.Downloader , error ) {
52
45
u , err := url .Parse (opts .CloneAddr )
@@ -60,7 +53,7 @@ func (f *GithubDownloaderV3Factory) New(opts base.MigrateOptions) (base.Download
60
53
61
54
log .Trace ("Create github downloader: %s/%s" , oldOwner , oldName )
62
55
63
- return NewGithubDownloaderV3 (opts .AuthUsername , opts .AuthPassword , oldOwner , oldName ), nil
56
+ return NewGithubDownloaderV3 (opts .AuthUsername , opts .AuthPassword , opts . AuthToken , oldOwner , oldName ), nil
64
57
}
65
58
66
59
// GitServiceType returns the type of git service
@@ -81,7 +74,7 @@ type GithubDownloaderV3 struct {
81
74
}
82
75
83
76
// 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 {
85
78
var downloader = GithubDownloaderV3 {
86
79
userName : userName ,
87
80
password : password ,
@@ -90,23 +83,19 @@ func NewGithubDownloaderV3(userName, password, repoOwner, repoName string) *Gith
90
83
repoName : repoName ,
91
84
}
92
85
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 )
110
99
}
111
100
downloader .client = github .NewClient (client )
112
101
return & downloader
@@ -290,10 +279,8 @@ func (g *GithubDownloaderV3) convertGithubRelease(rel *github.RepositoryRelease)
290
279
}
291
280
292
281
for _ , asset := range rel .Assets {
293
- u , _ := url .Parse (* asset .BrowserDownloadURL )
294
- u .User = url .UserPassword (g .userName , g .password )
295
282
r .Assets = append (r .Assets , base.ReleaseAsset {
296
- URL : u . String () ,
283
+ ID : * asset . ID ,
297
284
Name : * asset .Name ,
298
285
ContentType : asset .ContentType ,
299
286
Size : asset .Size ,
@@ -331,6 +318,18 @@ func (g *GithubDownloaderV3) GetReleases() ([]*base.Release, error) {
331
318
return releases , nil
332
319
}
333
320
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
+
334
333
// GetIssues returns issues according start and limit
335
334
func (g * GithubDownloaderV3 ) GetIssues (page , perPage int ) ([]* base.Issue , bool , error ) {
336
335
opt := & github.IssueListByRepoOptions {
0 commit comments