Avoid bigger change to plugin
Signed-off-by: Philipp Hugenroth <philipph@spotify.com>
This commit is contained in:
@@ -14,13 +14,27 @@
|
||||
* 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> {
|
||||
return createRouter({ ...env });
|
||||
const schedule: TaskScheduleDefinition = {
|
||||
frequency: { minutes: 2 },
|
||||
timeout: { minutes: 15 },
|
||||
initialDelay: { seconds: 15 },
|
||||
};
|
||||
|
||||
return createRouter(
|
||||
{
|
||||
schedule: schedule,
|
||||
age: { days: 30 },
|
||||
batchSize: 2,
|
||||
useSourceLocation: false,
|
||||
},
|
||||
{ ...env },
|
||||
);
|
||||
}
|
||||
|
||||
Vendored
+22
-1
@@ -14,12 +14,33 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import { TaskScheduleDefinition } from '@backstage/backend-tasks';
|
||||
import { HumanDuration } from '@backstage/types';
|
||||
|
||||
export interface Config {
|
||||
/** Configuration options for the linguist plugin */
|
||||
linguist?: {
|
||||
// TODO
|
||||
schedule?: TaskScheduleDefinition;
|
||||
/**
|
||||
* @default 20
|
||||
*/
|
||||
batchSize?: number;
|
||||
/**
|
||||
* @default false
|
||||
*/
|
||||
useSourceLocation?: boolean;
|
||||
/**
|
||||
* Refresh generated language breakdown
|
||||
*/
|
||||
age?: HumanDuration;
|
||||
/**
|
||||
* @default ['API', 'Component', 'Template']
|
||||
*/
|
||||
kind?: string[];
|
||||
/**
|
||||
* [linguist-js](https://www.npmjs.com/package/linguist-js) options
|
||||
*/
|
||||
linguistJsOptions: Record<string, unknown>;
|
||||
|
||||
/** Options for the tags processor */
|
||||
tagsProcessor?: {
|
||||
|
||||
@@ -19,8 +19,20 @@ import {
|
||||
coreServices,
|
||||
createBackendPlugin,
|
||||
} from '@backstage/backend-plugin-api';
|
||||
import {
|
||||
TaskScheduleDefinition,
|
||||
readTaskScheduleDefinitionFromConfig,
|
||||
} from '@backstage/backend-tasks';
|
||||
import { HumanDuration } from '@backstage/types';
|
||||
|
||||
import { createRouter } from './service/router';
|
||||
|
||||
const DEFAULT_SCHEDULE: TaskScheduleDefinition = {
|
||||
frequency: { minutes: 2 },
|
||||
timeout: { minutes: 15 },
|
||||
initialDelay: { seconds: 15 },
|
||||
};
|
||||
|
||||
/**
|
||||
* Linguist backend plugin
|
||||
*
|
||||
@@ -50,16 +62,42 @@ export const linguistPlugin = createBackendPlugin(() => ({
|
||||
tokenManager,
|
||||
httpRouter,
|
||||
}) {
|
||||
const schedule = config.has('linguist.schedule')
|
||||
? readTaskScheduleDefinitionFromConfig(
|
||||
config.getConfig('linguist.schedule'),
|
||||
)
|
||||
: DEFAULT_SCHEDULE;
|
||||
const batchSize = config.getOptionalNumber('linguist.batchSize');
|
||||
const useSourceLocation = config.getBoolean(
|
||||
'linguist.useSourceLocation',
|
||||
);
|
||||
const age = config.getOptionalConfig('linguist.age') as
|
||||
| HumanDuration
|
||||
| undefined;
|
||||
const kind = config.getOptionalStringArray('linguist.kind');
|
||||
const linguistJsOptions = config.getOptionalConfig(
|
||||
'linguist.linguistJsOptions',
|
||||
);
|
||||
|
||||
httpRouter.use(
|
||||
await createRouter({
|
||||
logger: loggerToWinstonLogger(logger),
|
||||
config,
|
||||
reader,
|
||||
database,
|
||||
discovery,
|
||||
scheduler,
|
||||
tokenManager,
|
||||
}),
|
||||
await createRouter(
|
||||
{
|
||||
schedule,
|
||||
batchSize,
|
||||
useSourceLocation,
|
||||
age,
|
||||
kind,
|
||||
linguistJsOptions,
|
||||
},
|
||||
{
|
||||
logger: loggerToWinstonLogger(logger),
|
||||
reader,
|
||||
database,
|
||||
discovery,
|
||||
scheduler,
|
||||
tokenManager,
|
||||
},
|
||||
),
|
||||
);
|
||||
},
|
||||
});
|
||||
|
||||
@@ -27,6 +27,7 @@ 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(
|
||||
@@ -58,20 +59,28 @@ 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({
|
||||
linguistBackendApi: linguistBackendApi,
|
||||
discovery: testDiscovery,
|
||||
database: createDatabase(),
|
||||
config: new ConfigReader({}),
|
||||
reader: mockUrlReader,
|
||||
logger: getVoidLogger(),
|
||||
tokenManager: mockedTokenManager,
|
||||
});
|
||||
const router = await createRouter(
|
||||
{ schedule: schedule, age: { days: 30 }, useSourceLocation: false },
|
||||
{
|
||||
linguistBackendApi: linguistBackendApi,
|
||||
discovery: testDiscovery,
|
||||
database: createDatabase(),
|
||||
reader: mockUrlReader,
|
||||
logger: getVoidLogger(),
|
||||
tokenManager: mockedTokenManager,
|
||||
},
|
||||
);
|
||||
app = express().use(router);
|
||||
});
|
||||
|
||||
|
||||
@@ -21,77 +21,53 @@ 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 { LinguistBackendClient } from '../api/LinguistBackendClient';
|
||||
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[];
|
||||
}
|
||||
|
||||
/** @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 { 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(
|
||||
const {
|
||||
schedule,
|
||||
age,
|
||||
batchSize,
|
||||
useSourceLocation,
|
||||
kind,
|
||||
linguistJsOptions,
|
||||
schedule,
|
||||
);
|
||||
} = pluginOptions;
|
||||
|
||||
const { logger, reader, database, discovery, scheduler, tokenManager } =
|
||||
routerOptions;
|
||||
|
||||
@@ -26,6 +26,7 @@ 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;
|
||||
@@ -52,15 +53,23 @@ 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({
|
||||
database: { getClient: async () => db },
|
||||
discovery: SingleHostDiscovery.fromConfig(config),
|
||||
reader: UrlReaders.default({ logger, config }),
|
||||
config,
|
||||
logger,
|
||||
tokenManager: ServerTokenManager.noop(),
|
||||
});
|
||||
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(),
|
||||
},
|
||||
);
|
||||
|
||||
let service = createServiceBuilder(module)
|
||||
.setPort(options.port)
|
||||
|
||||
Reference in New Issue
Block a user