diff --git a/.changeset/dirty-plums-fix.md b/.changeset/dirty-plums-fix.md new file mode 100644 index 0000000000..fb0655377c --- /dev/null +++ b/.changeset/dirty-plums-fix.md @@ -0,0 +1,17 @@ +--- +'@backstage/plugin-tech-insights-backend-module-jsonfc': patch +--- + +Add support for the new backend system. + +A new backend module for the tech-insights backend +was added and exported as `default`. + +The module will register the `JsonRulesEngineFactCheckerFactory` +as `FactCheckerFactory`, loading checks from the config. + +You can use it with the new backend system like + +```ts title="packages/backend/src/index.ts" +backend.add(import('@backstage/plugin-tech-insights-backend-module-jsonfc')); +``` diff --git a/plugins/tech-insights-backend-module-jsonfc/README.md b/plugins/tech-insights-backend-module-jsonfc/README.md index 3f9e91408c..c405ac34ef 100644 --- a/plugins/tech-insights-backend-module-jsonfc/README.md +++ b/plugins/tech-insights-backend-module-jsonfc/README.md @@ -13,7 +13,17 @@ To add this FactChecker into your Tech Insights you need to install the module i yarn add --cwd packages/backend @backstage/plugin-tech-insights-backend-module-jsonfc ``` -and modify the `techInsights.ts` file to contain a reference to the FactCheckers implementation. +### Add to the backend + +```ts title="packages/backend/src/index.ts" +backend.add(import('@backstage/plugin-tech-insights-backend-module-jsonfc')); +``` + +This setup requires checks to be provided using the config. + +### Add to the backend (old) + +Modify the `techInsights.ts` file to contain a reference to the FactCheckers implementation. ```diff +import { JsonRulesEngineFactCheckerFactory } from '@backstage/plugin-tech-insights-backend-module-jsonfc'; @@ -34,7 +44,7 @@ and modify the `techInsights.ts` file to contain a reference to the FactCheckers }); ``` -By default this implementation comes with an in-memory storage to store checks. You can inject an additional data store by adding an implementation of `TechInsightCheckRegistry` into the constructor options when creating a `JsonRulesEngineFactCheckerFactory`. That can be done as follows +By default, this implementation comes with an in-memory storage to store checks. You can inject an additional data store by adding an implementation of `TechInsightCheckRegistry` into the constructor options when creating a `JsonRulesEngineFactCheckerFactory`. That can be done as follows ```diff const myTechInsightCheckRegistry: TechInsightCheckRegistry = // snip diff --git a/plugins/tech-insights-backend-module-jsonfc/api-report.md b/plugins/tech-insights-backend-module-jsonfc/api-report.md index d28ee28f8d..790aa0b666 100644 --- a/plugins/tech-insights-backend-module-jsonfc/api-report.md +++ b/plugins/tech-insights-backend-module-jsonfc/api-report.md @@ -3,6 +3,7 @@ > Do not edit this file. It is a report generated by [API Extractor](https://api-extractor.com/). ```ts +import { BackendFeature } from '@backstage/backend-plugin-api'; import { BooleanCheckResult } from '@backstage/plugin-tech-insights-common'; import { CheckResponse } from '@backstage/plugin-tech-insights-common'; import { CheckValidationResponse } from '@backstage/plugin-tech-insights-node'; @@ -110,5 +111,9 @@ export interface TechInsightJsonRuleCheck extends TechInsightCheck { rule: Rule; } +// @public +const techInsightsModuleJsonRulesEngineFactCheckerFactory: () => BackendFeature; +export default techInsightsModuleJsonRulesEngineFactCheckerFactory; + // (No @packageDocumentation comment for this package) ``` diff --git a/plugins/tech-insights-backend-module-jsonfc/package.json b/plugins/tech-insights-backend-module-jsonfc/package.json index 431a7152d4..c2231f4c7e 100644 --- a/plugins/tech-insights-backend-module-jsonfc/package.json +++ b/plugins/tech-insights-backend-module-jsonfc/package.json @@ -34,6 +34,7 @@ }, "dependencies": { "@backstage/backend-common": "workspace:^", + "@backstage/backend-plugin-api": "workspace:^", "@backstage/config": "workspace:^", "@backstage/errors": "workspace:^", "@backstage/plugin-tech-insights-common": "workspace:^", @@ -46,6 +47,7 @@ "winston": "^3.2.1" }, "devDependencies": { + "@backstage/backend-test-utils": "workspace:^", "@backstage/cli": "workspace:^" }, "files": [ diff --git a/plugins/tech-insights-backend-module-jsonfc/src/index.ts b/plugins/tech-insights-backend-module-jsonfc/src/index.ts index 6462efc403..1c1e8ab77d 100644 --- a/plugins/tech-insights-backend-module-jsonfc/src/index.ts +++ b/plugins/tech-insights-backend-module-jsonfc/src/index.ts @@ -15,6 +15,7 @@ */ export { JSON_RULE_ENGINE_CHECK_TYPE } from './constants'; +export { techInsightsModuleJsonRulesEngineFactCheckerFactory as default } from './module'; export * from './service'; export type { JsonRuleCheckResponse, diff --git a/plugins/tech-insights-backend-module-jsonfc/src/module/index.ts b/plugins/tech-insights-backend-module-jsonfc/src/module/index.ts new file mode 100644 index 0000000000..0fa167e883 --- /dev/null +++ b/plugins/tech-insights-backend-module-jsonfc/src/module/index.ts @@ -0,0 +1,17 @@ +/* + * Copyright 2024 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. + */ + +export * from './techInsightsModuleJsonRulesEngineFactCheckerFactory'; diff --git a/plugins/tech-insights-backend-module-jsonfc/src/module/techInsightsModuleJsonRulesEngineFactCheckerFactory.test.ts b/plugins/tech-insights-backend-module-jsonfc/src/module/techInsightsModuleJsonRulesEngineFactCheckerFactory.test.ts new file mode 100644 index 0000000000..b6df48a8b2 --- /dev/null +++ b/plugins/tech-insights-backend-module-jsonfc/src/module/techInsightsModuleJsonRulesEngineFactCheckerFactory.test.ts @@ -0,0 +1,68 @@ +/* + * Copyright 2024 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 { mockServices, startTestBackend } from '@backstage/backend-test-utils'; +import { techInsightsFactCheckerFactoryExtensionPoint } from '@backstage/plugin-tech-insights-node'; +import { JSON_RULE_ENGINE_CHECK_TYPE } from '../constants'; +import { techInsightsModuleJsonRulesEngineFactCheckerFactory } from './techInsightsModuleJsonRulesEngineFactCheckerFactory'; + +describe('techInsightsModuleJsonRulesEngineFactCheckerFactory', () => { + it('should register the factory', async () => { + const extensionPoint = { + setFactCheckerFactory: jest.fn(), + } satisfies Partial; + + await startTestBackend({ + extensionPoints: [ + [techInsightsFactCheckerFactoryExtensionPoint, extensionPoint], + ], + features: [ + techInsightsModuleJsonRulesEngineFactCheckerFactory(), + mockServices.logger.factory(), + mockServices.rootConfig.factory({ + data: { + techInsights: { + factChecker: { + checks: { + groupOwnerCheck: { + type: JSON_RULE_ENGINE_CHECK_TYPE, + name: 'Group Owner Check', + description: + 'Verifies that a group has been set as the spec.owner for this entity', + factIds: ['entityOwnershipFactRetriever'], + rule: { + conditions: { + all: [ + { + fact: 'hasGroupOwner', + operator: 'equal', + value: true, + }, + ], + }, + }, + }, + }, + }, + }, + }, + }), + ], + }); + + expect(extensionPoint.setFactCheckerFactory).toHaveBeenCalled(); + }); +}); diff --git a/plugins/tech-insights-backend-module-jsonfc/src/module/techInsightsModuleJsonRulesEngineFactCheckerFactory.ts b/plugins/tech-insights-backend-module-jsonfc/src/module/techInsightsModuleJsonRulesEngineFactCheckerFactory.ts new file mode 100644 index 0000000000..8527b91d9d --- /dev/null +++ b/plugins/tech-insights-backend-module-jsonfc/src/module/techInsightsModuleJsonRulesEngineFactCheckerFactory.ts @@ -0,0 +1,51 @@ +/* + * Copyright 2024 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 { loggerToWinstonLogger } from '@backstage/backend-common'; +import { + coreServices, + createBackendModule, +} from '@backstage/backend-plugin-api'; +import { techInsightsFactCheckerFactoryExtensionPoint } from '@backstage/plugin-tech-insights-node'; +import { JsonRulesEngineFactCheckerFactory } from '../service'; + +/** + * Sets a JsonRulesEngineFactCheckerFactory as FactCheckerFactory + * loading checks from the config. + * + * @public + */ +export const techInsightsModuleJsonRulesEngineFactCheckerFactory = + createBackendModule({ + pluginId: 'tech-insights', + moduleId: 'json-rules-engine-fact-checker-factory', + register(env) { + env.registerInit({ + deps: { + config: coreServices.rootConfig, + logger: coreServices.logger, + techInsights: techInsightsFactCheckerFactoryExtensionPoint, + }, + async init({ config, logger, techInsights }) { + const winstonLogger = loggerToWinstonLogger(logger); + const factory = JsonRulesEngineFactCheckerFactory.fromConfig(config, { + logger: winstonLogger, + }); + techInsights.setFactCheckerFactory(factory); + }, + }); + }, + }); diff --git a/yarn.lock b/yarn.lock index 171c5107b8..db8db76869 100644 --- a/yarn.lock +++ b/yarn.lock @@ -9088,6 +9088,8 @@ __metadata: resolution: "@backstage/plugin-tech-insights-backend-module-jsonfc@workspace:plugins/tech-insights-backend-module-jsonfc" dependencies: "@backstage/backend-common": "workspace:^" + "@backstage/backend-plugin-api": "workspace:^" + "@backstage/backend-test-utils": "workspace:^" "@backstage/cli": "workspace:^" "@backstage/config": "workspace:^" "@backstage/errors": "workspace:^"