Skip to content

Commit 52c185c

Browse files
authored
Allow to filter out archived repositories in the github enterprise connector (#109)
* Allow to filter out archived repositories in the github enterprise connector Add a configuration option to do this for the GitHub connector by filtering out repositories returned (using the 'archived' attribute) - there is no API-level filter for this. This works for both the GitHub Enterprise and GitHub v2 connectors. Review comments addressed and squashed.
1 parent feac0ce commit 52c185c

File tree

5 files changed

+51
-37
lines changed

5 files changed

+51
-37
lines changed

pkg/config/conf.gen.go

Lines changed: 10 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/config/config.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@ var (
4343
field.WithDisplayName("Sync secrets"),
4444
field.WithDescription(`Whether to sync secrets or not`),
4545
)
46+
omitArchivedRepositories = field.BoolField(
47+
"omit-archived-repositories",
48+
field.WithDisplayName("Omit syncing archived repositories"),
49+
field.WithDescription("Whether to skip syncing archived repositories or not"),
50+
)
4651
fieldRelationships = []field.SchemaFieldRelationship{
4752
field.FieldsMutuallyExclusive(
4853
accessTokenField,
@@ -63,6 +68,7 @@ var Config = field.NewConfiguration(
6368
EnterprisesField,
6469
instanceUrlField,
6570
syncSecrets,
71+
omitArchivedRepositories,
6672
appIDField,
6773
appPrivateKeyPath,
6874
},

pkg/connector/connector.go

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -89,24 +89,25 @@ var (
8989
)
9090

9191
type GitHub struct {
92-
orgs []string
93-
client *github.Client
94-
appClient *github.Client
95-
customClient *customclient.Client
96-
instanceURL string
97-
graphqlClient *githubv4.Client
98-
hasSAMLEnabled *bool
99-
orgCache *orgNameCache
100-
syncSecrets bool
101-
enterprises []string
92+
orgs []string
93+
client *github.Client
94+
appClient *github.Client
95+
customClient *customclient.Client
96+
instanceURL string
97+
graphqlClient *githubv4.Client
98+
hasSAMLEnabled *bool
99+
orgCache *orgNameCache
100+
syncSecrets bool
101+
omitArchivedRepositories bool
102+
enterprises []string
102103
}
103104

104105
func (gh *GitHub) ResourceSyncers(ctx context.Context) []connectorbuilder.ResourceSyncer {
105106
resourceSyncers := []connectorbuilder.ResourceSyncer{
106107
orgBuilder(gh.client, gh.appClient, gh.orgCache, gh.orgs, gh.syncSecrets),
107108
teamBuilder(gh.client, gh.orgCache),
108109
userBuilder(gh.client, gh.hasSAMLEnabled, gh.graphqlClient, gh.orgCache, gh.orgs),
109-
repositoryBuilder(gh.client, gh.orgCache),
110+
repositoryBuilder(gh.client, gh.orgCache, gh.omitArchivedRepositories),
110111
orgRoleBuilder(gh.client, gh.orgCache),
111112
invitationBuilder(invitationBuilderParams{
112113
client: gh.client,
@@ -306,15 +307,16 @@ func New(ctx context.Context, ghc *cfg.Github, appKey string) (*GitHub, error) {
306307
}
307308

308309
gh := &GitHub{
309-
client: ghClient,
310-
appClient: appClient,
311-
customClient: customclient.New(ghClient),
312-
instanceURL: ghc.InstanceUrl,
313-
orgs: ghc.Orgs,
314-
enterprises: ghc.Enterprises,
315-
graphqlClient: graphqlClient,
316-
orgCache: newOrgNameCache(ghClient),
317-
syncSecrets: ghc.SyncSecrets,
310+
client: ghClient,
311+
appClient: appClient,
312+
customClient: customclient.New(ghClient),
313+
instanceURL: ghc.InstanceUrl,
314+
orgs: ghc.Orgs,
315+
enterprises: ghc.Enterprises,
316+
graphqlClient: graphqlClient,
317+
orgCache: newOrgNameCache(ghClient),
318+
syncSecrets: ghc.SyncSecrets,
319+
omitArchivedRepositories: ghc.OmitArchivedRepositories,
318320
}
319321
return gh, nil
320322
}

pkg/connector/repository.go

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,10 @@ func repositoryResource(ctx context.Context, repo *github.Repository, parentReso
5757
}
5858

5959
type repositoryResourceType struct {
60-
resourceType *v2.ResourceType
61-
client *github.Client
62-
orgCache *orgNameCache
60+
resourceType *v2.ResourceType
61+
client *github.Client
62+
orgCache *orgNameCache
63+
omitArchivedRepositories bool
6364
}
6465

6566
func (o *repositoryResourceType) ResourceType(_ context.Context) *v2.ResourceType {
@@ -105,6 +106,9 @@ func (o *repositoryResourceType) List(ctx context.Context, parentID *v2.Resource
105106

106107
rv := make([]*v2.Resource, 0, len(repos))
107108
for _, repo := range repos {
109+
if o.omitArchivedRepositories && repo.GetArchived() {
110+
continue
111+
}
108112
rr, err := repositoryResource(ctx, repo, parentID)
109113
if err != nil {
110114
return nil, "", nil, err
@@ -409,11 +413,12 @@ func (o *repositoryResourceType) Revoke(ctx context.Context, grant *v2.Grant) (a
409413
return nil, nil
410414
}
411415

412-
func repositoryBuilder(client *github.Client, orgCache *orgNameCache) *repositoryResourceType {
416+
func repositoryBuilder(client *github.Client, orgCache *orgNameCache, omitArchivedRepositories bool) *repositoryResourceType {
413417
return &repositoryResourceType{
414-
resourceType: resourceTypeRepository,
415-
client: client,
416-
orgCache: orgCache,
418+
resourceType: resourceTypeRepository,
419+
client: client,
420+
orgCache: orgCache,
421+
omitArchivedRepositories: omitArchivedRepositories,
417422
}
418423
}
419424

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)
27-
client := repositoryBuilder(githubClient, cache)
27+
client := repositoryBuilder(githubClient, cache, false)
2828

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

0 commit comments

Comments
 (0)