Skip to content

Commit 35c5f80

Browse files
committed
1. split integration tests
2. underscored unused args 3. commented exported api 4. enabled custom domain auth_test cases 5. typo, unuse var fixes 6. pagination support and bugfixes Signed-off-by: Malar Kannan <[email protected]>
1 parent 4f92274 commit 35c5f80

9 files changed

+1047
-740
lines changed

gitea/auth.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,6 @@ import (
2828
const (
2929
// DefaultDomain specifies the default domain used as the backend.
3030
DefaultDomain = "gitea.com"
31-
// TokenVariable is the common name for the environment variable
32-
// containing a Gitea authentication token.
33-
TokenVariable = "GITEA_TOKEN" // #nosec G101
3431
)
3532

3633
// NewClient creates a new gitprovider.Client instance for Gitea API endpoints.
@@ -56,7 +53,7 @@ func NewClient(optFns ...gitprovider.ClientOption) (gitprovider.Client, error) {
5653
var domain string
5754

5855
// Gitea is primarily self-hosted
59-
// using test domain if domain not provided
56+
// using default domain if domain not provided
6057
domain = *opts.Domain
6158
if opts.Domain == nil || *opts.Domain == DefaultDomain {
6259
// No domain set or the default gitea.com used

gitea/auth_test.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,16 @@ func Test_DomainVariations(t *testing.T) {
3434
opts: gitprovider.WithDomain("gitea.com"),
3535
want: "gitea.com",
3636
},
37-
// {
38-
// name: "custom domain without protocol",
39-
// opts: gitprovider.WithDomain("my-gitea.dev.com"),
40-
// want: "https://my-gitea.dev.com",
41-
// },
42-
// {
43-
// name: "custom domain with https protocol",
44-
// opts: gitprovider.WithDomain("https://my-gitea.dev.com"),
45-
// want: "https://my-gitea.dev.com",
46-
// },
37+
{
38+
name: "custom domain without protocol",
39+
opts: gitprovider.WithDomain("my-gitea.dev.com"),
40+
want: "https://my-gitea.dev.com",
41+
},
42+
{
43+
name: "custom domain with https protocol",
44+
opts: gitprovider.WithDomain("https://my-gitea.dev.com"),
45+
want: "https://my-gitea.dev.com",
46+
},
4747
{
4848
name: "custom domain with http protocol",
4949
opts: gitprovider.WithDomain("http://my-gitea.dev.com"),

gitea/giteaclient.go

Lines changed: 122 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -26,46 +26,66 @@ import (
2626

2727
// giteaClientImpl is a wrapper around *gitea.Client, which implements higher-level methods,
2828
// 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.
3030
// This interface is also fakeable, in order to unit-test the client.
3131
type giteaClient interface {
3232
// Client returns the underlying *gitea.Client
3333
Client() *gitea.Client
3434

35+
// GetOrg is a wrapper for "GET /orgs/{org}".
36+
// This function HTTP error wrapping, and validates the server result.
3537
GetOrg(ctx context.Context, orgName string) (*gitea.Organization, error)
36-
38+
// ListOrgs is a wrapper for "GET /user/orgs".
3739
ListOrgs(ctx context.Context) ([]*gitea.Organization, error)
3840

41+
// ListOrgTeamMembers is a wrapper for "GET /orgs/{org}/teams" then "GET /teams/{team}/members".
3942
ListOrgTeamMembers(ctx context.Context, orgName, teamName string) ([]*gitea.User, error)
40-
43+
// ListOrgTeams is a wrapper for "GET /orgs/{org}/teams".
4144
ListOrgTeams(ctx context.Context, orgName string) ([]*gitea.Team, error)
4245

46+
// GetRepo is a wrapper for "GET /repos/{owner}/{repo}".
47+
// This function handles HTTP error wrapping, and validates the server result.
4348
GetRepo(ctx context.Context, owner, repo string) (*gitea.Repository, error)
44-
49+
// ListOrgRepos is a wrapper for "GET /orgs/{org}/repos".
4550
ListOrgRepos(ctx context.Context, org string) ([]*gitea.Repository, error)
46-
51+
// ListUserRepos is a wrapper for "GET /users/{username}/repos".
4752
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.
4956
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.
5159
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.
5262
// DANGEROUS COMMAND: In order to use this, you must set destructiveActions to true.
5363
DeleteRepo(ctx context.Context, owner, repo string) error
5464

65+
// ListKeys is a wrapper for "GET /repos/{owner}/{repo}/keys".
66+
// This function handles pagination, HTTP error wrapping, and validates the server result.
5567
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.
5770
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.
5973
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.
6176
DeleteKey(ctx context.Context, owner, repo string, id int64) error
6277

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.
6380
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.
6583
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.
6786
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.
6989
RemoveTeam(ctx context.Context, orgName, repo, teamName string) error
7090
}
7191

@@ -80,7 +100,7 @@ func (c *giteaClientImpl) Client() *gitea.Client {
80100
return c.c
81101
}
82102

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) {
84104
apiObj, res, err := c.c.GetOrg(orgName)
85105
if err != nil {
86106
return nil, handleHTTPError(res, err)
@@ -92,10 +112,20 @@ func (c *giteaClientImpl) GetOrg(ctx context.Context, orgName string) (*gitea.Or
92112
return apiObj, nil
93113
}
94114

95-
func (c *giteaClientImpl) ListOrgs(ctx context.Context) ([]*gitea.Organization, error) {
115+
func (c *giteaClientImpl) ListOrgs(_ context.Context) ([]*gitea.Organization, error) {
96116
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+
})
99129
if err != nil {
100130
return nil, err
101131
}
@@ -110,33 +140,50 @@ func (c *giteaClientImpl) ListOrgs(ctx context.Context) ([]*gitea.Organization,
110140
}
111141

112142
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)
115144
if err != nil {
116145
return nil, err
117146
}
118147
apiObjs := []*gitea.User{}
148+
teamFound := false
119149
for _, team := range teams {
150+
if team.Name != teamName {
151+
continue
152+
}
153+
teamFound = true
120154
users, _, err := c.c.ListTeamMembers(team.ID, gitea.ListTeamMembersOptions{})
121155
if err != nil {
122156
continue
123157
}
124158
apiObjs = append(apiObjs, users...)
125159
}
126-
127-
return apiObjs, nil
160+
if teamFound {
161+
return apiObjs, nil
162+
}
163+
return nil, gitprovider.ErrNotFound
128164
}
129165

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+
})
133180
if err != nil {
134181
return nil, err
135182
}
136183
return apiObjs, nil
137184
}
138185

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) {
140187
apiObj, res, err := c.c.GetRepo(owner, repo)
141188
return validateRepositoryAPIResp(apiObj, res, err)
142189
}
@@ -153,9 +200,20 @@ func validateRepositoryAPIResp(apiObj *gitea.Repository, res *gitea.Response, er
153200
return apiObj, nil
154201
}
155202

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) {
157204
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+
})
159217
if err != nil {
160218
return nil, err
161219
}
@@ -172,16 +230,27 @@ func validateRepositoryObjects(apiObjs []*gitea.Repository) ([]*gitea.Repository
172230
return apiObjs, nil
173231
}
174232

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) {
176234
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+
})
178247
if err != nil {
179248
return nil, err
180249
}
181250
return validateRepositoryObjects(apiObjs)
182251
}
183252

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) {
185254
if orgName != "" {
186255
apiObj, res, err := c.c.CreateOrgRepo(orgName, *req)
187256
return validateRepositoryAPIResp(apiObj, res, err)
@@ -190,12 +259,12 @@ func (c *giteaClientImpl) CreateRepo(ctx context.Context, orgName string, req *g
190259
return validateRepositoryAPIResp(apiObj, res, err)
191260
}
192261

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) {
194263
apiObj, res, err := c.c.EditRepo(owner, repo, *req)
195264
return validateRepositoryAPIResp(apiObj, res, err)
196265
}
197266

198-
func (c *giteaClientImpl) DeleteRepo(ctx context.Context, owner, repo string) error {
267+
func (c *giteaClientImpl) DeleteRepo(_ context.Context, owner, repo string) error {
199268
// Don't allow deleting repositories if the user didn't explicitly allow dangerous API calls.
200269
if !c.destructiveActions {
201270
return fmt.Errorf("cannot delete repository: %w", gitprovider.ErrDestructiveCallDisallowed)
@@ -204,9 +273,21 @@ func (c *giteaClientImpl) DeleteRepo(ctx context.Context, owner, repo string) er
204273
return handleHTTPError(res, err)
205274
}
206275

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) {
208277
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+
210291
if err != nil {
211292
return nil, err
212293
}
@@ -219,7 +300,7 @@ func (c *giteaClientImpl) ListKeys(ctx context.Context, owner, repo string) ([]*
219300
return apiObjs, nil
220301
}
221302

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) {
223304
opts := gitea.ListCommitOptions{
224305
ListOptions: gitea.ListOptions{
225306
PageSize: perPage,
@@ -235,7 +316,7 @@ func (c *giteaClientImpl) ListCommitsPage(ctx context.Context, owner, repo, bran
235316
return apiObjs, nil
236317
}
237318

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) {
239320
opts := gitea.CreateKeyOption{Title: req.Title, Key: req.Key, ReadOnly: req.ReadOnly}
240321
apiObj, res, err := c.c.CreateDeployKey(owner, repo, opts)
241322
if err != nil {
@@ -247,12 +328,12 @@ func (c *giteaClientImpl) CreateKey(ctx context.Context, owner, repo string, req
247328
return apiObj, nil
248329
}
249330

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 {
251332
res, err := c.c.DeleteDeployKey(owner, repo, id)
252333
return handleHTTPError(res, err)
253334
}
254335

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) {
256337
apiObj, res, err := c.c.CheckRepoTeam(orgName, repo, teamName)
257338
if err != nil {
258339
return nil, handleHTTPError(res, err)
@@ -262,8 +343,7 @@ func (c *giteaClientImpl) GetTeamPermissions(ctx context.Context, orgName, repo,
262343
}
263344

264345
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)
267347
if err != nil {
268348
return nil, err
269349
}
@@ -280,12 +360,12 @@ func (c *giteaClientImpl) ListRepoTeams(ctx context.Context, orgName, repo strin
280360
return apiObjs, nil
281361
}
282362

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 {
284364
res, err := c.c.AddRepoTeam(orgName, repo, teamName)
285365
return handleHTTPError(res, err)
286366
}
287367

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 {
289369
res, err := c.c.RemoveRepoTeam(orgName, repo, teamName)
290370
return handleHTTPError(res, err)
291371
}

0 commit comments

Comments
 (0)