Skip to content

Commit

Permalink
chore: make the code simpler and do not emit errors
Browse files Browse the repository at this point in the history
  • Loading branch information
xNok committed Nov 11, 2024
1 parent 3503f17 commit 05e63be
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 36 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import {Entity} from "@backstage/catalog-model";
import {CatalogOPAEntityValidator} from "./CatalogOPAEntityValidator";
import {mockServices} from "@backstage/backend-test-utils";
import {LocationSpec} from "@backstage/plugin-catalog-common";
import {OpaResult} from "../service/entityCheckerApi";

describe('CatalogOPAEntityValidator', () => {
Expand All @@ -14,13 +12,6 @@ describe('CatalogOPAEntityValidator', () => {
},
};

const location: LocationSpec = {
type: 'url',
target:
'https://example.com',
};

const mockLogger = mockServices.logger.mock();
const mockEntityCheckerApi = {
checkEntity: jest.fn((): Promise<OpaResult> => Promise.resolve(
{
Expand All @@ -36,11 +27,10 @@ describe('CatalogOPAEntityValidator', () => {
}
))
};
const mockCatalogProcessorEmit = jest.fn()

const processor = new CatalogOPAEntityValidator(mockEntityCheckerApi, mockLogger)
const processor = new CatalogOPAEntityValidator(mockEntityCheckerApi)

expect(await processor.preProcessEntity(entity, location, mockCatalogProcessorEmit)).toEqual({
expect(await processor.preProcessEntity(entity)).toEqual({
apiVersion: 'backstage.io/v1alpha1',
kind: 'Component',
metadata: {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
import { CatalogProcessor, CatalogProcessorEmit, processingResult } from '@backstage/plugin-catalog-node';
import { CatalogProcessor } from '@backstage/plugin-catalog-node';
import {Entity} from "@backstage/catalog-model";
import { LocationSpec } from '@backstage/plugin-catalog-common'
import {EntityCheckerApi} from "../service/entityCheckerApi";
import {LoggerService} from "@backstage/backend-plugin-api";
import { merge } from 'lodash';

const OPA_ENTITY_CHECKER_GOOD_ENTITY_ANNOTATION = "entity-checker.opa/good-entity"

export class CatalogOPAEntityValidator implements CatalogProcessor {
constructor(
private readonly api: EntityCheckerApi,
private readonly logger: LoggerService
) {}

getProcessorName(): string {
Expand All @@ -19,35 +16,17 @@ export class CatalogOPAEntityValidator implements CatalogProcessor {

async preProcessEntity(
entity: Entity,
location: LocationSpec,
emit: CatalogProcessorEmit,
): Promise<Entity> {

let isGoodEntity = true

await this.api.checkEntity({
let opaResult = await this.api.checkEntity({
entityMetadata: JSON.stringify(entity)
}).then(data => {
this.logger.debug(JSON.stringify(data))

if (data.result) {
data.result.forEach((e) => {
emit(processingResult.inputError(location, e.message.toString()))
})

isGoodEntity = false
}
})

if (isGoodEntity) {
return entity;
}

return merge(
{
metadata: {
annotations: {
[OPA_ENTITY_CHECKER_GOOD_ENTITY_ANNOTATION]: "false"
[OPA_ENTITY_CHECKER_GOOD_ENTITY_ANNOTATION]: opaResult.good_entity.toString()
}
}
},
Expand Down

0 comments on commit 05e63be

Please sign in to comment.