From 9d4073f1b6149b7b1450cdb0cd850df7972a4f34 Mon Sep 17 00:00:00 2001 From: Julian Gonzalez Date: Thu, 23 Oct 2025 10:21:47 -0700 Subject: [PATCH 1/2] 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. --- pkg/config/conf.gen.go | 1 + pkg/config/config.go | 6 ++++++ pkg/connector/connector.go | 4 +++- pkg/connector/repository.go | 7 ++++++- pkg/connector/repository_test.go | 2 +- 5 files changed, 17 insertions(+), 3 deletions(-) diff --git a/pkg/config/conf.gen.go b/pkg/config/conf.gen.go index 9a1c2472..d47fa261 100644 --- a/pkg/config/conf.gen.go +++ b/pkg/config/conf.gen.go @@ -9,6 +9,7 @@ type Github struct { Enterprises []string `mapstructure:"enterprises"` InstanceUrl string `mapstructure:"instance-url"` SyncSecrets bool `mapstructure:"sync-secrets"` + OmitArchivedRepositories bool `mapstructure:"omit-archived-repositories"` AppId string `mapstructure:"app-id"` AppPrivatekeyPath string `mapstructure:"app-privatekey-path"` } diff --git a/pkg/config/config.go b/pkg/config/config.go index 50f6eb8c..86ee0ed1 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -43,6 +43,11 @@ var ( field.WithDisplayName("Sync secrets"), field.WithDescription(`Whether to sync secrets or not`), ) + omitArchivedRepositories = field.BoolField( + "omit-archived-repositories", + field.WithDisplayName("Omit syncying archived repositories"), + field.WithDescription("Whether to skip sync archived repositories or not"), + ) fieldRelationships = []field.SchemaFieldRelationship{ field.FieldsMutuallyExclusive( accessTokenField, @@ -63,6 +68,7 @@ var Config = field.NewConfiguration( EnterprisesField, instanceUrlField, syncSecrets, + omitArchivedRepositories, appIDField, appPrivateKeyPath, }, diff --git a/pkg/connector/connector.go b/pkg/connector/connector.go index 0269d214..d50d7654 100644 --- a/pkg/connector/connector.go +++ b/pkg/connector/connector.go @@ -98,6 +98,7 @@ type GitHub struct { hasSAMLEnabled *bool orgCache *orgNameCache syncSecrets bool + omitArchivedRepositories bool enterprises []string } @@ -106,7 +107,7 @@ func (gh *GitHub) ResourceSyncers(ctx context.Context) []connectorbuilder.Resour orgBuilder(gh.client, gh.appClient, gh.orgCache, gh.orgs, gh.syncSecrets), teamBuilder(gh.client, gh.orgCache), userBuilder(gh.client, gh.hasSAMLEnabled, gh.graphqlClient, gh.orgCache, gh.orgs), - repositoryBuilder(gh.client, gh.orgCache), + repositoryBuilder(gh.client, gh.orgCache, gh.omitArchivedRepositories), orgRoleBuilder(gh.client, gh.orgCache), invitationBuilder(invitationBuilderParams{ client: gh.client, @@ -315,6 +316,7 @@ func New(ctx context.Context, ghc *cfg.Github, appKey string) (*GitHub, error) { graphqlClient: graphqlClient, orgCache: newOrgNameCache(ghClient), syncSecrets: ghc.SyncSecrets, + omitArchivedRepositories: ghc.OmitArchivedRepositories, } return gh, nil } diff --git a/pkg/connector/repository.go b/pkg/connector/repository.go index 2e761c88..6cc22aa3 100644 --- a/pkg/connector/repository.go +++ b/pkg/connector/repository.go @@ -60,6 +60,7 @@ type repositoryResourceType struct { resourceType *v2.ResourceType client *github.Client orgCache *orgNameCache + omitArchivedRepositories bool } func (o *repositoryResourceType) ResourceType(_ context.Context) *v2.ResourceType { @@ -105,6 +106,9 @@ func (o *repositoryResourceType) List(ctx context.Context, parentID *v2.Resource rv := make([]*v2.Resource, 0, len(repos)) for _, repo := range repos { + if o.omitArchivedRepositories && repo.GetArchived() { + continue + } rr, err := repositoryResource(ctx, repo, parentID) if err != nil { return nil, "", nil, err @@ -409,11 +413,12 @@ func (o *repositoryResourceType) Revoke(ctx context.Context, grant *v2.Grant) (a return nil, nil } -func repositoryBuilder(client *github.Client, orgCache *orgNameCache) *repositoryResourceType { +func repositoryBuilder(client *github.Client, orgCache *orgNameCache, omitArchivedRepositories bool) *repositoryResourceType { return &repositoryResourceType{ resourceType: resourceTypeRepository, client: client, orgCache: orgCache, + omitArchivedRepositories: omitArchivedRepositories, } } diff --git a/pkg/connector/repository_test.go b/pkg/connector/repository_test.go index b6770bac..5c6ab7bc 100644 --- a/pkg/connector/repository_test.go +++ b/pkg/connector/repository_test.go @@ -24,7 +24,7 @@ func TestRepository(t *testing.T) { githubClient := github.NewClient(mgh.Server()) cache := newOrgNameCache(githubClient) - client := repositoryBuilder(githubClient, cache) + client := repositoryBuilder(githubClient, cache, false) organization, _ := organizationResource(ctx, githubOrganization, nil, false) repository, _ := repositoryResource(ctx, githubRepository, organization.Id) From ce0e1bf11e5f802c0caf9a13908cbbe08ac22327 Mon Sep 17 00:00:00 2001 From: Julian Gonzalez Date: Thu, 23 Oct 2025 13:30:48 -0700 Subject: [PATCH 2/2] Fix linter issues Now we know that it exists :) Also fix some grammar/spelling --- pkg/config/conf.gen.go | 20 +++++++++---------- pkg/config/config.go | 4 ++-- pkg/connector/connector.go | 38 ++++++++++++++++++------------------- pkg/connector/repository.go | 12 ++++++------ 4 files changed, 37 insertions(+), 37 deletions(-) diff --git a/pkg/config/conf.gen.go b/pkg/config/conf.gen.go index d47fa261..d6d0b78c 100644 --- a/pkg/config/conf.gen.go +++ b/pkg/config/conf.gen.go @@ -1,20 +1,20 @@ // Code generated by baton-sdk. DO NOT EDIT!!! package config -import "reflect" +import "reflect" type Github struct { - Token string `mapstructure:"token"` - Orgs []string `mapstructure:"orgs"` - Enterprises []string `mapstructure:"enterprises"` - InstanceUrl string `mapstructure:"instance-url"` - SyncSecrets bool `mapstructure:"sync-secrets"` - OmitArchivedRepositories bool `mapstructure:"omit-archived-repositories"` - AppId string `mapstructure:"app-id"` - AppPrivatekeyPath string `mapstructure:"app-privatekey-path"` + Token string `mapstructure:"token"` + Orgs []string `mapstructure:"orgs"` + Enterprises []string `mapstructure:"enterprises"` + InstanceUrl string `mapstructure:"instance-url"` + SyncSecrets bool `mapstructure:"sync-secrets"` + OmitArchivedRepositories bool `mapstructure:"omit-archived-repositories"` + AppId string `mapstructure:"app-id"` + AppPrivatekeyPath string `mapstructure:"app-privatekey-path"` } -func (c* Github) findFieldByTag(tagValue string) (any, bool) { +func (c *Github) findFieldByTag(tagValue string) (any, bool) { v := reflect.ValueOf(c).Elem() // Dereference pointer to struct t := v.Type() diff --git a/pkg/config/config.go b/pkg/config/config.go index 86ee0ed1..2e4dd814 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -45,8 +45,8 @@ var ( ) omitArchivedRepositories = field.BoolField( "omit-archived-repositories", - field.WithDisplayName("Omit syncying archived repositories"), - field.WithDescription("Whether to skip sync archived repositories or not"), + field.WithDisplayName("Omit syncing archived repositories"), + field.WithDescription("Whether to skip syncing archived repositories or not"), ) fieldRelationships = []field.SchemaFieldRelationship{ field.FieldsMutuallyExclusive( diff --git a/pkg/connector/connector.go b/pkg/connector/connector.go index d50d7654..d4c9808a 100644 --- a/pkg/connector/connector.go +++ b/pkg/connector/connector.go @@ -89,17 +89,17 @@ var ( ) type GitHub struct { - orgs []string - client *github.Client - appClient *github.Client - customClient *customclient.Client - instanceURL string - graphqlClient *githubv4.Client - hasSAMLEnabled *bool - orgCache *orgNameCache - syncSecrets bool + orgs []string + client *github.Client + appClient *github.Client + customClient *customclient.Client + instanceURL string + graphqlClient *githubv4.Client + hasSAMLEnabled *bool + orgCache *orgNameCache + syncSecrets bool omitArchivedRepositories bool - enterprises []string + enterprises []string } func (gh *GitHub) ResourceSyncers(ctx context.Context) []connectorbuilder.ResourceSyncer { @@ -307,15 +307,15 @@ func New(ctx context.Context, ghc *cfg.Github, appKey string) (*GitHub, error) { } gh := &GitHub{ - client: ghClient, - appClient: appClient, - customClient: customclient.New(ghClient), - instanceURL: ghc.InstanceUrl, - orgs: ghc.Orgs, - enterprises: ghc.Enterprises, - graphqlClient: graphqlClient, - orgCache: newOrgNameCache(ghClient), - syncSecrets: ghc.SyncSecrets, + client: ghClient, + appClient: appClient, + customClient: customclient.New(ghClient), + instanceURL: ghc.InstanceUrl, + orgs: ghc.Orgs, + enterprises: ghc.Enterprises, + graphqlClient: graphqlClient, + orgCache: newOrgNameCache(ghClient), + syncSecrets: ghc.SyncSecrets, omitArchivedRepositories: ghc.OmitArchivedRepositories, } return gh, nil diff --git a/pkg/connector/repository.go b/pkg/connector/repository.go index 6cc22aa3..550acd4d 100644 --- a/pkg/connector/repository.go +++ b/pkg/connector/repository.go @@ -57,9 +57,9 @@ func repositoryResource(ctx context.Context, repo *github.Repository, parentReso } type repositoryResourceType struct { - resourceType *v2.ResourceType - client *github.Client - orgCache *orgNameCache + resourceType *v2.ResourceType + client *github.Client + orgCache *orgNameCache omitArchivedRepositories bool } @@ -415,9 +415,9 @@ func (o *repositoryResourceType) Revoke(ctx context.Context, grant *v2.Grant) (a func repositoryBuilder(client *github.Client, orgCache *orgNameCache, omitArchivedRepositories bool) *repositoryResourceType { return &repositoryResourceType{ - resourceType: resourceTypeRepository, - client: client, - orgCache: orgCache, + resourceType: resourceTypeRepository, + client: client, + orgCache: orgCache, omitArchivedRepositories: omitArchivedRepositories, } }