From b271d5ca052a654ad7ac0d0326a69e6eed5b68f3 Mon Sep 17 00:00:00 2001 From: Thomas Camargo Date: Wed, 22 Feb 2023 16:28:14 -0700 Subject: [PATCH 1/3] [linguist-backend]: make kind configurable Signed-off-by: Thomas Camargo --- .changeset/witty-geckos-design.md | 5 ++++ plugins/linguist-backend/README.md | 8 ++++++ .../src/api/LinguistBackendApi.test.ts | 28 +++++++++++++++++++ .../src/api/LinguistBackendApi.ts | 12 +++++++- .../linguist-backend/src/service/router.ts | 4 ++- 5 files changed, 55 insertions(+), 2 deletions(-) create mode 100644 .changeset/witty-geckos-design.md create mode 100644 plugins/linguist-backend/src/api/LinguistBackendApi.test.ts 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) { From 20eba13f1d9618e7df8f4a87459a78d9ac74a66a Mon Sep 17 00:00:00 2001 From: Thomas Camargo Date: Thu, 23 Feb 2023 10:09:34 -0700 Subject: [PATCH 2/3] [linguist-backend]: update changeset and update api-report Signed-off-by: Thomas Camargo --- .changeset/witty-geckos-design.md | 4 ++++ plugins/linguist-backend/api-report.md | 3 +++ 2 files changed, 7 insertions(+) diff --git a/.changeset/witty-geckos-design.md b/.changeset/witty-geckos-design.md index 41f1a28081..249e5ddb1f 100644 --- a/.changeset/witty-geckos-design.md +++ b/.changeset/witty-geckos-design.md @@ -3,3 +3,7 @@ --- Allow kind to be configurable + +```ts +return createRouter({ schedule: schedule, kind: ['Component'] }, { ...env }); +``` diff --git a/plugins/linguist-backend/api-report.md b/plugins/linguist-backend/api-report.md index 18fcec978e..d3a77bc2d7 100644 --- a/plugins/linguist-backend/api-report.md +++ b/plugins/linguist-backend/api-report.md @@ -34,6 +34,7 @@ export class LinguistBackendApi { age?: HumanDuration, batchSize?: number, useSourceLocation?: boolean, + kind?: string[], ); // (undocumented) getEntityLanguages(entityRef: string): Promise; @@ -79,6 +80,8 @@ export interface PluginOptions { // (undocumented) batchSize?: number; // (undocumented) + kind?: string[]; + // (undocumented) schedule?: TaskScheduleDefinition; // (undocumented) useSourceLocation?: boolean; From 94fa5f59eb1c7f43ea2f0cf171ed050de7970d05 Mon Sep 17 00:00:00 2001 From: Ben Lambert Date: Fri, 24 Feb 2023 08:30:05 +0100 Subject: [PATCH 3/3] Update witty-geckos-design.md Signed-off-by: Ben Lambert --- .changeset/witty-geckos-design.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.changeset/witty-geckos-design.md b/.changeset/witty-geckos-design.md index 249e5ddb1f..d482332c23 100644 --- a/.changeset/witty-geckos-design.md +++ b/.changeset/witty-geckos-design.md @@ -1,5 +1,5 @@ --- -'@backstage/plugin-linguist-backend': minor +'@backstage/plugin-linguist-backend': patch --- Allow kind to be configurable