Skip to content

Commit 92d25e3

Browse files
authored
Cache org names to save on requests made (#24)
1 parent 5745a7d commit 92d25e3

File tree

6 files changed

+57
-17
lines changed

6 files changed

+57
-17
lines changed

pkg/connector/connector.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,15 @@ type Github struct {
5656
instanceURL string
5757
graphqlClient *githubv4.Client
5858
hasSAMLEnabled *bool
59+
orgCache *orgNameCache
5960
}
6061

6162
func (gh *Github) ResourceSyncers(ctx context.Context) []connectorbuilder.ResourceSyncer {
6263
return []connectorbuilder.ResourceSyncer{
63-
orgBuilder(gh.client, gh.orgs),
64-
teamBuilder(gh.client),
65-
userBuilder(gh.client, gh.hasSAMLEnabled, gh.graphqlClient),
66-
repositoryBuilder(gh.client),
64+
orgBuilder(gh.client, gh.orgCache, gh.orgs),
65+
teamBuilder(gh.client, gh.orgCache),
66+
userBuilder(gh.client, gh.hasSAMLEnabled, gh.graphqlClient, gh.orgCache),
67+
repositoryBuilder(gh.client, gh.orgCache),
6768
}
6869
}
6970

@@ -168,6 +169,7 @@ func New(ctx context.Context, githubOrgs []string, instanceURL, accessToken stri
168169
instanceURL: instanceURL,
169170
orgs: githubOrgs,
170171
graphqlClient: graphqlClient,
172+
orgCache: newOrgNameCache(client),
171173
}
172174

173175
return gh, nil

pkg/connector/helpers.go

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66
"strconv"
77
"strings"
8+
"sync"
89

910
v2 "github.com/conductorone/baton-sdk/pb/c1/connector/v2"
1011
"github.com/conductorone/baton-sdk/pkg/annotations"
@@ -22,20 +23,49 @@ func titleCase(s string) string {
2223
return titleCaser.String(s)
2324
}
2425

25-
func getOrgName(ctx context.Context, c *github.Client, orgID *v2.ResourceId) (string, error) {
26+
type orgNameCache struct {
27+
sync.RWMutex
28+
c *github.Client
29+
orgNames map[string]string
30+
}
31+
32+
func (o *orgNameCache) GetOrgName(ctx context.Context, orgID *v2.ResourceId) (string, error) {
33+
o.RLock()
34+
if orgName, ok := o.orgNames[orgID.Resource]; ok {
35+
o.RUnlock()
36+
return orgName, nil
37+
}
38+
o.RUnlock()
39+
40+
o.Lock()
41+
defer o.Unlock()
42+
43+
if orgName, ok := o.orgNames[orgID.Resource]; ok {
44+
return orgName, nil
45+
}
46+
2647
oID, err := strconv.ParseInt(orgID.Resource, 10, 64)
2748
if err != nil {
2849
return "", err
2950
}
3051

31-
org, _, err := c.Organizations.GetByID(ctx, oID)
52+
org, _, err := o.c.Organizations.GetByID(ctx, oID)
3253
if err != nil {
3354
return "", err
3455
}
3556

57+
o.orgNames[orgID.Resource] = org.GetLogin()
58+
3659
return org.GetLogin(), nil
3760
}
3861

62+
func newOrgNameCache(c *github.Client) *orgNameCache {
63+
return &orgNameCache{
64+
c: c,
65+
orgNames: make(map[string]string),
66+
}
67+
}
68+
3969
func v1AnnotationsForResourceType(resourceTypeID string) annotations.Annotations {
4070
annos := annotations.Annotations{}
4171
annos.Update(&v2.V1Identifier{

pkg/connector/org.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ type orgResourceType struct {
3232
resourceType *v2.ResourceType
3333
client *github.Client
3434
orgs map[string]struct{}
35+
orgCache *orgNameCache
3536
}
3637

3738
func (o *orgResourceType) ResourceType(_ context.Context) *v2.ResourceType {
@@ -161,7 +162,7 @@ func (o *orgResourceType) Grants(
161162
},
162163
}
163164

164-
orgName, err := getOrgName(ctx, o.client, resource.Id)
165+
orgName, err := o.orgCache.GetOrgName(ctx, resource.Id)
165166
if err != nil {
166167
return nil, "", nil, err
167168
}
@@ -232,7 +233,7 @@ func (o *orgResourceType) Grant(ctx context.Context, principal *v2.Resource, en
232233
return nil, fmt.Errorf("github-connectorv2: invalid entitlement id: %s", en.Id)
233234
}
234235

235-
orgName, err := getOrgName(ctx, o.client, en.Resource.Id)
236+
orgName, err := o.orgCache.GetOrgName(ctx, en.Resource.Id)
236237
if err != nil {
237238
return nil, err
238239
}
@@ -288,7 +289,7 @@ func (o *orgResourceType) Revoke(ctx context.Context, grant *v2.Grant) (annotati
288289
return nil, fmt.Errorf("github-connectorv2: invalid entitlement id: %s", en.Id)
289290
}
290291

291-
orgName, err := getOrgName(ctx, o.client, en.Resource.Id)
292+
orgName, err := o.orgCache.GetOrgName(ctx, en.Resource.Id)
292293
if err != nil {
293294
return nil, err
294295
}
@@ -325,7 +326,7 @@ func (o *orgResourceType) Revoke(ctx context.Context, grant *v2.Grant) (annotati
325326
return nil, nil
326327
}
327328

328-
func orgBuilder(client *github.Client, orgs []string) *orgResourceType {
329+
func orgBuilder(client *github.Client, orgCache *orgNameCache, orgs []string) *orgResourceType {
329330
orgMap := make(map[string]struct{})
330331

331332
for _, o := range orgs {
@@ -336,5 +337,6 @@ func orgBuilder(client *github.Client, orgs []string) *orgResourceType {
336337
resourceType: resourceTypeOrg,
337338
orgs: orgMap,
338339
client: client,
340+
orgCache: orgCache,
339341
}
340342
}

pkg/connector/repository.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ func repositoryResource(ctx context.Context, repo *github.Repository, parentReso
5555
type repositoryResourceType struct {
5656
resourceType *v2.ResourceType
5757
client *github.Client
58+
orgCache *orgNameCache
5859
}
5960

6061
func (o *repositoryResourceType) ResourceType(_ context.Context) *v2.ResourceType {
@@ -71,7 +72,7 @@ func (o *repositoryResourceType) List(ctx context.Context, parentID *v2.Resource
7172
return nil, "", nil, err
7273
}
7374

74-
orgName, err := getOrgName(ctx, o.client, parentID)
75+
orgName, err := o.orgCache.GetOrgName(ctx, parentID)
7576
if err != nil {
7677
return nil, "", nil, err
7778
}
@@ -136,7 +137,7 @@ func (o *repositoryResourceType) Grants(
136137
return nil, "", nil, err
137138
}
138139

139-
orgName, err := getOrgName(ctx, o.client, resource.ParentResourceId)
140+
orgName, err := o.orgCache.GetOrgName(ctx, resource.ParentResourceId)
140141
if err != nil {
141142
return nil, "", nil, err
142143
}
@@ -355,9 +356,10 @@ func (o *repositoryResourceType) Revoke(ctx context.Context, grant *v2.Grant) (a
355356
return nil, nil
356357
}
357358

358-
func repositoryBuilder(client *github.Client) *repositoryResourceType {
359+
func repositoryBuilder(client *github.Client, orgCache *orgNameCache) *repositoryResourceType {
359360
return &repositoryResourceType{
360361
resourceType: resourceTypeRepository,
361362
client: client,
363+
orgCache: orgCache,
362364
}
363365
}

pkg/connector/team.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ func teamResource(team *github.Team, parentResourceID *v2.ResourceId) (*v2.Resou
5656
type teamResourceType struct {
5757
resourceType *v2.ResourceType
5858
client *github.Client
59+
orgCache *orgNameCache
5960
}
6061

6162
func (o *teamResourceType) ResourceType(_ context.Context) *v2.ResourceType {
@@ -90,7 +91,7 @@ func (o *teamResourceType) List(ctx context.Context, parentID *v2.ResourceId, pt
9091
// No resource ID set, so just list teams and push an action for each that we see
9192
case "":
9293
pageState := bag.Pop()
93-
orgName, err := getOrgName(ctx, o.client, parentID)
94+
orgName, err := o.orgCache.GetOrgName(ctx, parentID)
9495
if err != nil {
9596
return nil, "", nil, err
9697
}
@@ -342,9 +343,10 @@ func (o *teamResourceType) Revoke(ctx context.Context, grant *v2.Grant) (annotat
342343
return nil, nil
343344
}
344345

345-
func teamBuilder(client *github.Client) *teamResourceType {
346+
func teamBuilder(client *github.Client, orgCache *orgNameCache) *teamResourceType {
346347
return &teamResourceType{
347348
resourceType: resourceTypeTeam,
348349
client: client,
350+
orgCache: orgCache,
349351
}
350352
}

pkg/connector/user.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ type userResourceType struct {
6767
client *github.Client
6868
graphqlClient *githubv4.Client
6969
hasSAMLEnabled *bool
70+
orgCache *orgNameCache
7071
}
7172

7273
func (o *userResourceType) ResourceType(_ context.Context) *v2.ResourceType {
@@ -84,7 +85,7 @@ func (o *userResourceType) List(ctx context.Context, parentID *v2.ResourceId, pt
8485
return nil, "", nil, err
8586
}
8687

87-
orgName, err := getOrgName(ctx, o.client, parentID)
88+
orgName, err := o.orgCache.GetOrgName(ctx, parentID)
8889
if err != nil {
8990
return nil, "", nil, err
9091
}
@@ -169,12 +170,13 @@ func (o *userResourceType) Grants(_ context.Context, _ *v2.Resource, _ *paginati
169170
return nil, "", nil, nil
170171
}
171172

172-
func userBuilder(client *github.Client, hasSAMLEnabled *bool, graphqlClient *githubv4.Client) *userResourceType {
173+
func userBuilder(client *github.Client, hasSAMLEnabled *bool, graphqlClient *githubv4.Client, orgCache *orgNameCache) *userResourceType {
173174
return &userResourceType{
174175
resourceType: resourceTypeUser,
175176
client: client,
176177
graphqlClient: graphqlClient,
177178
hasSAMLEnabled: hasSAMLEnabled,
179+
orgCache: orgCache,
178180
}
179181
}
180182

0 commit comments

Comments
 (0)