This commit is contained in:
@@ -14,13 +14,13 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"code.gitea.io/gitea/modules/git"
|
||||
"code.gitea.io/gitea/modules/log"
|
||||
base "code.gitea.io/gitea/modules/migration"
|
||||
"code.gitea.io/gitea/modules/proxy"
|
||||
"code.gitea.io/gitea/modules/structs"
|
||||
"gitea.dev/modules/git"
|
||||
"gitea.dev/modules/log"
|
||||
base "gitea.dev/modules/migration"
|
||||
"gitea.dev/modules/proxy"
|
||||
"gitea.dev/modules/structs"
|
||||
|
||||
"github.com/google/go-github/v84/github"
|
||||
"github.com/google/go-github/v88/github"
|
||||
"golang.org/x/oauth2"
|
||||
)
|
||||
|
||||
@@ -52,7 +52,7 @@ func (f *GithubDownloaderV3Factory) New(ctx context.Context, opts base.MigrateOp
|
||||
|
||||
log.Trace("Create github downloader BaseURL: %s %s/%s", baseURL, oldOwner, oldName)
|
||||
|
||||
return NewGithubDownloaderV3(ctx, baseURL, opts.AuthUsername, opts.AuthPassword, opts.AuthToken, oldOwner, oldName), nil
|
||||
return NewGithubDownloaderV3(ctx, baseURL, opts.AuthUsername, opts.AuthPassword, opts.AuthToken, oldOwner, oldName)
|
||||
}
|
||||
|
||||
// GitServiceType returns the type of git service
|
||||
@@ -78,7 +78,7 @@ type GithubDownloaderV3 struct {
|
||||
}
|
||||
|
||||
// NewGithubDownloaderV3 creates a github Downloader via github v3 API
|
||||
func NewGithubDownloaderV3(_ context.Context, baseURL, userName, password, token, repoOwner, repoName string) *GithubDownloaderV3 {
|
||||
func NewGithubDownloaderV3(_ context.Context, baseURL, userName, password, token, repoOwner, repoName string) (*GithubDownloaderV3, error) {
|
||||
downloader := GithubDownloaderV3{
|
||||
userName: userName,
|
||||
baseURL: baseURL,
|
||||
@@ -102,7 +102,9 @@ func NewGithubDownloaderV3(_ context.Context, baseURL, userName, password, token
|
||||
},
|
||||
}
|
||||
|
||||
downloader.addClient(client, baseURL)
|
||||
if err := downloader.addClient(client, baseURL); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
} else {
|
||||
transport := NewMigrationHTTPTransport()
|
||||
@@ -113,9 +115,11 @@ func NewGithubDownloaderV3(_ context.Context, baseURL, userName, password, token
|
||||
client := &http.Client{
|
||||
Transport: transport,
|
||||
}
|
||||
downloader.addClient(client, baseURL)
|
||||
if err := downloader.addClient(client, baseURL); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
return &downloader
|
||||
return &downloader, nil
|
||||
}
|
||||
|
||||
// String implements Stringer
|
||||
@@ -130,25 +134,34 @@ func (g *GithubDownloaderV3) LogString() string {
|
||||
return fmt.Sprintf("<GithubDownloaderV3 %s %s/%s>", g.baseURL, g.repoOwner, g.repoName)
|
||||
}
|
||||
|
||||
func (g *GithubDownloaderV3) addClient(client *http.Client, baseURL string) {
|
||||
githubClient := github.NewClient(client)
|
||||
func (g *GithubDownloaderV3) addClient(client *http.Client, baseURL string) error {
|
||||
opts := []github.ClientOptionsFunc{github.WithHTTPClient(client)}
|
||||
if baseURL != "https://github.com" {
|
||||
githubClient, _ = githubClient.WithEnterpriseURLs(baseURL, baseURL)
|
||||
opts = append(opts, github.WithEnterpriseURLs(baseURL, baseURL))
|
||||
}
|
||||
githubClient, err := github.NewClient(opts...)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
g.clients = append(g.clients, githubClient)
|
||||
g.rates = append(g.rates, nil)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (g *GithubDownloaderV3) waitAndPickClient(ctx context.Context) {
|
||||
var recentIdx int
|
||||
var maxRemaining int
|
||||
for i := 0; i < len(g.clients); i++ {
|
||||
if g.rates[i] != nil && g.rates[i].Remaining > maxRemaining {
|
||||
if g.rates[i] == nil { // probe unknown clients once, else their rate never gets learned
|
||||
g.curClientIdx = i
|
||||
return
|
||||
}
|
||||
if g.rates[i].Remaining > maxRemaining {
|
||||
maxRemaining = g.rates[i].Remaining
|
||||
recentIdx = i
|
||||
}
|
||||
}
|
||||
g.curClientIdx = recentIdx // if no max remain, it will always pick the first client.
|
||||
g.curClientIdx = recentIdx
|
||||
|
||||
for g.rates[g.curClientIdx] != nil && g.rates[g.curClientIdx].Remaining <= GithubLimitRateRemaining {
|
||||
timer := time.NewTimer(time.Until(g.rates[g.curClientIdx].Reset.Time))
|
||||
@@ -205,6 +218,7 @@ func (g *GithubDownloaderV3) GetRepoInfo(ctx context.Context) (*base.Repository,
|
||||
Name: gr.GetName(),
|
||||
IsPrivate: gr.GetPrivate(),
|
||||
Description: gr.GetDescription(),
|
||||
Website: gr.GetHomepage(),
|
||||
OriginalURL: gr.GetHTMLURL(),
|
||||
CloneURL: gr.GetCloneURL(),
|
||||
DefaultBranch: gr.GetDefaultBranch(),
|
||||
@@ -319,7 +333,7 @@ func (g *GithubDownloaderV3) convertGithubRelease(ctx context.Context, rel *gith
|
||||
r.Published = rel.PublishedAt.Time
|
||||
}
|
||||
|
||||
httpClient := NewMigrationHTTPClient()
|
||||
httpClient := newMigrationHTTPClient()
|
||||
|
||||
for _, asset := range rel.Assets {
|
||||
assetID := asset.GetID() // Don't optimize this, for closure we need a local variable TODO: no need to do so in new Golang
|
||||
|
||||
Reference in New Issue
Block a user