diff --git a/.changeset/witty-geckos-design.md b/.changeset/witty-geckos-design.md new file mode 100644 index 0000000000..41f1a28081 --- /dev/null +++ b/.changeset/witty-geckos-design.md @@ -0,0 +1,5 @@ +--- +'@backstage/plugin-linguist-backend': minor +--- + +Allow kind to be configurable diff --git a/plugins/linguist-backend/README.md b/plugins/linguist-backend/README.md index cd63f5db03..a7c89d23b3 100644 --- a/plugins/linguist-backend/README.md +++ b/plugins/linguist-backend/README.md @@ -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`: diff --git a/plugins/linguist-backend/src/api/LinguistBackendApi.test.ts b/plugins/linguist-backend/src/api/LinguistBackendApi.test.ts new file mode 100644 index 0000000000..741d52e734 --- /dev/null +++ b/plugins/linguist-backend/src/api/LinguistBackendApi.test.ts @@ -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']); + }); +}); diff --git a/plugins/linguist-backend/src/api/LinguistBackendApi.ts b/plugins/linguist-backend/src/api/LinguistBackendApi.ts index 105335b0ed..6a4cc0ea56 100644 --- a/plugins/linguist-backend/src/api/LinguistBackendApi.ts +++ b/plugins/linguist-backend/src/api/LinguistBackendApi.ts @@ -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 { @@ -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; +} diff --git a/plugins/linguist-backend/src/service/router.ts b/plugins/linguist-backend/src/service/router.ts index 18c43d996a..66138f5ed2 100644 --- a/plugins/linguist-backend/src/service/router.ts +++ b/plugins/linguist-backend/src/service/router.ts @@ -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 { - 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) {