[linguist-backend]: make kind configurable

Signed-off-by: Thomas Camargo <camargothomas@gmail.com>
This commit is contained in:
Thomas Camargo
2023-02-22 16:28:14 -07:00
parent f16d9f31cd
commit b271d5ca05
5 changed files with 55 additions and 2 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@backstage/plugin-linguist-backend': minor
---
Allow kind to be configurable
+8
View File
@@ -67,6 +67,14 @@ return createRouter({ schedule: schedule, batchSize: 40 }, { ...env });
**Note:** The default batch size is 20
## Kind
The default setup only processes entities of kind `['API', 'Component', 'Template']`. To control the `kind` that are processed provide that to the `createRouter` function in your `packages/backend/src/plugins/linguist.ts` like this:
```ts
return createRouter({ schedule: schedule, kind: ['Component'] }, { ...env });
```
## Refresh
The default setup will only generate the language breakdown for entities with the linguist annotation that have not been generated yet. If you want this process to also refresh the data you can do so by adding the `age` (as a `HumanDuration`) in your `packages/backend/src/plugins/linguist.ts` when you call `createRouter`:
@@ -0,0 +1,28 @@
/*
* Copyright 2023 The Backstage Authors
*
* 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.
*/
import { kindOrDefault } from './LinguistBackendApi';
describe('kindOrDefault', () => {
it('should return default kind when undefined', () => {
expect(kindOrDefault()).toEqual(['API', 'Component', 'Template']);
});
it('should return the default kind when empty', () => {
expect(kindOrDefault([])).toEqual(['API', 'Component', 'Template']);
});
it('should return provided kind when not empty', () => {
expect(kindOrDefault(['API'])).toEqual(['API']);
});
});
@@ -56,6 +56,7 @@ export class LinguistBackendApi {
private readonly age?: HumanDuration;
private readonly batchSize?: number;
private readonly useSourceLocation?: boolean;
private readonly kind: string[];
public constructor(
logger: Logger,
store: LinguistBackendStore,
@@ -65,6 +66,7 @@ export class LinguistBackendApi {
age?: HumanDuration,
batchSize?: number,
useSourceLocation?: boolean,
kind?: string[],
) {
this.logger = logger;
this.store = store;
@@ -75,6 +77,7 @@ export class LinguistBackendApi {
this.batchSize = batchSize;
this.age = age;
this.useSourceLocation = useSourceLocation;
this.kind = kindOrDefault(kind);
}
public async getEntityLanguages(entityRef: string): Promise<Languages> {
@@ -99,7 +102,7 @@ export class LinguistBackendApi {
: LINGUIST_ANNOTATION;
const request: GetEntitiesRequest = {
filter: {
kind: ['API', 'Component', 'Template'],
kind: this.kind,
[`metadata.annotations.${annotationKey}`]: CATALOG_FILTER_EXISTS,
},
fields: ['kind', 'metadata'],
@@ -228,3 +231,10 @@ export class LinguistBackendApi {
}
}
}
export function kindOrDefault(kind?: string[]) {
if (!kind || kind.length === 0) {
return ['API', 'Component', 'Template'];
}
return kind;
}
@@ -38,6 +38,7 @@ export interface PluginOptions {
age?: HumanDuration;
batchSize?: number;
useSourceLocation?: boolean;
kind?: string[];
}
/** @public */
@@ -56,7 +57,7 @@ export async function createRouter(
pluginOptions: PluginOptions,
routerOptions: RouterOptions,
): Promise<express.Router> {
const { schedule, age, batchSize, useSourceLocation } = pluginOptions;
const { schedule, age, batchSize, useSourceLocation, kind } = pluginOptions;
const { logger, reader, database, discovery, scheduler, tokenManager } =
routerOptions;
@@ -76,6 +77,7 @@ export async function createRouter(
age,
batchSize,
useSourceLocation,
kind,
);
if (scheduler && schedule) {