Files
gitea/services/migrations/http_client.go
T
silverwindandbircni fc49368845 feat: Improve Gitea-compatible forge migrations, fix many migration bugs (#39424)
Supports migrating from Gitea-compatible forges like Forgejo and fixes
many migration bugs.

1. Pre-release and `+gitea-` suffixed server versions no longer drop
reviews, reactions and pagination.
2. Review states, dismissals, scoped and archived labels and merge bases
are migrated.
3. Disabled units, external trackers, unavailable release assets, and
GitLab confidential issues and internal notes are skipped.
4. Credentials are only sent to the source host, and `Retry-After` rate
limits are waited out.
5. GitHub migrations of only issues or pull requests, basic auth clones
and dismissed reviews work again.
6. The API returns 404 for releases of a disabled unit and shows
migrated authors as mannequins, replacing the never populated
`original_author` and `original_author_id` fields.

Fixes: https://github.com/go-gitea/gitea/issues/19533
Fixes: https://github.com/go-gitea/gitea/issues/35885
Fixes: https://github.com/go-gitea/gitea/issues/18275
Fixes: https://github.com/go-gitea/gitea/issues/35069
Fixes: https://github.com/go-gitea/gitea/issues/17763
Fixes: https://github.com/go-gitea/gitea/issues/39548
Co-authored-by: bircni <bircni@icloud.com>
2026-10-24 01:15:31 +00:00

115 lines
3.4 KiB
Go

// Copyright 2021 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package migrations
import (
"crypto/tls"
"encoding/base64"
"net/http"
"net/url"
"strconv"
"sync"
"time"
"gitea.dev/modules/egress"
"gitea.dev/modules/log"
"gitea.dev/modules/setting"
)
// getMigrationHTTPClient returns the shared migration client, so downloads from one host reuse its connections
var getMigrationHTTPClient = sync.OnceValue(func() *http.Client {
return &http.Client{Transport: retryAfterTransport{getMigrationTransport()}}
})
var getMigrationTransport = sync.OnceValue(newMigrationTransport)
func newMigrationHTTPClient(baseURL, authorization string) *http.Client {
return &http.Client{Transport: authTransport(getMigrationHTTPClient().Transport, baseURL, authorization)}
}
// NewMigrationHTTPTransport returns a HTTP transport for migration, enforcing the migration policy on its direct dials.
func NewMigrationHTTPTransport() http.RoundTripper {
return retryAfterTransport{newMigrationTransport()}
}
func newMigrationTransport() *http.Transport {
t := egress.NewMigrationPolicy().NewHTTPTransport()
t.TLSClientConfig = &tls.Config{InsecureSkipVerify: setting.Migrations.SkipTLSVerify}
return t
}
type retryAfterTransport struct {
http.RoundTripper
}
func (t retryAfterTransport) RoundTrip(req *http.Request) (*http.Response, error) {
var waited time.Duration
for retries := 0; ; retries++ {
resp, err := t.RoundTripper.RoundTrip(req)
if err != nil {
return nil, err
}
if (resp.StatusCode != http.StatusTooManyRequests && resp.StatusCode != http.StatusForbidden) || retries == 5 || (req.Body != nil && req.GetBody == nil) { // GitHub's secondary rate limits answer 403
return resp, nil
}
delay, ok := parseRetryAfter(resp.Header.Get("Retry-After"))
if !ok || delay > time.Hour-waited {
return resp, nil
}
waited += delay
resp.Body.Close()
log.Info("Rate limited by %s, retrying in %s", req.URL.Host, delay)
select {
case <-req.Context().Done():
return nil, req.Context().Err()
case <-time.After(delay):
}
if req.GetBody != nil {
body, err := req.GetBody()
if err != nil {
return nil, err
}
req = req.Clone(req.Context())
req.Body = body
}
}
}
func parseRetryAfter(value string) (time.Duration, bool) {
if seconds, err := strconv.ParseUint(value, 10, 32); err == nil { // 32 bits can't overflow the Duration
return time.Duration(seconds) * time.Second, true
}
if retryAt, err := http.ParseTime(value); err == nil {
return max(time.Until(retryAt), 0), true
}
return 0, false
}
type roundTripperFunc func(req *http.Request) (*http.Response, error)
func (rt roundTripperFunc) RoundTrip(req *http.Request) (*http.Response, error) {
return rt(req)
}
func authTransport(transport http.RoundTripper, baseURL, authorization string) http.RoundTripper {
base, err := url.Parse(baseURL)
if authorization == "" || err != nil {
return transport
}
return roundTripperFunc(func(req *http.Request) (*http.Response, error) {
if req.URL.Host == base.Host && (req.URL.Scheme == "https" || base.Scheme == "http") { // the source host only, never downgraded to http
req = req.Clone(req.Context())
req.Header.Set("Authorization", authorization)
}
return transport.RoundTrip(req)
})
}
func basicAuthorization(username, password string) string {
if username == "" {
return ""
}
return "Basic " + base64.StdEncoding.EncodeToString([]byte(username+":"+password))
}