diff --git a/.changeset/tiny-squids-accept.md b/.changeset/tiny-squids-accept.md new file mode 100644 index 0000000000..e488c35197 --- /dev/null +++ b/.changeset/tiny-squids-accept.md @@ -0,0 +1,5 @@ +--- +'@backstage/plugin-search-backend-module-techdocs': patch +--- + +**BREAKING:** Moved `schedule` & `collators` settings from module options into app-config for the new backend system. You can now pass in a `TaskScheduleDefinitionConfig` through the `search.collators.techdocs.schedule` configuration key & configure the `TechDocsCollatorFactory` with the key `search.collators.techdocs`. diff --git a/.changeset/tiny-squids-deny.md b/.changeset/tiny-squids-deny.md new file mode 100644 index 0000000000..28de9903e9 --- /dev/null +++ b/.changeset/tiny-squids-deny.md @@ -0,0 +1,5 @@ +--- +'@backstage/plugin-search-backend-module-explore': patch +--- + +Breaking change for the alpha export moved `schedule` from module options into app-config for the new backend system. You can now pass in a `TaskScheduleDefinitionConfig` through the `search.collators.explore.schedule` configuration key. diff --git a/.changeset/tiny-squids-undecide.md b/.changeset/tiny-squids-undecide.md new file mode 100644 index 0000000000..91a047f966 --- /dev/null +++ b/.changeset/tiny-squids-undecide.md @@ -0,0 +1,5 @@ +--- +'@backstage/permission-backend': patch +--- + +Refactor backend plugin creation parameter from callback to object. diff --git a/plugins/permission-backend/src/plugin.ts b/plugins/permission-backend/src/plugin.ts index a6b731b120..a131b6e4b4 100644 --- a/plugins/permission-backend/src/plugin.ts +++ b/plugins/permission-backend/src/plugin.ts @@ -80,7 +80,7 @@ export const permissionModuleAllowAllPolicy = createBackendModule({ * * @alpha */ -export const permissionPlugin = createBackendPlugin(() => ({ +export const permissionPlugin = createBackendPlugin({ pluginId: 'permission', register(env) { const policies = new PolicyExtensionPointImpl(); @@ -115,4 +115,4 @@ export const permissionPlugin = createBackendPlugin(() => ({ }, }); }, -})); +}); diff --git a/plugins/search-backend-module-explore/alpha-api-report.md b/plugins/search-backend-module-explore/alpha-api-report.md index 7b0f2658d6..a01bc999fb 100644 --- a/plugins/search-backend-module-explore/alpha-api-report.md +++ b/plugins/search-backend-module-explore/alpha-api-report.md @@ -4,17 +4,9 @@ ```ts import { BackendFeature } from '@backstage/backend-plugin-api'; -import { TaskScheduleDefinition } from '@backstage/backend-tasks'; // @alpha -export const searchModuleExploreCollator: ( - options?: SearchModuleExploreCollatorOptions | undefined, -) => BackendFeature; - -// @alpha -export type SearchModuleExploreCollatorOptions = { - schedule?: TaskScheduleDefinition; -}; +export const searchModuleExploreCollator: () => BackendFeature; // (No @packageDocumentation comment for this package) ``` diff --git a/plugins/search-backend-module-explore/config.d.ts b/plugins/search-backend-module-explore/config.d.ts new file mode 100644 index 0000000000..4401f7a8bf --- /dev/null +++ b/plugins/search-backend-module-explore/config.d.ts @@ -0,0 +1,27 @@ +/* + * 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 { TaskScheduleDefinitionConfig } from '@backstage/backend-tasks'; + +export interface Config { + search?: { + collators?: { + explore?: { + schedule?: TaskScheduleDefinitionConfig; + }; + }; + }; +} diff --git a/plugins/search-backend-module-explore/package.json b/plugins/search-backend-module-explore/package.json index e8f3835b39..b8ee2f04e0 100644 --- a/plugins/search-backend-module-explore/package.json +++ b/plugins/search-backend-module-explore/package.json @@ -58,6 +58,8 @@ "msw": "^1.2.1" }, "files": [ - "dist" - ] + "dist", + "config.d.ts" + ], + "configSchema": "config.d.ts" } diff --git a/plugins/search-backend-module-explore/src/alpha.test.ts b/plugins/search-backend-module-explore/src/alpha.test.ts index 9b133f0fd5..b7a44e4898 100644 --- a/plugins/search-backend-module-explore/src/alpha.test.ts +++ b/plugins/search-backend-module-explore/src/alpha.test.ts @@ -14,7 +14,7 @@ * limitations under the License. */ -import { startTestBackend } from '@backstage/backend-test-utils'; +import { mockServices, startTestBackend } from '@backstage/backend-test-utils'; import { searchIndexRegistryExtensionPoint } from '@backstage/plugin-search-backend-node/alpha'; import { searchModuleExploreCollator } from './alpha'; @@ -34,7 +34,18 @@ describe('searchModuleExploreCollator', () => { extensionPoints: [ [searchIndexRegistryExtensionPoint, extensionPointMock], ], - features: [searchModuleExploreCollator({ schedule })], + features: [searchModuleExploreCollator()], + services: [ + mockServices.rootConfig.factory({ + data: { + search: { + explore: { + schedule, + }, + }, + }, + }), + ], }); expect(extensionPointMock.addCollator).toHaveBeenCalledTimes(1); diff --git a/plugins/search-backend-module-explore/src/alpha.ts b/plugins/search-backend-module-explore/src/alpha.ts index fadbcda3a5..403699e2cb 100644 --- a/plugins/search-backend-module-explore/src/alpha.ts +++ b/plugins/search-backend-module-explore/src/alpha.ts @@ -23,66 +23,59 @@ import { coreServices, createBackendModule, } from '@backstage/backend-plugin-api'; -import { TaskScheduleDefinition } from '@backstage/backend-tasks'; import { loggerToWinstonLogger } from '@backstage/backend-common'; import { searchIndexRegistryExtensionPoint } from '@backstage/plugin-search-backend-node/alpha'; import { ToolDocumentCollatorFactory } from '@backstage/plugin-search-backend-module-explore'; - -/** - * Options for {@link searchModuleExploreCollator}. - * - * @alpha - */ -export type SearchModuleExploreCollatorOptions = { - schedule?: TaskScheduleDefinition; -}; +import { readTaskScheduleDefinitionFromConfig } from '@backstage/backend-tasks'; /** * Search backend module for the Explore index. * * @alpha */ -export const searchModuleExploreCollator = createBackendModule( - (options?: SearchModuleExploreCollatorOptions) => ({ - moduleId: 'exploreCollator', - pluginId: 'search', - register(env) { - env.registerInit({ - deps: { - config: coreServices.rootConfig, - logger: coreServices.logger, - discovery: coreServices.discovery, - scheduler: coreServices.scheduler, - tokenManager: coreServices.tokenManager, - indexRegistry: searchIndexRegistryExtensionPoint, - }, - async init({ - config, - logger, - discovery, - scheduler, - indexRegistry, - tokenManager, - }) { - const defaultSchedule = { - frequency: { minutes: 10 }, - timeout: { minutes: 15 }, - initialDelay: { seconds: 3 }, - }; +export const searchModuleExploreCollator = createBackendModule({ + moduleId: 'exploreCollator', + pluginId: 'search', + register(env) { + env.registerInit({ + deps: { + config: coreServices.rootConfig, + logger: coreServices.logger, + discovery: coreServices.discovery, + scheduler: coreServices.scheduler, + tokenManager: coreServices.tokenManager, + indexRegistry: searchIndexRegistryExtensionPoint, + }, + async init({ + config, + logger, + discovery, + scheduler, + indexRegistry, + tokenManager, + }) { + const defaultSchedule = { + frequency: { minutes: 10 }, + timeout: { minutes: 15 }, + initialDelay: { seconds: 3 }, + }; - indexRegistry.addCollator({ - schedule: scheduler.createScheduledTaskRunner( - options?.schedule ?? defaultSchedule, - ), - factory: ToolDocumentCollatorFactory.fromConfig(config, { - discovery, - logger: loggerToWinstonLogger(logger), - tokenManager, - }), - }); - }, - }); - }, - }), -); + const schedule = config.has('search.collators.explore.schedule') + ? readTaskScheduleDefinitionFromConfig( + config.getConfig('search.collators.explore.schedule'), + ) + : defaultSchedule; + + indexRegistry.addCollator({ + schedule: scheduler.createScheduledTaskRunner(schedule), + factory: ToolDocumentCollatorFactory.fromConfig(config, { + discovery, + logger: loggerToWinstonLogger(logger), + tokenManager, + }), + }); + }, + }); + }, +}); diff --git a/plugins/search-backend-module-techdocs/alpha-api-report.md b/plugins/search-backend-module-techdocs/alpha-api-report.md index 0eac010b4c..6e36a0020a 100644 --- a/plugins/search-backend-module-techdocs/alpha-api-report.md +++ b/plugins/search-backend-module-techdocs/alpha-api-report.md @@ -4,21 +4,9 @@ ```ts import { BackendFeature } from '@backstage/backend-plugin-api'; -import { TaskScheduleDefinition } from '@backstage/backend-tasks'; -import { TechDocsCollatorFactoryOptions } from '@backstage/plugin-search-backend-module-techdocs'; // @alpha -export const searchModuleTechDocsCollator: ( - options?: SearchModuleTechDocsCollatorOptions | undefined, -) => BackendFeature; - -// @alpha -export type SearchModuleTechDocsCollatorOptions = Omit< - TechDocsCollatorFactoryOptions, - 'logger' | 'discovery' | 'tokenManager' | 'catalogClient' -> & { - schedule?: TaskScheduleDefinition; -}; +export const searchModuleTechDocsCollator: () => BackendFeature; // (No @packageDocumentation comment for this package) ``` diff --git a/plugins/search-backend-module-techdocs/config.d.ts b/plugins/search-backend-module-techdocs/config.d.ts new file mode 100644 index 0000000000..80e47e3254 --- /dev/null +++ b/plugins/search-backend-module-techdocs/config.d.ts @@ -0,0 +1,30 @@ +/* + * 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 { TaskScheduleDefinitionConfig } from '@backstage/backend-tasks'; + +export interface Config { + search?: { + collators?: { + techdocs?: { + schedule?: TaskScheduleDefinitionConfig; + locationTemplate?: string; + parallelismLimit?: number; + legacyPathCasing?: boolean; + }; + }; + }; +} diff --git a/plugins/search-backend-module-techdocs/package.json b/plugins/search-backend-module-techdocs/package.json index 1ee329075d..9baa673a4a 100644 --- a/plugins/search-backend-module-techdocs/package.json +++ b/plugins/search-backend-module-techdocs/package.json @@ -65,6 +65,8 @@ "msw": "^1.0.0" }, "files": [ - "dist" - ] + "dist", + "config.d.ts" + ], + "configSchema": "config.d.ts" } diff --git a/plugins/search-backend-module-techdocs/src/alpha.test.ts b/plugins/search-backend-module-techdocs/src/alpha.test.ts index 011b00b12c..1e7ef494bb 100644 --- a/plugins/search-backend-module-techdocs/src/alpha.test.ts +++ b/plugins/search-backend-module-techdocs/src/alpha.test.ts @@ -14,7 +14,7 @@ * limitations under the License. */ -import { startTestBackend } from '@backstage/backend-test-utils'; +import { mockServices, startTestBackend } from '@backstage/backend-test-utils'; import { searchIndexRegistryExtensionPoint } from '@backstage/plugin-search-backend-node/alpha'; import { searchModuleTechDocsCollator } from './alpha'; @@ -34,7 +34,18 @@ describe('searchModuleTechDocsCollator', () => { extensionPoints: [ [searchIndexRegistryExtensionPoint, extensionPointMock], ], - features: [searchModuleTechDocsCollator({ schedule })], + features: [searchModuleTechDocsCollator()], + services: [ + mockServices.rootConfig.factory({ + data: { + search: { + techdocs: { + schedule, + }, + }, + }, + }), + ], }); expect(extensionPointMock.addCollator).toHaveBeenCalledTimes(1); diff --git a/plugins/search-backend-module-techdocs/src/alpha.ts b/plugins/search-backend-module-techdocs/src/alpha.ts index 5d82b6804f..bb084ddfd9 100644 --- a/plugins/search-backend-module-techdocs/src/alpha.ts +++ b/plugins/search-backend-module-techdocs/src/alpha.ts @@ -19,79 +19,65 @@ * A module for the search backend that exports TechDocs modules. */ +import { loggerToWinstonLogger } from '@backstage/backend-common'; import { coreServices, createBackendModule, } from '@backstage/backend-plugin-api'; -import { TaskScheduleDefinition } from '@backstage/backend-tasks'; -import { loggerToWinstonLogger } from '@backstage/backend-common'; -import { searchIndexRegistryExtensionPoint } from '@backstage/plugin-search-backend-node/alpha'; +import { readTaskScheduleDefinitionFromConfig } from '@backstage/backend-tasks'; import { catalogServiceRef } from '@backstage/plugin-catalog-node/alpha'; - -import { - DefaultTechDocsCollatorFactory, - TechDocsCollatorFactoryOptions, -} from '@backstage/plugin-search-backend-module-techdocs'; - -/** - * @alpha - * Options for {@link searchModuleTechDocsCollator}. - */ -export type SearchModuleTechDocsCollatorOptions = Omit< - TechDocsCollatorFactoryOptions, - 'logger' | 'discovery' | 'tokenManager' | 'catalogClient' -> & { - schedule?: TaskScheduleDefinition; -}; +import { DefaultTechDocsCollatorFactory } from '@backstage/plugin-search-backend-module-techdocs'; +import { searchIndexRegistryExtensionPoint } from '@backstage/plugin-search-backend-node/alpha'; /** * @alpha * Search backend module for the TechDocs index. */ -export const searchModuleTechDocsCollator = createBackendModule( - (options?: SearchModuleTechDocsCollatorOptions) => ({ - moduleId: 'techDocsCollator', - pluginId: 'search', - register(env) { - env.registerInit({ - deps: { - config: coreServices.rootConfig, - logger: coreServices.logger, - discovery: coreServices.discovery, - tokenManager: coreServices.tokenManager, - scheduler: coreServices.scheduler, - catalog: catalogServiceRef, - indexRegistry: searchIndexRegistryExtensionPoint, - }, - async init({ - config, - logger, - discovery, - tokenManager, - scheduler, - catalog, - indexRegistry, - }) { - const defaultSchedule = { - frequency: { minutes: 10 }, - timeout: { minutes: 15 }, - initialDelay: { seconds: 3 }, - }; +export const searchModuleTechDocsCollator = createBackendModule({ + moduleId: 'techDocsCollator', + pluginId: 'search', + register(env) { + env.registerInit({ + deps: { + config: coreServices.rootConfig, + logger: coreServices.logger, + discovery: coreServices.discovery, + tokenManager: coreServices.tokenManager, + scheduler: coreServices.scheduler, + catalog: catalogServiceRef, + indexRegistry: searchIndexRegistryExtensionPoint, + }, + async init({ + config, + logger, + discovery, + tokenManager, + scheduler, + catalog, + indexRegistry, + }) { + const defaultSchedule = { + frequency: { minutes: 10 }, + timeout: { minutes: 15 }, + initialDelay: { seconds: 3 }, + }; - indexRegistry.addCollator({ - schedule: scheduler.createScheduledTaskRunner( - options?.schedule ?? defaultSchedule, - ), - factory: DefaultTechDocsCollatorFactory.fromConfig(config, { - ...options, - discovery, - tokenManager, - logger: loggerToWinstonLogger(logger), - catalogClient: catalog, - }), - }); - }, - }); - }, - }), -); + const schedule = config.has('search.collators.techdocs.schedule') + ? readTaskScheduleDefinitionFromConfig( + config.getConfig('search.collators.techdocs.schedule'), + ) + : defaultSchedule; + + indexRegistry.addCollator({ + schedule: scheduler.createScheduledTaskRunner(schedule), + factory: DefaultTechDocsCollatorFactory.fromConfig(config, { + discovery, + tokenManager, + logger: loggerToWinstonLogger(logger), + catalogClient: catalog, + }), + }); + }, + }); + }, +}); diff --git a/plugins/search-backend-module-techdocs/src/collators/DefaultTechDocsCollatorFactory.test.ts b/plugins/search-backend-module-techdocs/src/collators/DefaultTechDocsCollatorFactory.test.ts index d9329151c6..319b8fb446 100644 --- a/plugins/search-backend-module-techdocs/src/collators/DefaultTechDocsCollatorFactory.test.ts +++ b/plugins/search-backend-module-techdocs/src/collators/DefaultTechDocsCollatorFactory.test.ts @@ -174,10 +174,15 @@ describe('DefaultTechDocsCollatorFactory', () => { it('maps a returned entity with a custom locationTemplate', async () => { // Provide an alternate location template. - factory = DefaultTechDocsCollatorFactory.fromConfig(config, { + const _config = new ConfigReader({ + ...config.get(), + search: { + collators: { techdocs: { locationTemplate: '/software/:name' } }, + }, + }); + factory = DefaultTechDocsCollatorFactory.fromConfig(_config, { discovery: mockDiscoveryApi, tokenManager: mockTokenManager, - locationTemplate: '/software/:name', logger, }); collator = await factory.getCollator(); @@ -194,10 +199,17 @@ describe('DefaultTechDocsCollatorFactory', () => { // A parallelismLimit of 1 is a catalog limit of 50 per request. Code // above in the /entities handler ensures valid entities are only // returned on the second page. - factory = DefaultTechDocsCollatorFactory.fromConfig(config, { - ...options, - parallelismLimit: 1, + const _config = new ConfigReader({ + ...config.get(), + search: { + collators: { + techdocs: { + parallelismLimit: 1, + }, + }, + }, }); + factory = DefaultTechDocsCollatorFactory.fromConfig(_config, options); collator = await factory.getCollator(); const pipeline = TestPipeline.fromCollator(collator); @@ -205,6 +217,9 @@ describe('DefaultTechDocsCollatorFactory', () => { // Only 1 entity with TechDocs configured multiplied by 3 pages. expect(documents).toHaveLength(3); + expect(_config.get('search.collators.techdocs.parallelismLimit')).toEqual( + 1, + ); }); describe('with legacyPathCasing configuration', () => { diff --git a/plugins/search-backend-module-techdocs/src/collators/DefaultTechDocsCollatorFactory.ts b/plugins/search-backend-module-techdocs/src/collators/DefaultTechDocsCollatorFactory.ts index e2c6f5913c..051dc93189 100644 --- a/plugins/search-backend-module-techdocs/src/collators/DefaultTechDocsCollatorFactory.ts +++ b/plugins/search-backend-module-techdocs/src/collators/DefaultTechDocsCollatorFactory.ts @@ -104,7 +104,18 @@ export class DefaultTechDocsCollatorFactory implements DocumentCollatorFactory { config.getOptionalBoolean( 'techdocs.legacyUseCaseSensitiveTripletPaths', ) || false; - return new DefaultTechDocsCollatorFactory({ ...options, legacyPathCasing }); + const locationTemplate = config.getOptionalString( + 'search.collators.techdocs.locationTemplate', + ); + const parallelismLimit = config.getOptionalNumber( + 'search.collators.techdocs.parallelismLimit', + ); + return new DefaultTechDocsCollatorFactory({ + ...options, + locationTemplate, + parallelismLimit, + legacyPathCasing, + }); } async getCollator(): Promise {