Skip to content

Commit 4f3128b

Browse files
committed
fix test
1 parent b5d35fd commit 4f3128b

File tree

10 files changed

+85
-139
lines changed

10 files changed

+85
-139
lines changed

pkg/config/config.go

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package config
22

33
import (
4-
"fmt"
5-
64
"github.com/conductorone/baton-sdk/pkg/field"
75
)
86

@@ -27,6 +25,16 @@ var (
2725
"app-privatekey",
2826
field.WithDescription("The private key used to connect to the GitHub App"),
2927
)
28+
fieldRelationships = []field.SchemaFieldRelationship{
29+
field.FieldsAtLeastOneUsed(
30+
accessTokenField,
31+
appPrivateKey,
32+
),
33+
field.FieldsRequiredTogether(
34+
appPrivateKey,
35+
appIDField,
36+
),
37+
}
3038
)
3139

3240
//go:generate go run ./gen
@@ -36,13 +44,4 @@ var Config = field.NewConfiguration([]field.SchemaField{
3644
instanceUrlField,
3745
appIDField,
3846
appPrivateKey,
39-
})
40-
41-
func ValidateConfig(cfg *Github) error {
42-
apiKey := cfg.GetString(accessTokenField.FieldName)
43-
appKey := cfg.GetString(appIDField.FieldName)
44-
if len(apiKey) == 0 && len(appKey) == 0 {
45-
return fmt.Errorf("api-key or app-privatekey is missing")
46-
}
47-
return nil
48-
}
47+
}, fieldRelationships...)

pkg/connector/connector.go

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -160,25 +160,25 @@ func (gh *GitHub) validateAppCredentials(ctx context.Context) (annotations.Annot
160160
}
161161

162162
for _, o := range gh.orgs {
163-
if _, ok := iResp.names[o]; !ok {
163+
if _, ok := iResp.accountNames[o]; !ok {
164164
return nil, fmt.Errorf("access token must be an admin on the %s organization", o)
165165
}
166166
}
167167
return nil, nil
168168
}
169169

170170
type getAllInstallationsResp struct {
171-
ids map[int64]struct{}
172171
installationIds map[int64]*github.Installation
173-
names map[string]struct{}
172+
accountIds map[int64]struct{}
173+
accountNames map[string]struct{}
174174
}
175175

176176
func getAllInstallations(ctx context.Context, c *github.Client) (getAllInstallationsResp, error) {
177177
var (
178-
installationsMap = make(map[int64]struct{})
179-
installationsIDMap = make(map[int64]*github.Installation)
180-
installationsNameMap = make(map[string]struct{})
181-
page = 0
178+
AccountIDMap = make(map[int64]struct{})
179+
installationsIDToInstallation = make(map[int64]*github.Installation)
180+
AccountNameMap = make(map[string]struct{})
181+
page = 0
182182
)
183183
for {
184184
installations, resp, err := c.Apps.ListInstallations(ctx, &github.ListOptions{Page: page})
@@ -192,9 +192,9 @@ func getAllInstallations(ctx context.Context, c *github.Client) (getAllInstallat
192192
if installation.GetAccount().GetType() != "Organization" {
193193
continue
194194
}
195-
installationsMap[installation.GetAccount().GetID()] = struct{}{}
196-
installationsIDMap[installation.GetID()] = installation
197-
installationsNameMap[installation.GetAccount().GetLogin()] = struct{}{}
195+
installationsIDToInstallation[installation.GetID()] = installation
196+
AccountIDMap[installation.GetAccount().GetID()] = struct{}{}
197+
AccountNameMap[installation.GetAccount().GetLogin()] = struct{}{}
198198
}
199199

200200
if resp.NextPage == 0 {
@@ -203,9 +203,9 @@ func getAllInstallations(ctx context.Context, c *github.Client) (getAllInstallat
203203
page = resp.NextPage
204204
}
205205
return getAllInstallationsResp{
206-
ids: installationsMap,
207-
installationIds: installationsIDMap,
208-
names: installationsNameMap,
206+
installationIds: installationsIDToInstallation,
207+
accountIds: AccountIDMap,
208+
accountNames: AccountNameMap,
209209
}, nil
210210
}
211211

@@ -293,7 +293,7 @@ func newAppClient(ctx context.Context, ghc *cfg.Github) (gitHubApp, error) {
293293

294294
var (
295295
installationsClient = make(map[int64]*github.Client, len(iResp.installationIds))
296-
installationsGLClient = make(map[int64]*githubv4.Client, len(iResp.ids))
296+
installationsGLClient = make(map[int64]*githubv4.Client, len(iResp.accountIds))
297297
)
298298
for id, installation := range iResp.installationIds {
299299
c, glc, err := getInstallationClient(ctx, ghc.InstanceUrl, id, token)

pkg/connector/helpers.go

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func titleCase(s string) string {
2727
type orgNameCache struct {
2828
sync.RWMutex
2929
c *github.Client
30-
appClients map[int64]*github.Client
30+
appClients map[int64]*github.Client // org id -> app installation client.
3131
orgNames map[string]string
3232
}
3333

@@ -231,3 +231,21 @@ func isNotFoundError(resp *github.Response) bool {
231231
}
232232
return resp.StatusCode == http.StatusNotFound
233233
}
234+
235+
// getClient returns PAT client if PAT is used.
236+
// otherwise it returns client using app installation token.
237+
func getClient(patClient *github.Client, app gitHubApp, orgId string) (*github.Client, error) {
238+
client := patClient
239+
if len(app.appInstallationClient) > 0 {
240+
i, err := strconv.ParseInt(orgId, 10, 64)
241+
if err != nil {
242+
return nil, err
243+
}
244+
var ok bool
245+
client, ok = app.appInstallationClient[i]
246+
if !ok {
247+
return nil, fmt.Errorf("organization: %d doesn't exist", i)
248+
}
249+
}
250+
return client, nil
251+
}

pkg/connector/org.go

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -200,18 +200,11 @@ func (o *orgResourceType) Grants(
200200
return nil, "", nil, err
201201
}
202202

203-
cli := o.client
204-
if len(o.app.appInstallationClient) > 0 {
205-
i, err := strconv.ParseInt(resource.Id.GetResource(), 10, 64)
206-
if err != nil {
207-
return nil, "", nil, err
208-
}
209-
var ok bool
210-
cli, ok = o.app.appInstallationClient[i]
211-
if !ok {
212-
return nil, "", nil, fmt.Errorf("organization: %d doesn't exist", i)
213-
}
203+
cli, err := getClient(o.client, o.app, resource.Id.GetResource())
204+
if err != nil {
205+
return nil, "", nil, err
214206
}
207+
215208
users, resp, err := cli.Organizations.ListMembers(ctx, orgName, &opts)
216209
if err != nil {
217210
return nil, "", nil, fmt.Errorf("github-connectorv2: failed to list org members: %w", err)

pkg/connector/repository.go

Lines changed: 16 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -86,18 +86,11 @@ func (o *repositoryResourceType) List(ctx context.Context, parentID *v2.Resource
8686
},
8787
}
8888

89-
cli := o.client
90-
if len(o.app.appInstallationClient) > 0 {
91-
i, err := strconv.ParseInt(parentID.GetResource(), 10, 64)
92-
if err != nil {
93-
return nil, "", nil, err
94-
}
95-
var ok bool
96-
cli, ok = o.app.appInstallationClient[i]
97-
if !ok {
98-
return nil, "", nil, fmt.Errorf("organization: %d doesn't exist", i)
99-
}
89+
cli, err := getClient(o.client, o.app, parentID.GetResource())
90+
if err != nil {
91+
return nil, "", nil, err
10092
}
93+
10194
repos, resp, err := cli.Repositories.ListByOrg(ctx, orgName, opts)
10295
if err != nil {
10396
return nil, "", nil, fmt.Errorf("github-connector: failed to list repositories: %w", err)
@@ -159,18 +152,11 @@ func (o *repositoryResourceType) Grants(
159152
var rv []*v2.Grant
160153
var reqAnnos annotations.Annotations
161154

162-
cli := o.client
163-
if len(o.app.appInstallationClient) > 0 {
164-
i, err := strconv.ParseInt(resource.GetParentResourceId().GetResource(), 10, 64)
165-
if err != nil {
166-
return nil, "", nil, err
167-
}
168-
var ok bool
169-
cli, ok = o.app.appInstallationClient[i]
170-
if !ok {
171-
return nil, "", nil, fmt.Errorf("organization: %d doesn't exist", i)
172-
}
155+
cli, err := getClient(o.client, o.app, resource.GetParentResourceId().GetResource())
156+
if err != nil {
157+
return nil, "", nil, err
173158
}
159+
174160
switch bag.ResourceTypeID() {
175161
case resourceTypeRepository.Id:
176162
bag.Pop()
@@ -287,18 +273,11 @@ func (o *repositoryResourceType) Grant(ctx context.Context, principal *v2.Resour
287273
return nil, err
288274
}
289275

290-
cli := o.client
291-
if len(o.app.appInstallationClient) > 0 {
292-
i, err := strconv.ParseInt(en.Resource.ParentResourceId.GetResource(), 10, 64)
293-
if err != nil {
294-
return nil, err
295-
}
296-
var ok bool
297-
cli, ok = o.app.appInstallationClient[i]
298-
if !ok {
299-
return nil, fmt.Errorf("organization: %d doesn't exist", i)
300-
}
276+
cli, err := getClient(o.client, o.app, en.Resource.ParentResourceId.GetResource())
277+
if err != nil {
278+
return nil, err
301279
}
280+
302281
repo, _, err := cli.Repositories.GetByID(ctx, repoID)
303282
if err != nil {
304283
return nil, fmt.Errorf("github-connectorv2: failed to get repository: %w", err)
@@ -370,18 +349,11 @@ func (o *repositoryResourceType) Revoke(ctx context.Context, grant *v2.Grant) (a
370349
return nil, err
371350
}
372351

373-
cli := o.client
374-
if len(o.app.appInstallationClient) > 0 {
375-
i, err := strconv.ParseInt(grant.GetEntitlement().GetResource().GetParentResourceId().GetResource(), 10, 64)
376-
if err != nil {
377-
return nil, err
378-
}
379-
var ok bool
380-
cli, ok = o.app.appInstallationClient[i]
381-
if !ok {
382-
return nil, fmt.Errorf("organization: %d doesn't exist", i)
383-
}
352+
cli, err := getClient(o.client, o.app, grant.GetEntitlement().GetResource().GetParentResourceId().GetResource())
353+
if err != nil {
354+
return nil, err
384355
}
356+
385357
repo, _, err := cli.Repositories.GetByID(ctx, repoID)
386358
if err != nil {
387359
return nil, fmt.Errorf("github-connectorv2: failed to get repository: %w", err)

pkg/connector/repository_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ func TestRepository(t *testing.T) {
2424

2525
githubClient := github.NewClient(mgh.Server())
2626
cache := newOrgNameCache(githubClient, nil)
27-
client := repositoryBuilder(githubClient, cache)
27+
client := repositoryBuilder(githubClient, gitHubApp{}, cache)
2828

2929
organization, _ := organizationResource(ctx, githubOrganization, nil)
3030
repository, _ := repositoryResource(ctx, githubRepository, organization.Id)

pkg/connector/team.go

Lines changed: 14 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -94,18 +94,11 @@ func (o *teamResourceType) List(ctx context.Context, parentID *v2.ResourceId, pt
9494
return nil, "", nil, err
9595
}
9696

97-
cli := o.client
98-
if len(o.app.appInstallationClient) > 0 {
99-
i, err := strconv.ParseInt(parentID.GetResource(), 10, 64)
100-
if err != nil {
101-
return nil, "", nil, err
102-
}
103-
var ok bool
104-
cli, ok = o.app.appInstallationClient[i]
105-
if !ok {
106-
return nil, "", nil, fmt.Errorf("organization: %d doesn't exist", i)
107-
}
97+
cli, err := getClient(o.client, o.app, parentID.GetResource())
98+
if err != nil {
99+
return nil, "", nil, err
108100
}
101+
109102
teams, resp, err := cli.Teams.ListTeams(ctx, orgName, opts)
110103
if err != nil {
111104
return nil, "", nil, fmt.Errorf("github-connector: failed to list teams: %w", err)
@@ -180,17 +173,9 @@ func (o *teamResourceType) Grants(ctx context.Context, resource *v2.Resource, pT
180173
return nil, "", nil, fmt.Errorf("error fetching orgID from team profile")
181174
}
182175

183-
cli := o.client
184-
if len(o.app.appInstallationClient) > 0 {
185-
i, err := strconv.ParseInt(resource.GetParentResourceId().GetResource(), 10, 64)
186-
if err != nil {
187-
return nil, "", nil, err
188-
}
189-
var ok bool
190-
cli, ok = o.app.appInstallationClient[i]
191-
if !ok {
192-
return nil, "", nil, fmt.Errorf("organization: %d doesn't exist", i)
193-
}
176+
cli, err := getClient(o.client, o.app, resource.GetParentResourceId().GetResource())
177+
if err != nil {
178+
return nil, "", nil, err
194179
}
195180

196181
org, _, err := cli.Organizations.GetByID(ctx, orgID)
@@ -299,17 +284,9 @@ func (o *teamResourceType) Grant(ctx context.Context, principal *v2.Resource, en
299284
return nil, err
300285
}
301286

302-
cli := o.client
303-
if len(o.app.appInstallationClient) > 0 {
304-
i, err := strconv.ParseInt(entitlement.Resource.ParentResourceId.GetResource(), 10, 64)
305-
if err != nil {
306-
return nil, err
307-
}
308-
var ok bool
309-
cli, ok = o.app.appInstallationClient[i]
310-
if !ok {
311-
return nil, fmt.Errorf("organization: %d doesn't exist", i)
312-
}
287+
cli, err := getClient(o.client, o.app, entitlement.Resource.ParentResourceId.GetResource())
288+
if err != nil {
289+
return nil, err
313290
}
314291

315292
user, _, err := cli.Users.GetByID(ctx, userId)
@@ -372,18 +349,11 @@ func (o *teamResourceType) Revoke(ctx context.Context, grant *v2.Grant) (annotat
372349
return nil, err
373350
}
374351

375-
cli := o.client
376-
if len(o.app.appInstallationClient) > 0 {
377-
i, err := strconv.ParseInt(grant.GetEntitlement().GetResource().GetParentResourceId().GetResource(), 10, 64)
378-
if err != nil {
379-
return nil, err
380-
}
381-
var ok bool
382-
cli, ok = o.app.appInstallationClient[i]
383-
if !ok {
384-
return nil, fmt.Errorf("organization: %d doesn't exist", i)
385-
}
352+
cli, err := getClient(o.client, o.app, grant.GetEntitlement().GetResource().GetParentResourceId().GetResource())
353+
if err != nil {
354+
return nil, err
386355
}
356+
387357
user, _, err := cli.Users.GetByID(ctx, userId)
388358
if err != nil {
389359
return nil, fmt.Errorf("github-connectorv2: failed to get user %d, err: %w", userId, err)

pkg/connector/team_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ func TestTeam(t *testing.T) {
2424

2525
githubClient := github.NewClient(mgh.Server())
2626
cache := newOrgNameCache(githubClient, nil)
27-
client := teamBuilder(githubClient, cache)
27+
client := teamBuilder(githubClient, gitHubApp{}, cache)
2828

2929
organization, _ := organizationResource(ctx, githubOrganization, nil)
3030
team, _ := teamResource(githubTeam, organization.Id)

pkg/connector/user.go

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -127,18 +127,11 @@ func (o *userResourceType) List(ctx context.Context, parentID *v2.ResourceId, pt
127127
ListOptions: github.ListOptions{Page: page, PerPage: pt.Size},
128128
}
129129

130-
cli := o.client
131-
if len(o.app.appInstallationClient) > 0 {
132-
i, err := strconv.ParseInt(parentID.GetResource(), 10, 64)
133-
if err != nil {
134-
return nil, "", nil, err
135-
}
136-
var ok bool
137-
cli, ok = o.app.appInstallationClient[i]
138-
if !ok {
139-
return nil, "", nil, fmt.Errorf("organization: %d doesn't exist", i)
140-
}
130+
cli, err := getClient(o.client, o.app, parentID.GetResource())
131+
if err != nil {
132+
return nil, "", nil, err
141133
}
134+
142135
users, resp, err := cli.Organizations.ListMembers(ctx, orgName, &opts)
143136
if err != nil {
144137
return nil, "", nil, fmt.Errorf("github-connector: ListMembers failed: %w", err)

pkg/connector/user_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ func TestUsersList(t *testing.T) {
4545
cache := newOrgNameCache(githubClient, nil)
4646
client := userBuilder(
4747
githubClient,
48+
gitHubApp{},
4849
testCase.hasSamlEnabled,
4950
graphQLClient,
5051
cache,

0 commit comments

Comments
 (0)