From 1416e69de9f73601094bf0a84631036f7ee04a46 Mon Sep 17 00:00:00 2001 From: Nikolas Skoufis Date: Mon, 10 Oct 2022 16:32:19 +1100 Subject: [PATCH] Add a config field to configure entity validation This adds a new config field that can be used to configure entity validation. It defaults to false to ensure backwards compatibility. It is not permitted when catalog paths contain wildcards, due to limitations with how the GraphQL query will work. Signed-off-by: Nikolas Skoufis --- .../GitHubEntityProviderConfig.test.ts | 37 +++++++++++++++++++ .../providers/GitHubEntityProviderConfig.ts | 12 ++++++ 2 files changed, 49 insertions(+) diff --git a/plugins/catalog-backend-module-github/src/providers/GitHubEntityProviderConfig.test.ts b/plugins/catalog-backend-module-github/src/providers/GitHubEntityProviderConfig.test.ts index 66a5463bf5..16f9d96010 100644 --- a/plugins/catalog-backend-module-github/src/providers/GitHubEntityProviderConfig.test.ts +++ b/plugins/catalog-backend-module-github/src/providers/GitHubEntityProviderConfig.test.ts @@ -101,6 +101,7 @@ describe('readProviderConfigs', () => { exclude: undefined, }, }, + validateLocationsExist: false, }); expect(providerConfigs[1]).toEqual({ id: 'providerCustomCatalogPath', @@ -115,6 +116,7 @@ describe('readProviderConfigs', () => { exclude: undefined, }, }, + validateLocationsExist: false, }); expect(providerConfigs[2]).toEqual({ id: 'providerWithRepositoryFilter', @@ -129,6 +131,7 @@ describe('readProviderConfigs', () => { exclude: undefined, }, }, + validateLocationsExist: false, }); expect(providerConfigs[3]).toEqual({ id: 'providerWithBranchFilter', @@ -143,6 +146,7 @@ describe('readProviderConfigs', () => { exclude: undefined, }, }, + validateLocationsExist: false, }); expect(providerConfigs[4]).toEqual({ id: 'providerWithTopicFilter', @@ -157,6 +161,7 @@ describe('readProviderConfigs', () => { exclude: ['backstage-exclude'], }, }, + validateLocationsExist: false, }); expect(providerConfigs[5]).toEqual({ id: 'providerWithHost', @@ -171,6 +176,38 @@ describe('readProviderConfigs', () => { exclude: undefined, }, }, + validateLocationsExist: false, }); }); + + it('defaults validateLocationsExist to false', () => { + const config = new ConfigReader({ + catalog: { + providers: { + github: { + organization: 'test-org', + }, + }, + }, + }); + const providerConfigs = readProviderConfigs(config); + + expect(providerConfigs[0].validateLocationsExist).toEqual(false); + }); + + it('throws an error when a wildcard catalog path is configured with validation of locations', () => { + const config = new ConfigReader({ + catalog: { + providers: { + github: { + organization: 'test-org', + validateLocationsExist: true, + catalogPath: '/*/catalog-info.yaml', + }, + }, + }, + }); + + expect(() => readProviderConfigs(config)).toThrow(); + }); }); diff --git a/plugins/catalog-backend-module-github/src/providers/GitHubEntityProviderConfig.ts b/plugins/catalog-backend-module-github/src/providers/GitHubEntityProviderConfig.ts index d40b206751..fd5482d1b6 100644 --- a/plugins/catalog-backend-module-github/src/providers/GitHubEntityProviderConfig.ts +++ b/plugins/catalog-backend-module-github/src/providers/GitHubEntityProviderConfig.ts @@ -29,6 +29,7 @@ export type GitHubEntityProviderConfig = { branch?: string; topic?: GithubTopicFilters; }; + validateLocationsExist: boolean; }; export type GithubTopicFilters = { @@ -72,6 +73,16 @@ function readProviderConfig( const topicFilterExclude = config?.getOptionalStringArray( 'filters.topic.exclude', ); + const validateLocationsExist = + config?.getOptionalBoolean('validateLocationsExist') ?? false; + + const catalogPathContainsWildcard = catalogPath.includes('*'); + + if (validateLocationsExist && catalogPathContainsWildcard) { + throw Error( + `Error while processing GitHub provider config. The catalog path ${catalogPath} contains a wildcard, which is incompatible with validation of locations existing before emitting them. Ensure that validateLocationsExist is set to false.`, + ); + } return { id, @@ -88,6 +99,7 @@ function readProviderConfig( exclude: topicFilterExclude, }, }, + validateLocationsExist, }; } /**