Skip to content

Commit

Permalink
Add bulk model validation
Browse files Browse the repository at this point in the history
These changes add another GitHub Actions workflow that allow the user to validate all models in one repository at once.
  • Loading branch information
LuLeRoemer committed Feb 2, 2023
1 parent 5efb4e6 commit 9185dbc
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 26 deletions.
4 changes: 4 additions & 0 deletions .github/actions/model-validation/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ inputs:
pr_number:
description: Number of the pull request that triggered the action
required: true
bulk:
description: Flag that defines whether all files on a branch should be validated
required: false
default: "false"
runs:
using: node16
main: index.js
98 changes: 72 additions & 26 deletions .github/actions/model-validation/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,47 +4,93 @@ const https = require('https');
const fs = require('fs');
const path = require('path')
const { exec } = require("child_process");
const { TIMEOUT } = require('dns');


try {
var added = JSON.parse(core.getInput('added'))
var modified = JSON.parse(core.getInput('modified'))
var bammVersion = core.getInput('bamm_version')
var prNumber = core.getInput('pr_number')
var bulk = JSON.parse(core.getInput('bulk'))
var bammVersion = core.getInput('bamm_version')
var bammSdkPath = `${__dirname}/bamm-cli-${bammVersion}.jar`;

var bammSdkPath = `${__dirname}/bamm-cli-${bammVersion}.jar`;
var validationOutput = []
var added = JSON.parse(core.getInput('added'))
var modified = JSON.parse(core.getInput('modified'))
var prNumber = core.getInput('pr_number')

main()
} catch (error) {
core.setFailed(error.message)
}
main()

async function main() {
await asyncBammSdkDownload(`https://github.com/eclipse-esmf/esmf-sdk/releases/download/v${bammVersion}/bamm-cli-${bammVersion}.jar`)
try {
await asyncBammSdkDownload(`https://github.com/eclipse-esmf/esmf-sdk/releases/download/v${bammVersion}/bamm-cli-${bammVersion}.jar`)

if(bulk === false){
validateChanges(added, modified, prNumber)
} else {
validateAllModels()
}
} catch (error) {
core.setFailed(error.message)
}
}

async function validateAllModels() {
var validationResultPromises = []

gatherValidationResults('./', '.ttl', validationResultPromises)

var validationResults = await Promise.all(validationResultPromises)

var formattedOutput = await produceValidationMarkdown(validationResults)

var fullReport = `# Validation report for all models
`

formattedOutput.forEach((report) => {
fullReport += '\n\n' + report + '\n'
})

writeOutputToFilesystem(fullReport, 'full-validation-report.md')
}

async function gatherValidationResults(startPath, fileEnding, returnValue) {
var allFiles = fs.readdirSync(startPath)

for (file of allFiles) {
var fileName = path.join(startPath, file)

if(fs.lstatSync(fileName).isDirectory()) {
gatherValidationResults(fileName, fileEnding, returnValue)
} else if(fileName.endsWith(fileEnding)) {
returnValue.push(validateModel(fileName))
}
}
}

await validateAllInputs()

async function validateChanges(added, modified, prNumber) {
var validationOutput = []

await validateAllInputs(added, modified, validationOutput)

console.log(validationOutput)

var output = await produceValidationMarkdown()
var output = await produceValidationMarkdown(validationOutput)

writeOutputToFilesystem({
writeOutputToFilesystem(JSON.stringify({
comments: output,
prNumber: prNumber
})
}), 'validation-output.json')
}

function writeOutputToFilesystem(output) {
function writeOutputToFilesystem(output, filename) {
const archiveDir = "output"
if (!fs.existsSync(archiveDir)){
fs.mkdirSync(archiveDir);
}

fs.writeFileSync(`${archiveDir}/validation-output.json`, JSON.stringify(output))
fs.writeFileSync(`${archiveDir}/${filename}`, output)
}

function produceValidationMarkdown() {
function produceValidationMarkdown(validationOutput) {
return Promise.all(validationOutput.map(async (model) => {
var lines = model.response.split('\n')

Expand All @@ -67,15 +113,12 @@ function produceValidationMarkdown() {
}))
}

async function validateAllInputs() {
async function validateAllInputs(added, modified, validationOutput) {
return Promise.all(
added.concat(modified).map((file) => {
return validateModel(file)
.then(result => {
validationOutput.push({
file: file,
response: result
})
validationOutput.push(result)
})
})
)
Expand All @@ -84,14 +127,17 @@ async function validateAllInputs() {
async function validateModel(file) {
return new Promise((resolve, reject) => {
if (path.extname(file) === ".ttl") {
console.log(`Validating TTL file ${path.basename(file)}`)
console.log(`Validating TTL file ${file}`)

exec(`java -jar ${bammSdkPath} aspect ${file} validate`, (error, stdout, stderr) => {
if (stderr) {
reject(stderr)
}

resolve(stdout)
resolve({
file: file,
response: stdout
})
})
}
})
Expand Down
37 changes: 37 additions & 0 deletions .github/workflows/bulk-validation.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#
# Copyright (c) 2023 Robert Bosch Manufacturing Solutions GmbH
#
# See the AUTHORS file(s) distributed with this work for additional
# information regarding authorship.
#
# See the LICENSE file(s) distributed with this work for
# additional information regarding license terms.
#

name: Bulk Validation
on:
workflow_dispatch:

jobs:
validate:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
- uses: actions/setup-java@v3
with:
distribution: 'temurin'
java-version: '17'
- name: Install Node dependencies
working-directory: ./.github/actions/model-validation
run: npm install
- name: validate models
uses: ./.github/actions/model-validation
with:
bamm_version: 2.1.1
bulk: true
- name: Archive
uses: actions/upload-artifact@v3
with:
name: full-validation-report
path: output

0 comments on commit 9185dbc

Please sign in to comment.