feat: allow other hosts to be used for integration config

Signed-off-by: blam <ben@blam.sh>
This commit is contained in:
blam
2022-08-29 13:55:12 +02:00
parent 0c42005843
commit ff72b79193
4 changed files with 61 additions and 16 deletions
@@ -75,6 +75,29 @@ describe('GitHubEntityProvider', () => {
expect(providers[0].getProviderName()).toEqual('github-provider:default');
});
it('throws when the integration config does not exist', () => {
const schedule = new PersistingTaskRunner();
const config = new ConfigReader({
catalog: {
providers: {
github: {
organization: 'test-org',
host: 'ghe.internal.com',
},
},
},
});
expect(() =>
GitHubEntityProvider.fromConfig(config, {
logger,
schedule,
}),
).toThrowError(
/There is no GitHub config that matches host ghe.internal.com/,
);
});
it('multiple provider configs', () => {
const schedule = new PersistingTaskRunner();
const config = new ConfigReader({
@@ -63,23 +63,24 @@ export class GitHubEntityProvider implements EntityProvider {
},
): GitHubEntityProvider[] {
const integrations = ScmIntegrations.fromConfig(config);
const integration = integrations.github.byHost('github.com');
if (!integration) {
throw new Error(
`There is no GitHub config that matches github. Please add a configuration entry for it under integrations.github`,
return readProviderConfigs(config).map(providerConfig => {
const integrationHost = providerConfig.host;
const integration = integrations.github.byHost(integrationHost);
if (!integration) {
throw new Error(
`There is no GitHub config that matches host ${integrationHost}. Please add a configuration entry for it under integrations.github`,
);
}
return new GitHubEntityProvider(
providerConfig,
integration,
options.logger,
options.schedule,
);
}
return readProviderConfigs(config).map(
providerConfig =>
new GitHubEntityProvider(
providerConfig,
integration,
options.logger,
options.schedule,
),
);
});
}
private constructor(
@@ -68,17 +68,22 @@ describe('readProviderConfigs', () => {
branch: 'branch-name',
},
},
providerWithHost: {
organization: 'test-org1',
host: 'ghe.internal.com',
},
},
},
},
});
const providerConfigs = readProviderConfigs(config);
expect(providerConfigs).toHaveLength(4);
expect(providerConfigs).toHaveLength(5);
expect(providerConfigs[0]).toEqual({
id: 'providerOrganizationOnly',
organization: 'test-org1',
catalogPath: '/catalog-info.yaml',
host: 'github.com',
filters: {
repository: undefined,
branch: undefined,
@@ -88,6 +93,7 @@ describe('readProviderConfigs', () => {
id: 'providerCustomCatalogPath',
organization: 'test-org2',
catalogPath: 'custom/path/catalog-info.yaml',
host: 'github.com',
filters: {
repository: undefined,
branch: undefined,
@@ -97,6 +103,7 @@ describe('readProviderConfigs', () => {
id: 'providerWithRepositoryFilter',
organization: 'test-org3', // organization
catalogPath: '/catalog-info.yaml', // file
host: 'github.com',
filters: {
repository: /^repository.*filter$/, // repo
branch: undefined, // branch
@@ -106,10 +113,21 @@ describe('readProviderConfigs', () => {
id: 'providerWithBranchFilter',
organization: 'test-org4',
catalogPath: '/catalog-info.yaml',
host: 'github.com',
filters: {
repository: undefined,
branch: 'branch-name',
},
});
expect(providerConfigs[4]).toEqual({
id: 'providerWithHost',
organization: 'test-org1',
catalogPath: '/catalog-info.yaml',
host: 'ghe.internal.com',
filters: {
repository: undefined,
branch: undefined,
},
});
});
});
@@ -23,6 +23,7 @@ export type GitHubEntityProviderConfig = {
id: string;
catalogPath: string;
organization: string;
host: string;
filters?: {
repository?: RegExp;
branch?: string;
@@ -56,6 +57,7 @@ function readProviderConfig(
const organization = config.getString('organization');
const catalogPath =
config.getOptionalString('catalogPath') ?? DEFAULT_CATALOG_PATH;
const host = config.getOptionalString('host') ?? 'github.com';
const repositoryPattern = config.getOptionalString('filters.repository');
const branchPattern = config.getOptionalString('filters.branch');
@@ -63,6 +65,7 @@ function readProviderConfig(
id,
catalogPath,
organization,
host,
filters: {
repository: repositoryPattern
? compileRegExp(repositoryPattern)