@@ -26,46 +26,66 @@ import (
26
26
27
27
// giteaClientImpl is a wrapper around *gitea.Client, which implements higher-level methods,
28
28
// operating on the gitea structs. TODO: Verify pagination is implemented for all List* methods,
29
- // all returnedobjects are validated, and HTTP errors are handled/wrapped using handleHTTPError.
29
+ // all returned objects are validated, and HTTP errors are handled/wrapped using handleHTTPError.
30
30
// This interface is also fakeable, in order to unit-test the client.
31
31
type giteaClient interface {
32
32
// Client returns the underlying *gitea.Client
33
33
Client () * gitea.Client
34
34
35
+ // GetOrg is a wrapper for "GET /orgs/{org}".
36
+ // This function HTTP error wrapping, and validates the server result.
35
37
GetOrg (ctx context.Context , orgName string ) (* gitea.Organization , error )
36
-
38
+ // ListOrgs is a wrapper for "GET /user/orgs".
37
39
ListOrgs (ctx context.Context ) ([]* gitea.Organization , error )
38
40
41
+ // ListOrgTeamMembers is a wrapper for "GET /orgs/{org}/teams" then "GET /teams/{team}/members".
39
42
ListOrgTeamMembers (ctx context.Context , orgName , teamName string ) ([]* gitea.User , error )
40
-
43
+ // ListOrgTeams is a wrapper for "GET /orgs/{org}/teams".
41
44
ListOrgTeams (ctx context.Context , orgName string ) ([]* gitea.Team , error )
42
45
46
+ // GetRepo is a wrapper for "GET /repos/{owner}/{repo}".
47
+ // This function handles HTTP error wrapping, and validates the server result.
43
48
GetRepo (ctx context.Context , owner , repo string ) (* gitea.Repository , error )
44
-
49
+ // ListOrgRepos is a wrapper for "GET /orgs/{org}/repos".
45
50
ListOrgRepos (ctx context.Context , org string ) ([]* gitea.Repository , error )
46
-
51
+ // ListUserRepos is a wrapper for "GET /users/{username}/repos".
47
52
ListUserRepos (ctx context.Context , username string ) ([]* gitea.Repository , error )
48
-
53
+ // CreateRepo is a wrapper for "POST /user/repos" (if orgName == "")
54
+ // or "POST /orgs/{org}/repos" (if orgName != "").
55
+ // This function handles HTTP error wrapping, and validates the server result.
49
56
CreateRepo (ctx context.Context , orgName string , req * gitea.CreateRepoOption ) (* gitea.Repository , error )
50
-
57
+ // UpdateRepo is a wrapper for "PATCH /repos/{owner}/{repo}".
58
+ // This function handles HTTP error wrapping, and validates the server result.
51
59
UpdateRepo (ctx context.Context , owner , repo string , req * gitea.EditRepoOption ) (* gitea.Repository , error )
60
+ // DeleteRepo is a wrapper for "DELETE /repos/{owner}/{repo}".
61
+ // This function handles HTTP error wrapping.
52
62
// DANGEROUS COMMAND: In order to use this, you must set destructiveActions to true.
53
63
DeleteRepo (ctx context.Context , owner , repo string ) error
54
64
65
+ // ListKeys is a wrapper for "GET /repos/{owner}/{repo}/keys".
66
+ // This function handles pagination, HTTP error wrapping, and validates the server result.
55
67
ListKeys (ctx context.Context , owner , repo string ) ([]* gitea.DeployKey , error )
56
-
68
+ // ListCommitsPage is a wrapper for "GET /repos/{owner}/{repo}/git/commits".
69
+ // This function handles pagination, HTTP error wrapping.
57
70
ListCommitsPage (ctx context.Context , owner , repo , branch string , perPage int , page int ) ([]* gitea.Commit , error )
58
-
71
+ // CreateKey is a wrapper for "POST /repos/{owner}/{repo}/keys".
72
+ // This function handles HTTP error wrapping, and validates the server result.
59
73
CreateKey (ctx context.Context , owner , repo string , req * gitea.DeployKey ) (* gitea.DeployKey , error )
60
-
74
+ // DeleteKey is a wrapper for "DELETE /repos/{owner}/{repo}/keys/{key_id}".
75
+ // This function handles HTTP error wrapping.
61
76
DeleteKey (ctx context.Context , owner , repo string , id int64 ) error
62
77
78
+ // GetTeamPermissions is a wrapper for "GET /repos/{owner}/{repo}/teams/{team_slug}
79
+ // This function handles HTTP error wrapping, and validates the server result.
63
80
GetTeamPermissions (ctx context.Context , orgName , repo , teamName string ) (* gitea.AccessMode , error )
64
-
81
+ // ListRepoTeams is a wrapper for "GET /repos/{owner}/{repo}/teams".
82
+ // This function handles pagination, HTTP error wrapping, and validates the server result.
65
83
ListRepoTeams (ctx context.Context , orgName , repo string ) ([]* gitea.Team , error )
66
-
84
+ // AddTeam is a wrapper for "PUT /repos/{owner}/{repo}/teams/{team_slug}".
85
+ // This function handles HTTP error wrapping.
67
86
AddTeam (ctx context.Context , orgName , repo , teamName string , permission gitprovider.RepositoryPermission ) error
68
-
87
+ // RemoveTeam is a wrapper for "DELETE /repos/{owner}/{repo}/teams/{team_slug}".
88
+ // This function handles HTTP error wrapping.
69
89
RemoveTeam (ctx context.Context , orgName , repo , teamName string ) error
70
90
}
71
91
@@ -80,7 +100,7 @@ func (c *giteaClientImpl) Client() *gitea.Client {
80
100
return c .c
81
101
}
82
102
83
- func (c * giteaClientImpl ) GetOrg (ctx context.Context , orgName string ) (* gitea.Organization , error ) {
103
+ func (c * giteaClientImpl ) GetOrg (_ context.Context , orgName string ) (* gitea.Organization , error ) {
84
104
apiObj , res , err := c .c .GetOrg (orgName )
85
105
if err != nil {
86
106
return nil , handleHTTPError (res , err )
@@ -92,10 +112,20 @@ func (c *giteaClientImpl) GetOrg(ctx context.Context, orgName string) (*gitea.Or
92
112
return apiObj , nil
93
113
}
94
114
95
- func (c * giteaClientImpl ) ListOrgs (ctx context.Context ) ([]* gitea.Organization , error ) {
115
+ func (c * giteaClientImpl ) ListOrgs (_ context.Context ) ([]* gitea.Organization , error ) {
96
116
opts := gitea.ListOrgsOptions {}
97
- apiObjs , _ , err := c .c .ListMyOrgs (opts )
98
-
117
+ apiObjs := []* gitea.Organization {}
118
+ listOpts := & opts .ListOptions
119
+
120
+ err := allPages (listOpts , func () (* gitea.Response , error ) {
121
+ // GET /user/orgs"
122
+ pageObjs , resp , listErr := c .c .ListMyOrgs (opts )
123
+ if len (pageObjs ) > 0 {
124
+ apiObjs = append (apiObjs , pageObjs ... )
125
+ return resp , listErr
126
+ }
127
+ return nil , nil
128
+ })
99
129
if err != nil {
100
130
return nil , err
101
131
}
@@ -110,33 +140,50 @@ func (c *giteaClientImpl) ListOrgs(ctx context.Context) ([]*gitea.Organization,
110
140
}
111
141
112
142
func (c * giteaClientImpl ) ListOrgTeamMembers (ctx context.Context , orgName , teamName string ) ([]* gitea.User , error ) {
113
- orgTeamOpts := gitea.ListTeamsOptions {}
114
- teams , _ , err := c .c .ListOrgTeams (orgName , orgTeamOpts )
143
+ teams , err := c .ListOrgTeams (ctx , orgName )
115
144
if err != nil {
116
145
return nil , err
117
146
}
118
147
apiObjs := []* gitea.User {}
148
+ teamFound := false
119
149
for _ , team := range teams {
150
+ if team .Name != teamName {
151
+ continue
152
+ }
153
+ teamFound = true
120
154
users , _ , err := c .c .ListTeamMembers (team .ID , gitea.ListTeamMembersOptions {})
121
155
if err != nil {
122
156
continue
123
157
}
124
158
apiObjs = append (apiObjs , users ... )
125
159
}
126
-
127
- return apiObjs , nil
160
+ if teamFound {
161
+ return apiObjs , nil
162
+ }
163
+ return nil , gitprovider .ErrNotFound
128
164
}
129
165
130
- func (c * giteaClientImpl ) ListOrgTeams (ctx context.Context , orgName string ) ([]* gitea.Team , error ) {
131
- orgTeamOpts := gitea.ListTeamsOptions {}
132
- apiObjs , _ , err := c .c .ListOrgTeams (orgName , orgTeamOpts )
166
+ func (c * giteaClientImpl ) ListOrgTeams (_ context.Context , orgName string ) ([]* gitea.Team , error ) {
167
+ opts := gitea.ListTeamsOptions {}
168
+ apiObjs := []* gitea.Team {}
169
+ listOpts := & opts .ListOptions
170
+
171
+ err := allPages (listOpts , func () (* gitea.Response , error ) {
172
+ // GET /orgs/{org}/teams"
173
+ pageObjs , resp , listErr := c .c .ListOrgTeams (orgName , opts )
174
+ if len (pageObjs ) > 0 {
175
+ apiObjs = append (apiObjs , pageObjs ... )
176
+ return resp , listErr
177
+ }
178
+ return nil , nil
179
+ })
133
180
if err != nil {
134
181
return nil , err
135
182
}
136
183
return apiObjs , nil
137
184
}
138
185
139
- func (c * giteaClientImpl ) GetRepo (ctx context.Context , owner , repo string ) (* gitea.Repository , error ) {
186
+ func (c * giteaClientImpl ) GetRepo (_ context.Context , owner , repo string ) (* gitea.Repository , error ) {
140
187
apiObj , res , err := c .c .GetRepo (owner , repo )
141
188
return validateRepositoryAPIResp (apiObj , res , err )
142
189
}
@@ -153,9 +200,20 @@ func validateRepositoryAPIResp(apiObj *gitea.Repository, res *gitea.Response, er
153
200
return apiObj , nil
154
201
}
155
202
156
- func (c * giteaClientImpl ) ListOrgRepos (ctx context.Context , org string ) ([]* gitea.Repository , error ) {
203
+ func (c * giteaClientImpl ) ListOrgRepos (_ context.Context , org string ) ([]* gitea.Repository , error ) {
157
204
opts := gitea.ListOrgReposOptions {}
158
- apiObjs , _ , err := c .c .ListOrgRepos (org , opts )
205
+ apiObjs := []* gitea.Repository {}
206
+ listOpts := & opts .ListOptions
207
+
208
+ err := allPages (listOpts , func () (* gitea.Response , error ) {
209
+ // GET /orgs/{org}/repos
210
+ pageObjs , resp , listErr := c .c .ListOrgRepos (org , opts )
211
+ if len (pageObjs ) > 0 {
212
+ apiObjs = append (apiObjs , pageObjs ... )
213
+ return resp , listErr
214
+ }
215
+ return nil , nil
216
+ })
159
217
if err != nil {
160
218
return nil , err
161
219
}
@@ -172,16 +230,27 @@ func validateRepositoryObjects(apiObjs []*gitea.Repository) ([]*gitea.Repository
172
230
return apiObjs , nil
173
231
}
174
232
175
- func (c * giteaClientImpl ) ListUserRepos (ctx context.Context , username string ) ([]* gitea.Repository , error ) {
233
+ func (c * giteaClientImpl ) ListUserRepos (_ context.Context , username string ) ([]* gitea.Repository , error ) {
176
234
opts := gitea.ListReposOptions {}
177
- apiObjs , _ , err := c .c .ListUserRepos (username , opts )
235
+ apiObjs := []* gitea.Repository {}
236
+ listOpts := & opts .ListOptions
237
+
238
+ err := allPages (listOpts , func () (* gitea.Response , error ) {
239
+ // GET /users/{username}/repos
240
+ pageObjs , resp , listErr := c .c .ListUserRepos (username , opts )
241
+ if len (pageObjs ) > 0 {
242
+ apiObjs = append (apiObjs , pageObjs ... )
243
+ return resp , listErr
244
+ }
245
+ return nil , nil
246
+ })
178
247
if err != nil {
179
248
return nil , err
180
249
}
181
250
return validateRepositoryObjects (apiObjs )
182
251
}
183
252
184
- func (c * giteaClientImpl ) CreateRepo (ctx context.Context , orgName string , req * gitea.CreateRepoOption ) (* gitea.Repository , error ) {
253
+ func (c * giteaClientImpl ) CreateRepo (_ context.Context , orgName string , req * gitea.CreateRepoOption ) (* gitea.Repository , error ) {
185
254
if orgName != "" {
186
255
apiObj , res , err := c .c .CreateOrgRepo (orgName , * req )
187
256
return validateRepositoryAPIResp (apiObj , res , err )
@@ -190,12 +259,12 @@ func (c *giteaClientImpl) CreateRepo(ctx context.Context, orgName string, req *g
190
259
return validateRepositoryAPIResp (apiObj , res , err )
191
260
}
192
261
193
- func (c * giteaClientImpl ) UpdateRepo (ctx context.Context , owner , repo string , req * gitea.EditRepoOption ) (* gitea.Repository , error ) {
262
+ func (c * giteaClientImpl ) UpdateRepo (_ context.Context , owner , repo string , req * gitea.EditRepoOption ) (* gitea.Repository , error ) {
194
263
apiObj , res , err := c .c .EditRepo (owner , repo , * req )
195
264
return validateRepositoryAPIResp (apiObj , res , err )
196
265
}
197
266
198
- func (c * giteaClientImpl ) DeleteRepo (ctx context.Context , owner , repo string ) error {
267
+ func (c * giteaClientImpl ) DeleteRepo (_ context.Context , owner , repo string ) error {
199
268
// Don't allow deleting repositories if the user didn't explicitly allow dangerous API calls.
200
269
if ! c .destructiveActions {
201
270
return fmt .Errorf ("cannot delete repository: %w" , gitprovider .ErrDestructiveCallDisallowed )
@@ -204,9 +273,21 @@ func (c *giteaClientImpl) DeleteRepo(ctx context.Context, owner, repo string) er
204
273
return handleHTTPError (res , err )
205
274
}
206
275
207
- func (c * giteaClientImpl ) ListKeys (ctx context.Context , owner , repo string ) ([]* gitea.DeployKey , error ) {
276
+ func (c * giteaClientImpl ) ListKeys (_ context.Context , owner , repo string ) ([]* gitea.DeployKey , error ) {
208
277
opts := gitea.ListDeployKeysOptions {}
209
- apiObjs , _ , err := c .c .ListDeployKeys (owner , repo , opts )
278
+ apiObjs := []* gitea.DeployKey {}
279
+ listOpts := & opts .ListOptions
280
+
281
+ err := allPages (listOpts , func () (* gitea.Response , error ) {
282
+ // GET /repos/{owner}/{repo}/keys"
283
+ pageObjs , resp , listErr := c .c .ListDeployKeys (owner , repo , opts )
284
+ if len (pageObjs ) > 0 {
285
+ apiObjs = append (apiObjs , pageObjs ... )
286
+ return resp , listErr
287
+ }
288
+ return nil , nil
289
+ })
290
+
210
291
if err != nil {
211
292
return nil , err
212
293
}
@@ -219,7 +300,7 @@ func (c *giteaClientImpl) ListKeys(ctx context.Context, owner, repo string) ([]*
219
300
return apiObjs , nil
220
301
}
221
302
222
- func (c * giteaClientImpl ) ListCommitsPage (ctx context.Context , owner , repo , branch string , perPage int , page int ) ([]* gitea.Commit , error ) {
303
+ func (c * giteaClientImpl ) ListCommitsPage (_ context.Context , owner , repo , branch string , perPage int , page int ) ([]* gitea.Commit , error ) {
223
304
opts := gitea.ListCommitOptions {
224
305
ListOptions : gitea.ListOptions {
225
306
PageSize : perPage ,
@@ -235,7 +316,7 @@ func (c *giteaClientImpl) ListCommitsPage(ctx context.Context, owner, repo, bran
235
316
return apiObjs , nil
236
317
}
237
318
238
- func (c * giteaClientImpl ) CreateKey (ctx context.Context , owner , repo string , req * gitea.DeployKey ) (* gitea.DeployKey , error ) {
319
+ func (c * giteaClientImpl ) CreateKey (_ context.Context , owner , repo string , req * gitea.DeployKey ) (* gitea.DeployKey , error ) {
239
320
opts := gitea.CreateKeyOption {Title : req .Title , Key : req .Key , ReadOnly : req .ReadOnly }
240
321
apiObj , res , err := c .c .CreateDeployKey (owner , repo , opts )
241
322
if err != nil {
@@ -247,12 +328,12 @@ func (c *giteaClientImpl) CreateKey(ctx context.Context, owner, repo string, req
247
328
return apiObj , nil
248
329
}
249
330
250
- func (c * giteaClientImpl ) DeleteKey (ctx context.Context , owner , repo string , id int64 ) error {
331
+ func (c * giteaClientImpl ) DeleteKey (_ context.Context , owner , repo string , id int64 ) error {
251
332
res , err := c .c .DeleteDeployKey (owner , repo , id )
252
333
return handleHTTPError (res , err )
253
334
}
254
335
255
- func (c * giteaClientImpl ) GetTeamPermissions (ctx context.Context , orgName , repo , teamName string ) (* gitea.AccessMode , error ) {
336
+ func (c * giteaClientImpl ) GetTeamPermissions (_ context.Context , orgName , repo , teamName string ) (* gitea.AccessMode , error ) {
256
337
apiObj , res , err := c .c .CheckRepoTeam (orgName , repo , teamName )
257
338
if err != nil {
258
339
return nil , handleHTTPError (res , err )
@@ -262,8 +343,7 @@ func (c *giteaClientImpl) GetTeamPermissions(ctx context.Context, orgName, repo,
262
343
}
263
344
264
345
func (c * giteaClientImpl ) ListRepoTeams (ctx context.Context , orgName , repo string ) ([]* gitea.Team , error ) {
265
- opts := gitea.ListTeamsOptions {}
266
- teamObjs , _ , err := c .c .ListOrgTeams (orgName , opts )
346
+ teamObjs , err := c .ListOrgTeams (ctx , orgName )
267
347
if err != nil {
268
348
return nil , err
269
349
}
@@ -280,12 +360,12 @@ func (c *giteaClientImpl) ListRepoTeams(ctx context.Context, orgName, repo strin
280
360
return apiObjs , nil
281
361
}
282
362
283
- func (c * giteaClientImpl ) AddTeam (ctx context.Context , orgName , repo , teamName string , permission gitprovider.RepositoryPermission ) error {
363
+ func (c * giteaClientImpl ) AddTeam (_ context.Context , orgName , repo , teamName string , permission gitprovider.RepositoryPermission ) error {
284
364
res , err := c .c .AddRepoTeam (orgName , repo , teamName )
285
365
return handleHTTPError (res , err )
286
366
}
287
367
288
- func (c * giteaClientImpl ) RemoveTeam (ctx context.Context , orgName , repo , teamName string ) error {
368
+ func (c * giteaClientImpl ) RemoveTeam (_ context.Context , orgName , repo , teamName string ) error {
289
369
res , err := c .c .RemoveRepoTeam (orgName , repo , teamName )
290
370
return handleHTTPError (res , err )
291
371
}
0 commit comments