Skip to content

Commit

Permalink
Update dependencies (#294)
Browse files Browse the repository at this point in the history
* Update dependencies

* Bumping packages.

Upping template validator to match newest.

Co-authored-by: Irma12 <[email protected]>
Co-authored-by: Jussi Hallila <[email protected]>
  • Loading branch information
3 people authored Nov 26, 2021
1 parent 79af21c commit e3267f8
Show file tree
Hide file tree
Showing 4 changed files with 183 additions and 205 deletions.
2 changes: 1 addition & 1 deletion packages/backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"@backstage/integration": "^0.6.2",
"@backstage/plugin-app-backend": "^0.3.13",
"@backstage/plugin-auth-backend": "^0.4.0",
"@backstage/plugin-catalog-backend": "^0.17.0",
"@backstage/plugin-catalog-backend": "^0.18.0",
"@backstage/plugin-proxy-backend": "^0.2.8",
"@backstage/plugin-scaffolder-backend": "^0.15.1",
"@backstage/plugin-techdocs-backend": "^0.10.0",
Expand Down
4 changes: 2 additions & 2 deletions utils/roadie-backstage-entity-validator/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@roadiehq/roadie-backstage-entity-validator",
"version": "1.0.1",
"version": "1.2.0",
"author": "RoadieHQ",
"description": "Backstage entity validator library",
"main": "src/index.js",
Expand Down Expand Up @@ -28,7 +28,7 @@
},
"dependencies": {
"@actions/core": "^1.2.7",
"@backstage/catalog-model": "^0.7.8",
"@backstage/catalog-model": "^0.9.7",
"ajv": "^8.4.0",
"ajv-formats": "^2.1.0",
"glob": "^7.1.7",
Expand Down
90 changes: 49 additions & 41 deletions utils/roadie-backstage-entity-validator/src/validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,103 +10,111 @@ import {
componentEntityV1alpha1Validator,
groupEntityV1alpha1Validator,
locationEntityV1alpha1Validator,
templateEntityV1alpha1Validator,
templateEntityV1beta2Validator,
userEntityV1alpha1Validator,
} from '@backstage/catalog-model';
import annotationSchema from './schemas/annotations.schema.json';
import Ajv from 'ajv';

const ajv = new Ajv({verbose: true})
require('ajv-formats')(ajv)
const ajv = new Ajv({ verbose: true });
require('ajv-formats')(ajv);

const VALIDATORS = {
api: apiEntityV1alpha1Validator,
component: componentEntityV1alpha1Validator,
group: groupEntityV1alpha1Validator,
location: locationEntityV1alpha1Validator,
template: templateEntityV1alpha1Validator,
template: templateEntityV1beta2Validator,
user: userEntityV1alpha1Validator,
}
};

function modifyPlaceholders(obj) {
for (const k in obj) {
if (typeof obj[k] === "object") {
if(obj[k].$text){
obj[k] = "DUMMY TEXT"
if (typeof obj[k] === 'object') {
if (obj[k].$text) {
obj[k] = 'DUMMY TEXT';
return;
}
modifyPlaceholders(obj[k])
modifyPlaceholders(obj[k]);
}
}
}

export const validate = async (fileContents, verbose = true) => {
let validator
let validator;
const validateAnnotations = (entity, idx) => {
if (!validator) {
validator = ajv.compile(annotationSchema);
}
if (verbose) {
console.log(`Validating entity annotations for file document ${idx}`);
}
const result = validator(entity)
const result = validator(entity);
if (result === true) {
return true
return true;
}

const [error] = validator.errors || []
const [error] = validator.errors || [];
if (!error) {
throw new Error(`Malformed annotation, Unknown error`)
throw new Error(`Malformed annotation, Unknown error`);
}

throw new Error(
`Malformed annotation, ${error.instancePath || '<root>'} ${error.message}`,
)

}
`Malformed annotation, ${error.instancePath || '<root>'} ${
error.message
}`,
);
};

try {
const data = yaml.loadAll(fileContents)
const data = yaml.loadAll(fileContents);
data.forEach(it => {
modifyPlaceholders(it)
})
modifyPlaceholders(it);
});
const entityPolicies = EntityPolicies.allOf([
new DefaultNamespaceEntityPolicy(),
new FieldFormatEntityPolicy(),
new NoForeignRootFieldsEntityPolicy(),
new SchemaValidEntityPolicy()]
)
const responses = await Promise.all(data.map((it) => {
return entityPolicies.enforce(it)
}))
const validateEntityKind = async (entity) => {
const results = {}
new SchemaValidEntityPolicy(),
]);
const responses = await Promise.all(
data.map(it => {
return entityPolicies.enforce(it);
}),
);
const validateEntityKind = async entity => {
const results = {};
for (const v of Object.entries(VALIDATORS)) {
const result = await v[1].check(entity)
results[v[0]] = result
const result = await v[1].check(entity);
results[v[0]] = result;
if (result === true && verbose) {
console.log(`Validated entity kind '${v[0]}' successfully.`);
}
}
return results
}
return results;
};
const validateEntities = async entities => {
const results = await Promise.all(entities.map(validateEntityKind))
return Object.values(results[0]).filter((r) => r === false).length > 0
}
const validKind = await validateEntities(data)
const validAnnotations = data.map((it, idx) => validateAnnotations(it, idx))
const results = await Promise.all(entities.map(validateEntityKind));
return Object.values(results[0]).filter(r => r === false).length > 0;
};
const validKind = await validateEntities(data);
const validAnnotations = data.map((it, idx) =>
validateAnnotations(it, idx),
);

if (validKind && validAnnotations && verbose) {
console.log('Entity Schema policies validated\n')
responses.forEach(it => console.log(yaml.dump(it)))
console.log('Entity Schema policies validated\n');
responses.forEach(it => console.log(yaml.dump(it)));
}
} catch (e) {
throw new Error(e)
throw new Error(e);
}
};

export const validateFromFile = async (filepath = './sample/catalog-info.yml', verbose = true) => {
export const validateFromFile = async (
filepath = './sample/catalog-info.yml',
verbose = true,
) => {
const fileContents = fs.readFileSync(filepath, 'utf8');
if (verbose) {
console.log(`Validating Entity Schema policies for file ${filepath}`);
Expand Down
Loading

0 comments on commit e3267f8

Please sign in to comment.