Remove options & use config instead

Signed-off-by: Philipp Hugenroth <philipph@spotify.com>
This commit is contained in:
Philipp Hugenroth
2023-08-08 12:59:37 +02:00
parent ea706fd0f3
commit b141003f3d
8 changed files with 107 additions and 148 deletions
+1 -15
View File
@@ -36,7 +36,6 @@ import { badgesPlugin } from '@backstage/plugin-badges-backend';
import { azureDevOpsPlugin } from '@backstage/plugin-azure-devops-backend';
import { linguistPlugin } from '@backstage/plugin-linguist-backend';
import { devtoolsPlugin } from '@backstage/plugin-devtools-backend';
import { TaskScheduleDefinition } from '@backstage/backend-tasks';
import { adrPlugin } from '@backstage/plugin-adr-backend';
import { lighthousePlugin } from '@backstage/plugin-lighthouse-backend';
import { proxyPlugin } from '@backstage/plugin-proxy-backend';
@@ -58,20 +57,7 @@ backend.add(devtoolsPlugin());
backend.add(entityFeedbackPlugin());
// Linguist
const linguistSchedule: TaskScheduleDefinition = {
frequency: { minutes: 2 },
timeout: { minutes: 15 },
initialDelay: { seconds: 15 },
};
backend.add(
linguistPlugin({
schedule: linguistSchedule,
age: { days: 30 },
batchSize: 2,
useSourceLocation: false,
}),
);
backend.add(linguistPlugin());
// Todo
backend.add(todoPlugin());
+2 -16
View File
@@ -14,27 +14,13 @@
* limitations under the License.
*/
import { TaskScheduleDefinition } from '@backstage/backend-tasks';
import { createRouter } from '@backstage/plugin-linguist-backend';
import { Router } from 'express';
import type { PluginEnvironment } from '../types';
export default async function createPlugin(
env: PluginEnvironment,
): Promise<Router> {
const schedule: TaskScheduleDefinition = {
frequency: { minutes: 2 },
timeout: { minutes: 15 },
initialDelay: { seconds: 15 },
};
return createRouter(
{
schedule: schedule,
age: { days: 30 },
batchSize: 2,
useSourceLocation: false,
},
{ ...env },
);
return createRouter({ ...env });
}
+2
View File
@@ -19,6 +19,8 @@ import { HumanDuration } from '@backstage/types';
export interface Config {
/** Configuration options for the linguist plugin */
linguist?: {
// TODO
/** Options for the tags processor */
tagsProcessor?: {
/**
-1
View File
@@ -24,4 +24,3 @@ export * from './processor';
export * from './service/router';
export type { LinguistBackendApi } from './api';
export { linguistPlugin } from './plugin';
export type { LinguistPluginOptions } from './plugin';
+39 -59
View File
@@ -20,68 +20,48 @@ import {
createBackendPlugin,
} from '@backstage/backend-plugin-api';
import { createRouter } from './service/router';
import { TaskScheduleDefinition } from '@backstage/backend-tasks';
import { HumanDuration } from '@backstage/types';
/**
* Options for Linguist backend plugin
*
* @public
*/
export interface LinguistPluginOptions {
schedule?: TaskScheduleDefinition;
age?: HumanDuration;
batchSize?: number;
useSourceLocation?: boolean;
linguistJsOptions?: Record<string, unknown>;
kind?: string[];
}
/**
* Linguist backend plugin
*
* @public
*/
export const linguistPlugin = createBackendPlugin(
(options: LinguistPluginOptions) => ({
pluginId: 'linguist',
register(env) {
env.registerInit({
deps: {
logger: coreServices.logger,
reader: coreServices.urlReader,
database: coreServices.database,
discovery: coreServices.discovery,
scheduler: coreServices.scheduler,
tokenManager: coreServices.tokenManager,
httpRouter: coreServices.httpRouter,
},
async init({
logger,
reader,
database,
discovery,
scheduler,
tokenManager,
httpRouter,
}) {
httpRouter.use(
await createRouter(
{
...options,
},
{
logger: loggerToWinstonLogger(logger),
reader,
database,
discovery,
scheduler,
tokenManager,
},
),
);
},
});
},
}),
);
export const linguistPlugin = createBackendPlugin(() => ({
pluginId: 'linguist',
register(env) {
env.registerInit({
deps: {
logger: coreServices.logger,
config: coreServices.rootConfig,
reader: coreServices.urlReader,
database: coreServices.database,
discovery: coreServices.discovery,
scheduler: coreServices.scheduler,
tokenManager: coreServices.tokenManager,
httpRouter: coreServices.httpRouter,
},
async init({
logger,
config,
reader,
database,
discovery,
scheduler,
tokenManager,
httpRouter,
}) {
httpRouter.use(
await createRouter({
logger: loggerToWinstonLogger(logger),
config,
reader,
database,
discovery,
scheduler,
tokenManager,
}),
);
},
});
},
}));
@@ -27,7 +27,6 @@ import request from 'supertest';
import { ConfigReader } from '@backstage/config';
import { createRouter } from './router';
import { LinguistBackendApi } from '../api';
import { TaskScheduleDefinition } from '@backstage/backend-tasks';
function createDatabase(): PluginDatabaseManager {
return DatabaseManager.fromConfig(
@@ -59,28 +58,20 @@ const mockUrlReader = UrlReaders.default({
config: new ConfigReader({}),
});
const schedule: TaskScheduleDefinition = {
frequency: { minutes: 2 },
timeout: { minutes: 15 },
initialDelay: { seconds: 15 },
};
describe('createRouter', () => {
let linguistBackendApi: jest.Mocked<LinguistBackendApi>;
let app: express.Express;
beforeAll(async () => {
const router = await createRouter(
{ schedule: schedule, age: { days: 30 }, useSourceLocation: false },
{
linguistBackendApi: linguistBackendApi,
discovery: testDiscovery,
database: createDatabase(),
reader: mockUrlReader,
logger: getVoidLogger(),
tokenManager: mockedTokenManager,
},
);
const router = await createRouter({
linguistBackendApi: linguistBackendApi,
discovery: testDiscovery,
database: createDatabase(),
config: new ConfigReader({}),
reader: mockUrlReader,
logger: getVoidLogger(),
tokenManager: mockedTokenManager,
});
app = express().use(router);
});
+46 -22
View File
@@ -21,53 +21,77 @@ import {
TokenManager,
UrlReader,
} from '@backstage/backend-common';
import {
PluginTaskScheduler,
readTaskScheduleDefinitionFromConfig,
TaskScheduleDefinition,
} from '@backstage/backend-tasks';
import { CatalogClient } from '@backstage/catalog-client';
import { Config } from '@backstage/config';
import { HumanDuration } from '@backstage/types';
import express from 'express';
import Router from 'express-promise-router';
import { Logger } from 'winston';
import { LinguistBackendApi } from '../api';
import { LinguistBackendDatabase } from '../db';
import {
PluginTaskScheduler,
TaskScheduleDefinition,
} from '@backstage/backend-tasks';
import { HumanDuration } from '@backstage/types';
import { CatalogClient } from '@backstage/catalog-client';
import { LinguistBackendClient } from '../api/LinguistBackendClient';
/** @public */
export interface PluginOptions {
schedule?: TaskScheduleDefinition;
age?: HumanDuration;
batchSize?: number;
useSourceLocation?: boolean;
linguistJsOptions?: Record<string, unknown>;
kind?: string[];
}
import { LinguistBackendApi } from '../api';
import { LinguistBackendClient } from '../api/LinguistBackendClient';
import { LinguistBackendDatabase } from '../db';
/** @public */
export interface RouterOptions {
linguistBackendApi?: LinguistBackendApi;
logger: Logger;
reader: UrlReader;
config: Config;
tokenManager: TokenManager;
database: PluginDatabaseManager;
discovery: PluginEndpointDiscovery;
scheduler?: PluginTaskScheduler;
}
const DEFAULT_SCHEDULE: TaskScheduleDefinition = {
frequency: { minutes: 2 },
timeout: { minutes: 15 },
initialDelay: { seconds: 15 },
};
// const DEFAULT_AGE = { days: 30 };
const DEFAULT_BATCH_SIZE = 20;
const DEFAULT_USE_SOURCE_LOCATION = false;
/** @public */
export async function createRouter(
pluginOptions: PluginOptions,
routerOptions: RouterOptions,
): Promise<express.Router> {
const {
schedule,
const { config } = routerOptions;
const schedule = config.has('linguist.schedule')
? readTaskScheduleDefinitionFromConfig(
config.getConfig('linguist.schedule'),
)
: DEFAULT_SCHEDULE;
const batchSize = config.has('linguist.batchSize')
? config.getNumber('linguist.batchSize')
: DEFAULT_BATCH_SIZE;
const useSourceLocation = config.has('linguist.useSourceLocation')
? config.getBoolean('linguist.useSourceLocation')
: DEFAULT_USE_SOURCE_LOCATION;
const age = config.getOptionalConfig('linguist.age') as
| HumanDuration
| undefined;
const kind = config.getOptionalStringArray('linguist.kind');
const linguistJsOptions = config.getOptionalConfig(
'linguist.linguistJsOptions',
);
console.log(
age,
batchSize,
useSourceLocation,
kind,
linguistJsOptions,
} = pluginOptions;
schedule,
);
const { logger, reader, database, discovery, scheduler, tokenManager } =
routerOptions;
@@ -26,7 +26,6 @@ import { Server } from 'http';
import { Logger } from 'winston';
import { createRouter } from './router';
import knexFactory from 'knex';
import { TaskScheduleDefinition } from '@backstage/backend-tasks';
export interface ServerOptions {
port: number;
@@ -53,23 +52,15 @@ export async function startStandaloneServer(
return knex;
});
const schedule: TaskScheduleDefinition = {
frequency: { minutes: 2 },
timeout: { minutes: 15 },
initialDelay: { seconds: 15 },
};
logger.debug('Starting application server...');
const router = await createRouter(
{ schedule: schedule, age: { days: 30 }, useSourceLocation: false },
{
database: { getClient: async () => db },
discovery: SingleHostDiscovery.fromConfig(config),
reader: UrlReaders.default({ logger, config }),
logger,
tokenManager: ServerTokenManager.noop(),
},
);
const router = await createRouter({
database: { getClient: async () => db },
discovery: SingleHostDiscovery.fromConfig(config),
reader: UrlReaders.default({ logger, config }),
config,
logger,
tokenManager: ServerTokenManager.noop(),
});
let service = createServiceBuilder(module)
.setPort(options.port)