Expose catalogFactRetriever & configure in backend

Signed-off-by: Iain Billett <iain@roadie.io>
This commit is contained in:
Iain Billett
2021-11-15 18:27:48 +00:00
parent 5b31874a76
commit d4a3dbb75b
5 changed files with 40 additions and 49 deletions
+5 -36
View File
@@ -17,10 +17,10 @@ import {
createRouter,
buildTechInsightsContext,
createFactRetrieverRegistration,
createCatalogFactRetriever,
} from '@backstage/plugin-tech-insights-backend';
import { Router } from 'express';
import { PluginEnvironment } from '../types';
import { CatalogClient } from '@backstage/catalog-client';
import {
JsonRulesEngineFactCheckerFactory,
JSON_RULE_ENGINE_CHECK_TYPE,
@@ -38,41 +38,10 @@ export default async function createPlugin({
database,
discovery,
factRetrievers: [
createFactRetrieverRegistration('5 4 * * 6', {
// Example cron, At 04:05 on Saturday.
id: 'testRetriever',
version: '1.1.2',
entityFilter: [{ kind: 'component' }], // EntityFilter to be used in the future (creating checks, graphs etc.) to figure out which entities this fact retrieves data for.
schema: {
examplenumberfact: {
type: 'integer',
description: 'Example fact returning a number',
},
},
handler: async _ctx => {
const catalogClient = new CatalogClient({
discoveryApi: discovery,
});
const entities = await catalogClient.getEntities({
filter: [{ kind: 'component' }],
});
return Promise.resolve(
entities.items.map(it => {
return {
entity: {
namespace: it.metadata.namespace!!,
kind: it.kind,
name: it.metadata.name,
},
facts: {
examplenumberfact: 2,
},
};
}),
);
},
}),
createFactRetrieverRegistration(
'* * * * *', // Example cron, every minute
createCatalogFactRetriever(),
),
],
factCheckerFactory: new JsonRulesEngineFactCheckerFactory({
checks: [
@@ -25,3 +25,4 @@ export type {
export type { PersistenceContext } from './service/persistence/persistenceContext';
export { createFactRetrieverRegistration } from './service/fact/createFactRetriever';
export * from './service/fact/factRetrievers';
@@ -14,7 +14,7 @@
* limitations under the License.
*/
import { createEntityFactRetriever } from './entityFactRetriever';
import { createCatalogFactRetriever } from './catalogFactRetriever';
import { Entity, RELATION_OWNED_BY } from '@backstage/catalog-model';
import {
PluginEndpointDiscovery,
@@ -52,7 +52,7 @@ const defaultEntityListResponse: CatalogListResponse<Entity> = {
spec: {
type: 'service',
lifecycle: 'test',
owner: 'group:team-a',
owner: 'team-a',
},
relations: [
{
@@ -106,7 +106,7 @@ const handlerContext = {
config: ConfigReader.fromConfigs([]),
};
const entityFactRetriever = createEntityFactRetriever({
const entityFactRetriever = createCatalogFactRetriever({
annotations: ['backstage.io/techdocs-ref'],
});
@@ -14,18 +14,23 @@
* limitations under the License.
*/
import { FactRetrieverContext } from '@backstage/plugin-tech-insights-node';
import {
FactRetriever,
FactRetrieverContext,
} from '@backstage/plugin-tech-insights-node';
import { CatalogClient } from '@backstage/catalog-client';
import { Entity } from '@backstage/catalog-model';
import isEmpty from 'lodash/isEmpty';
import camelCase from 'lodash/camelCase';
import { get } from 'lodash';
export const createEntityFactRetriever = ({
annotations = [],
}: {
type Options = {
annotations?: string[];
}) => ({
};
export const createCatalogFactRetriever = (
{ annotations = [] }: Options = { annotations: [] },
): FactRetriever => ({
id: 'entityRetriever',
version: '0.0.1',
schema: {
@@ -39,18 +44,18 @@ export const createEntityFactRetriever = ({
},
hasDescription: {
type: 'boolean',
description: 'The entity has an owned_by relation',
description: 'The entity has a description in metadata',
},
hasTags: {
type: 'boolean',
description: 'The entity has tags in metadata',
},
...annotations.reduce((acc: object, it: string) => {
...annotations.reduce((acc: object, annotation: string) => {
return {
...acc,
[camelCase(`hasAnnotation-${it}`)]: {
[camelCase(`hasAnnotation-${annotation}`)]: {
type: 'boolean',
description: `The entity has the annotation: ${it} `,
description: `The entity has the annotation: ${annotation} `,
},
};
}, {}),
@@ -72,7 +77,7 @@ export const createEntityFactRetriever = ({
hasOwner: Boolean(entity.spec?.owner),
hasGroupOwner: Boolean(
entity.spec?.owner &&
(entity.spec?.owner as string).startsWith('group:'),
!(entity.spec?.owner as string).startsWith('user:'),
),
hasDescription: Boolean(entity.metadata?.description),
hasTags: !isEmpty(entity.metadata?.tags),
@@ -0,0 +1,16 @@
/*
* Copyright 2021 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 './catalogFactRetriever';