feat(tech-insights): add module for 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.

The plugin can be used like

```ts title="packages/backend/src/index.ts"
backend.add(import('@backstage/plugin-tech-insights-backend-module-jsonfc'));
```

Signed-off-by: Patrick Jungermann <Patrick.Jungermann@gmail.com>
This commit is contained in:
Patrick Jungermann
2024-01-09 00:27:58 +01:00
parent 7201af3445
commit 25cfb763be
9 changed files with 175 additions and 2 deletions
+17
View File
@@ -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'));
```
@@ -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<MyCheckType> = // snip
@@ -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)
```
@@ -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": [
@@ -15,6 +15,7 @@
*/
export { JSON_RULE_ENGINE_CHECK_TYPE } from './constants';
export { techInsightsModuleJsonRulesEngineFactCheckerFactory as default } from './module';
export * from './service';
export type {
JsonRuleCheckResponse,
@@ -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';
@@ -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<typeof techInsightsFactCheckerFactoryExtensionPoint.T>;
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();
});
});
@@ -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);
},
});
},
});
+2
View File
@@ -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:^"