Skip to content

Commit f10c360

Browse files
authored
Merge pull request #1 from wxiaoguang/temp-gogs-context
Make gogs client work with context
2 parents 89d4a09 + 54fbd07 commit f10c360

File tree

3 files changed

+30
-26
lines changed

3 files changed

+30
-26
lines changed

go.mod

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -318,8 +318,6 @@ replace github.com/shurcooL/vfsgen => github.com/lunny/vfsgen v0.0.0-20220105142
318318

319319
replace github.com/nektos/act => gitea.com/gitea/act v0.261.3
320320

321-
replace github.com/gogs/go-gogs-client => github.com/TheFox0x7/go-gogs-client v0.0.0-20250205203315-0e70fbf458bf
322-
323321
// TODO: the only difference is in `PutObject`: the fork doesn't use `NewVerifyingReader(r, sha256.New(), oid, expectedSize)`, need to figure out why
324322
replace github.com/charmbracelet/git-lfs-transfer => gitea.com/gitea/git-lfs-transfer v0.2.0
325323

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,6 @@ github.com/RoaringBitmap/roaring v1.9.4 h1:yhEIoH4YezLYT04s1nHehNO64EKFTop/wBhxv
8181
github.com/RoaringBitmap/roaring v1.9.4/go.mod h1:6AXUsoIEzDTFFQCe1RbGA6uFONMhvejWj5rqITANK90=
8282
github.com/SaveTheRbtz/zstd-seekable-format-go/pkg v0.7.3 h1:BP0HiyNT3AQEYi+if3wkRcIdQFHtsw6xX3Kx0glckgA=
8383
github.com/SaveTheRbtz/zstd-seekable-format-go/pkg v0.7.3/go.mod h1:hMNtySovKkn2gdDuLqnqveP+mfhUSaBdoBcr2I7Zt0E=
84-
github.com/TheFox0x7/go-gogs-client v0.0.0-20250205203315-0e70fbf458bf h1:ZEY0JnHJlOOAwOQuA8HRtkvs86DWbFPivqnNDFtk/vo=
85-
github.com/TheFox0x7/go-gogs-client v0.0.0-20250205203315-0e70fbf458bf/go.mod h1:E7sa6iZuHNhgy84Z+wN9618wGcsHjqKFo5lq0B0FpQ4=
8684
github.com/alecthomas/assert/v2 v2.11.0 h1:2Q9r3ki8+JYXvGsDyBXwH3LcJ+WK5D0gc5E8vS6K3D0=
8785
github.com/alecthomas/assert/v2 v2.11.0/go.mod h1:Bze95FyfUr7x34QZrjL+XP+0qgp/zg8yS+TtBj1WA3k=
8886
github.com/alecthomas/chroma/v2 v2.2.0/go.mod h1:vf4zrexSH54oEjJ7EdB65tGNHmH3pGZmVkgTP5RHvAs=
@@ -373,6 +371,8 @@ github.com/goccy/go-json v0.10.3 h1:KZ5WoDbxAIgm2HNbYckL0se1fHD6rz5j4ywS6ebzDqA=
373371
github.com/goccy/go-json v0.10.3/go.mod h1:oq7eo15ShAhp70Anwd5lgX2pLfOS3QCiwU/PULtXL6M=
374372
github.com/gogs/chardet v0.0.0-20211120154057-b7413eaefb8f h1:3BSP1Tbs2djlpprl7wCLuiqMaUh5SJkkzI2gDs+FgLs=
375373
github.com/gogs/chardet v0.0.0-20211120154057-b7413eaefb8f/go.mod h1:Pcatq5tYkCW2Q6yrR2VRHlbHpZ/R4/7qyL1TCF7vl14=
374+
github.com/gogs/go-gogs-client v0.0.0-20210131175652-1d7215cd8d85 h1:UjoPNDAQ5JPCjlxoJd6K8ALZqSDDhk2ymieAZOVaDg0=
375+
github.com/gogs/go-gogs-client v0.0.0-20210131175652-1d7215cd8d85/go.mod h1:fR6z1Ie6rtF7kl/vBYMfgD5/G5B1blui7z426/sj2DU=
376376
github.com/golang-jwt/jwt/v4 v4.5.1 h1:JdqV9zKUdtaa9gdPlywC3aeoEsR681PlKC+4F5gQgeo=
377377
github.com/golang-jwt/jwt/v4 v4.5.1/go.mod h1:m21LjoU+eqJr34lmDMbreY2eSTRJ1cv77w39/MY0Ch0=
378378
github.com/golang-jwt/jwt/v5 v5.2.1 h1:OuVbFODueb089Lh128TAcimifWaLhJwVflnrgM17wHk=

services/migrations/gogs.go

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import (
1313

1414
"code.gitea.io/gitea/modules/log"
1515
base "code.gitea.io/gitea/modules/migration"
16-
"code.gitea.io/gitea/modules/proxy"
1716
"code.gitea.io/gitea/modules/structs"
1817

1918
"github.com/gogs/go-gogs-client"
@@ -60,12 +59,12 @@ func (f *GogsDownloaderFactory) GitServiceType() structs.GitServiceType {
6059
// from gogs via API
6160
type GogsDownloader struct {
6261
base.NullDownloader
63-
client *gogs.Client
6462
baseURL string
6563
repoOwner string
6664
repoName string
6765
userName string
6866
password string
67+
token string
6968
openIssuesFinished bool
7069
openIssuesPages int
7170
transport http.RoundTripper
@@ -89,32 +88,39 @@ func NewGogsDownloader(_ context.Context, baseURL, userName, password, token, re
8988
baseURL: baseURL,
9089
userName: userName,
9190
password: password,
91+
token: token,
9292
repoOwner: repoOwner,
9393
repoName: repoName,
9494
}
95+
return &downloader
96+
}
9597

96-
var client *gogs.Client
97-
if len(token) != 0 {
98-
client = gogs.NewClient(baseURL, token)
99-
downloader.userName = token
100-
} else {
101-
transport := NewMigrationHTTPTransport()
102-
transport.Proxy = func(req *http.Request) (*url.URL, error) {
103-
req.SetBasicAuth(userName, password)
104-
return proxy.Proxy()(req)
105-
}
106-
downloader.transport = transport
98+
type roundTripperFunc func(req *http.Request) (*http.Response, error)
10799

108-
client = gogs.NewClient(baseURL, "")
109-
}
100+
func (rt roundTripperFunc) RoundTrip(r *http.Request) (*http.Response, error) {
101+
return rt(r)
102+
}
110103

111-
downloader.client = client
112-
return &downloader
104+
func (g *GogsDownloader) client(ctx context.Context) *gogs.Client {
105+
// Gogs client lacks the context support, so we use a custom transport
106+
// Then each request uses a dedicated client with its own context
107+
httpTransport := NewMigrationHTTPTransport()
108+
gogsClient := gogs.NewClient(g.baseURL, g.token)
109+
gogsClient.SetHTTPClient(&http.Client{
110+
Transport: roundTripperFunc(func(req *http.Request) (*http.Response, error) {
111+
if g.password != "" {
112+
// Gogs client lacks the support for basic auth, this is the only way to set it
113+
req.SetBasicAuth(g.userName, g.password)
114+
}
115+
return httpTransport.RoundTrip(req.WithContext(ctx))
116+
}),
117+
})
118+
return gogsClient
113119
}
114120

115121
// GetRepoInfo returns a repository information
116122
func (g *GogsDownloader) GetRepoInfo(ctx context.Context) (*base.Repository, error) {
117-
gr, err := g.client.GetRepo(ctx, g.repoOwner, g.repoName)
123+
gr, err := g.client(ctx).GetRepo(g.repoOwner, g.repoName)
118124
if err != nil {
119125
return nil, err
120126
}
@@ -136,7 +142,7 @@ func (g *GogsDownloader) GetMilestones(ctx context.Context) ([]*base.Milestone,
136142
perPage := 100
137143
milestones := make([]*base.Milestone, 0, perPage)
138144

139-
ms, err := g.client.ListRepoMilestones(ctx, g.repoOwner, g.repoName)
145+
ms, err := g.client(ctx).ListRepoMilestones(g.repoOwner, g.repoName)
140146
if err != nil {
141147
return nil, err
142148
}
@@ -158,7 +164,7 @@ func (g *GogsDownloader) GetMilestones(ctx context.Context) ([]*base.Milestone,
158164
func (g *GogsDownloader) GetLabels(ctx context.Context) ([]*base.Label, error) {
159165
perPage := 100
160166
labels := make([]*base.Label, 0, perPage)
161-
ls, err := g.client.ListRepoLabels(ctx, g.repoOwner, g.repoName)
167+
ls, err := g.client(ctx).ListRepoLabels(g.repoOwner, g.repoName)
162168
if err != nil {
163169
return nil, err
164170
}
@@ -199,7 +205,7 @@ func (g *GogsDownloader) GetIssues(ctx context.Context, page, _ int) ([]*base.Is
199205
func (g *GogsDownloader) getIssues(ctx context.Context, page int, state string) ([]*base.Issue, bool, error) {
200206
allIssues := make([]*base.Issue, 0, 10)
201207

202-
issues, err := g.client.ListRepoIssues(ctx, g.repoOwner, g.repoName, gogs.ListIssueOption{
208+
issues, err := g.client(ctx).ListRepoIssues(g.repoOwner, g.repoName, gogs.ListIssueOption{
203209
Page: page,
204210
State: state,
205211
})
@@ -221,7 +227,7 @@ func (g *GogsDownloader) getIssues(ctx context.Context, page int, state string)
221227
func (g *GogsDownloader) GetComments(ctx context.Context, commentable base.Commentable) ([]*base.Comment, bool, error) {
222228
allComments := make([]*base.Comment, 0, 100)
223229

224-
comments, err := g.client.ListIssueComments(ctx, g.repoOwner, g.repoName, commentable.GetForeignIndex())
230+
comments, err := g.client(ctx).ListIssueComments(g.repoOwner, g.repoName, commentable.GetForeignIndex())
225231
if err != nil {
226232
return nil, false, fmt.Errorf("error while listing repos: %w", err)
227233
}

0 commit comments

Comments
 (0)