From 55508b36fbd36f5aebc045c89689e7a4b8712909 Mon Sep 17 00:00:00 2001 From: silverwind Date: Mon, 25 May 2026 12:20:47 +0200 Subject: [PATCH] fix(migrations): adapt to go-github v87 client constructor NewClient now uses the functional-options pattern and returns an error, so thread that error through the downloader constructors and factories. Co-Authored-By: Claude (Opus 4.7) --- services/migrations/gitbucket.go | 11 ++++++---- services/migrations/gitea_uploader_test.go | 12 ++++++----- services/migrations/github.go | 25 +++++++++++++++------- services/migrations/github_test.go | 5 +++-- 4 files changed, 34 insertions(+), 19 deletions(-) diff --git a/services/migrations/gitbucket.go b/services/migrations/gitbucket.go index 4fe9e30a39c..71358736e8e 100644 --- a/services/migrations/gitbucket.go +++ b/services/migrations/gitbucket.go @@ -43,7 +43,7 @@ func (f *GitBucketDownloaderFactory) New(ctx context.Context, opts base.MigrateO oldName := strings.TrimSuffix(fields[len(fields)-1], ".git") log.Trace("Create GitBucket downloader. BaseURL: %s RepoOwner: %s RepoName: %s", baseURL, oldOwner, oldName) - return NewGitBucketDownloader(ctx, baseURL, opts.AuthUsername, opts.AuthPassword, opts.AuthToken, oldOwner, oldName), nil + return NewGitBucketDownloader(ctx, baseURL, opts.AuthUsername, opts.AuthPassword, opts.AuthToken, oldOwner, oldName) } // GitServiceType returns the type of git service @@ -70,8 +70,11 @@ func (g *GitBucketDownloader) LogString() string { } // NewGitBucketDownloader creates a GitBucket downloader -func NewGitBucketDownloader(ctx context.Context, baseURL, userName, password, token, repoOwner, repoName string) *GitBucketDownloader { - githubDownloader := NewGithubDownloaderV3(ctx, baseURL, userName, password, token, repoOwner, repoName) +func NewGitBucketDownloader(ctx context.Context, baseURL, userName, password, token, repoOwner, repoName string) (*GitBucketDownloader, error) { + githubDownloader, err := NewGithubDownloaderV3(ctx, baseURL, userName, password, token, repoOwner, repoName) + if err != nil { + return nil, err + } // Gitbucket 4.40 uses different internal hard-coded perPage values. // Issues, PRs, and other major parts use 25. Release page uses 10. // Some API doesn't support paging yet. Sounds difficult, but using @@ -81,7 +84,7 @@ func NewGitBucketDownloader(ctx context.Context, baseURL, userName, password, to githubDownloader.SkipReviews = true return &GitBucketDownloader{ githubDownloader, - } + }, nil } // SupportGetRepoComments return true if it supports get repo comments diff --git a/services/migrations/gitea_uploader_test.go b/services/migrations/gitea_uploader_test.go index 0b9f0eb9964..636551e125a 100644 --- a/services/migrations/gitea_uploader_test.go +++ b/services/migrations/gitea_uploader_test.go @@ -21,6 +21,7 @@ import ( repo_service "code.gitea.io/gitea/services/repository" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestGiteaUploadRepo(t *testing.T) { @@ -31,14 +32,15 @@ func TestGiteaUploadRepo(t *testing.T) { user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1}) + ctx := t.Context() + downloader, err := NewGithubDownloaderV3(ctx, "https://github.com", "", "", "", "go-xorm", "builder") + require.NoError(t, err) var ( - ctx = t.Context() - downloader = NewGithubDownloaderV3(ctx, "https://github.com", "", "", "", "go-xorm", "builder") - repoName = "builder-" + time.Now().Format("2006-01-02-15-04-05") - uploader = NewGiteaLocalUploader(graceful.GetManager().HammerContext(), user, user.Name, repoName) + repoName = "builder-" + time.Now().Format("2006-01-02-15-04-05") + uploader = NewGiteaLocalUploader(graceful.GetManager().HammerContext(), user, user.Name, repoName) ) - err := migrateRepository(t.Context(), user, downloader, uploader, base.MigrateOptions{ + err = migrateRepository(t.Context(), user, downloader, uploader, base.MigrateOptions{ CloneAddr: "https://github.com/go-xorm/builder", RepoName: repoName, AuthUsername: "", diff --git a/services/migrations/github.go b/services/migrations/github.go index dad20f97792..4ca95428de8 100644 --- a/services/migrations/github.go +++ b/services/migrations/github.go @@ -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,13 +134,18 @@ func (g *GithubDownloaderV3) LogString() string { return fmt.Sprintf("", 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) { diff --git a/services/migrations/github_test.go b/services/migrations/github_test.go index ca6090b789e..3d2a42c59c7 100644 --- a/services/migrations/github_test.go +++ b/services/migrations/github_test.go @@ -30,8 +30,9 @@ func TestGitHubDownloadRepo(t *testing.T) { GithubLimitRateRemaining = 3 // Wait at 3 remaining since we could have 3 CI in // ctx := t.Context() - downloader := NewGithubDownloaderV3(ctx, mockServer.URL, "", "", token, "go-gitea", "test_repo") - err := downloader.RefreshRate(ctx) + downloader, err := NewGithubDownloaderV3(ctx, mockServer.URL, "", "", token, "go-gitea", "test_repo") + require.NoError(t, err) + err = downloader.RefreshRate(ctx) require.NoError(t, err) repo, err := downloader.GetRepoInfo(ctx)