Adjust to review

Signed-off-by: Philipp Hugenroth <philipph@spotify.com>
This commit is contained in:
Philipp Hugenroth
2023-08-14 16:41:16 +02:00
parent 1bceb3049b
commit 53c52953e4
6 changed files with 100 additions and 62 deletions
+2 -2
View File
@@ -1,5 +1,5 @@
---
'@backstage/plugin-linguist-backend': minor
'@backstage/plugin-linguist-backend': patch
---
**BREAKING**: Removing options from exported `linguistPlugin()` in favour of static config
Breaking change for the alpha export plugin options from exported `linguistPlugin()` in favour of static config.
+7
View File
@@ -27,6 +27,11 @@ export function createRouter(
routerOptions: RouterOptions,
): Promise<express.Router>;
// @public (undocumented)
export function createRouterFromConfig(
routerOptions: RouterOptions,
): Promise<express.Router>;
// @public (undocumented)
export interface LinguistBackendApi {
// (undocumented)
@@ -88,6 +93,8 @@ export interface PluginOptions {
// @public (undocumented)
export interface RouterOptions {
// (undocumented)
config?: Config;
// (undocumented)
database: PluginDatabaseManager;
// (undocumented)
+3 -2
View File
@@ -15,7 +15,8 @@
*/
import { TaskScheduleDefinition } from '@backstage/backend-tasks';
import { HumanDuration, JsonObject } from '@backstage/types';
import { HumanDuration } from '@backstage/types';
import { Options as LinguistJsOptions } from 'linguist-js/dist/types';
export interface Config {
/** Configuration options for the linguist plugin */
@@ -40,7 +41,7 @@ export interface Config {
/**
* [linguist-js](https://www.npmjs.com/package/linguist-js) options
*/
linguistJsOptions?: JsonObject;
linguistJsOptions?: LinguistJsOptions;
/** Options for the tags processor */
tagsProcessor?: {
+10 -39
View File
@@ -19,10 +19,8 @@ import {
coreServices,
createBackendPlugin,
} from '@backstage/backend-plugin-api';
import { readTaskScheduleDefinitionFromConfig } from '@backstage/backend-tasks';
import { HumanDuration } from '@backstage/types';
import { createRouter } from './service/router';
import { createRouterFromConfig } from './service/router';
/**
* Linguist backend plugin
@@ -53,43 +51,16 @@ export const linguistPlugin = createBackendPlugin({
tokenManager,
httpRouter,
}) {
let schedule;
if (config.has('linguist.schedule')) {
schedule = readTaskScheduleDefinitionFromConfig(
config.getConfig('linguist.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(
{
schedule,
batchSize,
useSourceLocation,
age,
kind,
linguistJsOptions,
},
{
logger: loggerToWinstonLogger(logger),
reader,
database,
discovery,
scheduler,
tokenManager,
},
),
await createRouterFromConfig({
logger: loggerToWinstonLogger(logger),
config,
reader,
database,
discovery,
scheduler,
tokenManager,
}),
);
},
});
@@ -25,7 +25,7 @@ import {
import express from 'express';
import request from 'supertest';
import { ConfigReader } from '@backstage/config';
import { createRouter } from './router';
import { createRouter, createRouterFromConfig } from './router';
import { LinguistBackendApi } from '../api';
import { TaskScheduleDefinition } from '@backstage/backend-tasks';
@@ -69,26 +69,57 @@ 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,
},
);
app = express().use(router);
});
beforeEach(() => {
jest.resetAllMocks();
});
describe('GET /health', () => {
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,
},
);
app = express().use(router);
});
it('returns ok', async () => {
const response = await request(app).get('/health');
expect(response.status).toEqual(200);
expect(response.body).toEqual({ status: 'ok' });
});
});
describe('GET /health from config', () => {
beforeAll(async () => {
const config = new ConfigReader({
linguist: {
schedule: {
frequency: { minutes: 2 },
timeout: { minutes: 15 },
initialDelay: { seconds: 15 },
},
age: { days: 30 },
useSourceLocation: false,
},
});
const router = await createRouterFromConfig({
linguistBackendApi: linguistBackendApi,
discovery: testDiscovery,
database: createDatabase(),
reader: mockUrlReader,
logger: getVoidLogger(),
config,
tokenManager: mockedTokenManager,
});
app = express().use(router);
});
it('returns ok', async () => {
const response = await request(app).get('/health');
+31 -3
View File
@@ -28,11 +28,13 @@ import { LinguistBackendApi } from '../api';
import { LinguistBackendDatabase } from '../db';
import {
PluginTaskScheduler,
readTaskScheduleDefinitionFromConfig,
TaskScheduleDefinition,
} from '@backstage/backend-tasks';
import { HumanDuration } from '@backstage/types';
import { CatalogClient } from '@backstage/catalog-client';
import { LinguistBackendClient } from '../api/LinguistBackendClient';
import { Config } from '@backstage/config';
/** @public */
export interface PluginOptions {
@@ -53,6 +55,7 @@ export interface RouterOptions {
database: PluginDatabaseManager;
discovery: PluginEndpointDiscovery;
scheduler?: PluginTaskScheduler;
config?: Config;
}
/** @public */
@@ -60,6 +63,9 @@ export async function createRouter(
pluginOptions: PluginOptions,
routerOptions: RouterOptions,
): Promise<express.Router> {
const { logger, reader, database, discovery, scheduler, tokenManager } =
routerOptions;
const {
schedule,
age,
@@ -69,9 +75,6 @@ export async function createRouter(
linguistJsOptions,
} = pluginOptions;
const { logger, reader, database, discovery, scheduler, tokenManager } =
routerOptions;
const linguistBackendStore = await LinguistBackendDatabase.create(
await database.getClient(),
);
@@ -135,3 +138,28 @@ export async function createRouter(
router.use(errorHandler());
return router;
}
/** @public */
export async function createRouterFromConfig(routerOptions: RouterOptions) {
const { config } = routerOptions;
const pluginOptions: PluginOptions = {};
if (config) {
if (config.has('linguist.schedule')) {
pluginOptions.schedule = readTaskScheduleDefinitionFromConfig(
config.getConfig('linguist.schedule'),
);
}
pluginOptions.batchSize = config.getOptionalNumber('linguist.batchSize');
pluginOptions.useSourceLocation = config.getBoolean(
'linguist.useSourceLocation',
);
pluginOptions.age = config.getOptionalConfig('linguist.age') as
| HumanDuration
| undefined;
pluginOptions.kind = config.getOptionalStringArray('linguist.kind');
pluginOptions.linguistJsOptions = config.getOptionalConfig(
'linguist.linguistJsOptions',
);
}
return createRouter(pluginOptions, routerOptions);
}