chore: migrate the rest of the scaffolding to the new way for instegration config

This commit is contained in:
blam
2021-01-19 19:04:06 +01:00
parent 8b66c474e3
commit c0a7910db5
13 changed files with 28 additions and 129 deletions
@@ -13,15 +13,11 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import {
makeDeprecatedLocationTypeDetector,
parseLocationAnnotation,
} from './helpers';
import { parseLocationAnnotation } from './helpers';
import {
TemplateEntityV1alpha1,
LOCATION_ANNOTATION,
} from '@backstage/catalog-model';
import { ConfigReader } from '@backstage/config';
describe('Helpers', () => {
describe('parseLocationAnnotation', () => {
@@ -254,29 +250,4 @@ describe('Helpers', () => {
});
});
});
describe('makeDeprecatedLocationTypeDetector', () => {
it('detects deprecated location types', () => {
const detector = makeDeprecatedLocationTypeDetector(
new ConfigReader({
integrations: {
github: [{ host: 'derp.com' }, { host: 'foo.com' }],
gitlab: [{ host: 'derp.org' }, { host: 'foo.org' }],
azure: [{ host: 'derp.net' }, { host: 'foo.net' }],
},
}),
);
expect(detector('http://lol:wut@derp.com/wat')).toBe('github');
expect(detector('https://foo.com/wat')).toBe('github');
expect(detector('http://derp.org:80/wat')).toBe('gitlab');
expect(detector('https://foo.org/wat')).toBe('gitlab');
expect(detector('http://not.derp.net')).toBe(undefined);
expect(detector('http://derp.net')).toBe('azure');
expect(detector('http://derp.net:8080/wat')).toBe('azure');
expect(detector('http://github.com')).toBe('github');
expect(detector('http://gitlab.com')).toBe('gitlab');
expect(detector('http://dev.azure.com')).toBe('azure');
});
});
});
@@ -17,12 +17,10 @@ import {
TemplateEntityV1alpha1,
LOCATION_ANNOTATION,
} from '@backstage/catalog-model';
import { Config } from '@backstage/config';
import { InputError } from '@backstage/backend-common';
import { RemoteProtocol } from './types';
export type ParsedLocationAnnotation = {
protocol: RemoteProtocol;
protocol: 'file' | 'url';
location: string;
};
@@ -40,7 +38,7 @@ export const parseLocationAnnotation = (
// split on the first colon for the protocol and the rest after the first split
// is the location.
const [protocol, location] = annotation.split(/:(.+)/) as [
RemoteProtocol?,
('file' | 'url')?,
string?,
];
@@ -55,42 +53,3 @@ export const parseLocationAnnotation = (
location,
};
};
export type DeprecatedLocationTypeDetector = (
url: string,
) => string | undefined;
// The reason for the existence of this is to help in migration to using mostly locations
// of type 'url'. This allows us to detect the deprecated location type based on the host,
// which we in turn can use to select out preparer or publisher.
//
// TODO(Rugvip): This should be removed in the future once we fully migrate to using
// integrations configuration for the scaffolder.
export function makeDeprecatedLocationTypeDetector(
config: Config,
): DeprecatedLocationTypeDetector {
const hostMap = new Map();
// These are installed by default by the integrations
hostMap.set('github.com', 'github');
hostMap.set('gitlab.com', 'gitlab');
hostMap.set('dev.azure.com', 'azure');
hostMap.set('bitbucket.org', 'bitbucket');
config.getOptionalConfigArray('integrations.github')?.forEach(sub => {
hostMap.set(sub.getString('host'), 'github');
});
config.getOptionalConfigArray('integrations.gitlab')?.forEach(sub => {
hostMap.set(sub.getString('host'), 'gitlab');
});
config.getOptionalConfigArray('integrations.azure')?.forEach(sub => {
hostMap.set(sub.getString('host'), 'azure');
});
config.getOptionalConfigArray('integrations.bitbucket')?.forEach(sub => {
hostMap.set(sub.getString('host'), 'bitbucket');
});
return (url: string): string | undefined => {
const parsed = new URL(url);
return hostMap.get(parsed.hostname);
};
}
@@ -24,7 +24,6 @@ import {
TemplateEntityV1alpha1,
LOCATION_ANNOTATION,
} from '@backstage/catalog-model';
import { ConfigReader } from '@backstage/config';
import { getVoidLogger, Git } from '@backstage/backend-common';
const mockTemplate = (): TemplateEntityV1alpha1 => ({
@@ -14,20 +14,15 @@
* limitations under the License.
*/
import { Preparers } from '.';
import { TemplateEntityV1alpha1 } from '@backstage/catalog-model';
import { FilePreparer } from './file';
import { GithubPreparer } from './github';
import { ConfigReader } from '@backstage/config';
describe('Preparers', () => {
it('should return the correct preparer based on the hostname', async () => {
const preparer = new GithubPreparer(
new ConfigReader({
host: 'github.com',
apiBaseUrl: 'lols',
token: 'something else yo',
}),
);
const preparer = await GithubPreparer.fromConfig({
host: 'github.com',
apiBaseUrl: 'lols',
token: 'something else yo',
});
const preparers = new Preparers();
preparers.register('github.com', preparer);
@@ -38,13 +33,11 @@ describe('Preparers', () => {
});
it('should throw an error if there is nothing that will match the url provided', async () => {
const preparer = new GithubPreparer(
new ConfigReader({
host: 'github.com',
apiBaseUrl: 'lols',
token: 'something else yo',
}),
);
const preparer = await GithubPreparer.fromConfig({
host: 'github.com',
apiBaseUrl: 'lols',
token: 'something else yo',
});
const preparers = new Preparers();
preparers.register('github.com', preparer);
@@ -14,7 +14,6 @@
* limitations under the License.
*/
import type { TemplateEntityV1alpha1 } from '@backstage/catalog-model';
import { RemoteProtocol } from '../types';
import { Logger } from 'winston';
export type PreparerOptions = {
@@ -18,7 +18,6 @@ jest.mock('@octokit/rest');
jest.mock('./helpers');
import { getVoidLogger } from '@backstage/backend-common';
import { ConfigReader } from '@backstage/config';
import { Octokit, RestEndpointMethodTypes } from '@octokit/rest';
import { GithubPublisher } from './github';
import { initRepoAndPush } from './helpers';
@@ -24,7 +24,6 @@ import { GitlabPublisher } from './gitlab';
import { Gitlab } from '@gitbeaker/node';
import { initRepoAndPush } from './helpers';
import { getVoidLogger } from '@backstage/backend-common';
import { ConfigReader } from '@backstage/config';
describe('GitLab Publisher', () => {
const logger = getVoidLogger();
@@ -15,7 +15,6 @@
*/
import { RequiredTemplateValues } from '../templater';
import { JsonValue } from '@backstage/config';
import { RemoteProtocol } from '../types';
import { Logger } from 'winston';
/**
@@ -44,6 +43,6 @@ export type PublisherResult = {
};
export type PublisherBuilder = {
register(protocol: RemoteProtocol, publisher: PublisherBase): void;
register(host: string, publisher: PublisherBase): void;
get(storePath: string): PublisherBase;
};
@@ -1,21 +0,0 @@
/*
* Copyright 2020 Spotify AB
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
export type RemoteProtocol =
| 'file'
| 'github'
| 'gitlab'
| 'azure'
| 'bitbucket';
@@ -47,7 +47,7 @@ describe('createRouter - working directory', () => {
const mockPreparer = {
prepare: mockPrepare,
};
mockPreparers.register('azure', mockPreparer);
mockPreparers.register('dev.azure.com', mockPreparer);
});
beforeEach(() => {
@@ -65,7 +65,7 @@ describe('createRouter - working directory', () => {
kind: 'Template',
metadata: {
annotations: {
'backstage.io/managed-by-location': 'azure:dev.azure.com',
'backstage.io/managed-by-location': 'azure:https://dev.azure.com',
},
},
spec: {
@@ -28,6 +28,7 @@ import {
TemplaterBuilder,
PublisherBuilder,
parseLocationAnnotation,
FilePreparer,
} from '../scaffolder';
import { CatalogEntityClient } from '../lib/catalog';
import { validate, ValidatorResult } from 'jsonschema';
@@ -135,7 +136,9 @@ export async function createRouter(
);
const preparer =
protcol === file ? new FilePreparer() : preparers.get(pullPath);
protocol === 'file'
? new FilePreparer()
: preparers.get(pullPath);
const skeletonDir = await preparer.prepare(ctx.entity, {
logger: ctx.logger,
@@ -161,9 +164,7 @@ export async function createRouter(
{
name: 'Publish template',
handler: async (ctx: StageContext<{ resultDir: string }>) => {
const publisher = publishers.get(ctx.values.storePath, {
logger: ctx.logger,
});
const publisher = publishers.get(ctx.values.storePath);
ctx.logger.info('Will now store the template');
const result = await publisher.publish({
values: ctx.values,
@@ -176,9 +177,9 @@ export async function createRouter(
],
});
res.status(201).json({ id: job.id });
jobProcessor.run(job);
res.status(201).json({ id: job.id });
});
const app = express();