Skip to content

Commit

Permalink
[semver:patch] handle text replacement (#9)
Browse files Browse the repository at this point in the history
* Handle cases where replacement text is referenced in catalog-info.yaml
  • Loading branch information
Xantier authored May 28, 2021
1 parent b92b6eb commit 285c038
Show file tree
Hide file tree
Showing 9 changed files with 3,158 additions and 48 deletions.
8 changes: 2 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,9 @@ This package can be used as a GitHub action or a standalone node.js module

None. Prints out the validated YAML on success. Prints out errors on invalid YAML

The time we greeted you.

### Example usage
```
- uses: RoadieHQ/[email protected].5
- uses: RoadieHQ/[email protected].6
with:
path: 'catalog-info-1.yaml'
```
Expand All @@ -36,16 +34,14 @@ The time we greeted you.

None. Prints out the validated YAML on success. Prints out errors on invalid YAML

The time we greeted you.

### Example config
```
description: >
Sample catalog-info.yaml validation
usage:
version: 2.1
orbs:
entity-validator: "roadiehq/[email protected].5"
entity-validator: "roadiehq/[email protected].6"
workflows:
use-entity-validator:
jobs:
Expand Down
37 changes: 27 additions & 10 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -47145,10 +47145,22 @@ const VALIDATORS = {
user: userEntityV1alpha1Validator,
}

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

exports.validate = async (filepath = './sample/catalog-info.yml') => {
const validator = ajv.compile(annotationSchema)
const validateAnnotations = (entity) => {
console.log('Validating entity annotations')
const validateAnnotations = (entity, idx) => {
console.log(`Validating entity annotations for file ${filepath}, document ${idx}`)
const result = validator(entity)
if (result === true) {
return true
Expand All @@ -47165,25 +47177,29 @@ exports.validate = async (filepath = './sample/catalog-info.yml') => {

}

console.log(`Validating Entity Schema for file ${filepath}\n`)
try {
const fileContents = fs.readFileSync(filepath, 'utf8')
const data = yaml.load(fileContents)
const data = yaml.loadAll(fileContents)
data.forEach(it => {
modifyPlaceholders(it)
})
const entityPolicies = EntityPolicies.allOf([
new DefaultNamespaceEntityPolicy(),
new FieldFormatEntityPolicy(),
new NoForeignRootFieldsEntityPolicy(),
new SchemaValidEntityPolicy()]
)
const respo = await entityPolicies.enforce(data)
console.log('Running file through validators\n')
const responses = await Promise.all(data.map((it, idx) => {
console.log(`Validating Entity Schema policies for file ${filepath}, document ${idx}`)
return entityPolicies.enforce(it)
}))
const validateEntityKind = async (entity) => {
const results = {}
for (const validator of Object.entries(VALIDATORS)) {
const result = await validator[1].check(entity)
results[validator[0]] = result
if (result === true) {
console.log(`Validated entity kind '${validator[0]}' successfully.\n`)
console.log(`Validated entity kind '${validator[0]}' successfully.`)
}
}
return results
Expand All @@ -47192,11 +47208,12 @@ exports.validate = async (filepath = './sample/catalog-info.yml') => {
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 = validateAnnotations(data)
const validKind = await validateEntities(data)
const validAnnotations = data.map((it, idx) => validateAnnotations(it, idx))

if (validKind && validAnnotations) {
console.log('Entity Schema policy validated\n', yaml.dump(respo))
console.log('Entity Schema policies validated\n')
responses.forEach(it => console.log(yaml.dump(it)))
}
} catch (e) {
core.setFailed(`Action failed with error ${e}`)
Expand Down
2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

Loading

0 comments on commit 285c038

Please sign in to comment.