-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
These changes add a new workflow that validates new and updated models and adds a comment for each new or updated model of a pull request
- Loading branch information
1 parent
dff87bd
commit 93ef4eb
Showing
6 changed files
with
685 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
name: Validate Semantic Models | ||
description: Validates TTL files with the SDS SDK | ||
inputs: | ||
added: | ||
description: 'The changed files, detected by a previous action' | ||
default: "[]" | ||
required: false | ||
modified: | ||
description: 'The modified files, detected by a previous action' | ||
default: "[]" | ||
required: false | ||
bamm_version: | ||
description: The version of the used BAMM SDK | ||
default: 2.1.1 | ||
required: true | ||
token: | ||
description: GitHub token | ||
required: true | ||
pr_number: | ||
description: Number of the pull request that triggered the action | ||
required: true | ||
runs: | ||
using: node16 | ||
main: index.js |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
const core = require('@actions/core'); | ||
const github = require('@actions/github'); | ||
const https = require('https'); | ||
const fs = require('fs'); | ||
const path = require('path') | ||
const { exec } = require("child_process"); | ||
|
||
|
||
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 bammSdkPath = `${__dirname}/bamm-cli-${bammVersion}.jar`; | ||
var validationOutput = [] | ||
|
||
main() | ||
} catch (error) { | ||
core.setFailed(error.message) | ||
} | ||
|
||
async function main() { | ||
await asyncBammSdkDownload(`https://github.com/eclipse-esmf/esmf-sdk/releases/download/v${bammVersion}/bamm-cli-${bammVersion}.jar`) | ||
|
||
await validateAllInputs() | ||
|
||
console.log(validationOutput) | ||
|
||
var output = await produceValidationMarkdown() | ||
|
||
writeOutputToFilesystem({ | ||
comments: output, | ||
prNumber: prNumber | ||
}) | ||
} | ||
|
||
function writeOutputToFilesystem(output) { | ||
const archiveDir = "output" | ||
if (!fs.existsSync(archiveDir)){ | ||
fs.mkdirSync(archiveDir); | ||
} | ||
|
||
fs.writeFileSync(`${archiveDir}/validation-output.json`, JSON.stringify(output)) | ||
} | ||
|
||
function produceValidationMarkdown() { | ||
return Promise.all(validationOutput.map(async (model) => { | ||
var lines = model.response.split('\n') | ||
|
||
var firstLine = lines[0] | ||
|
||
lines.splice(0,2) | ||
lines.splice(-3) | ||
|
||
var remainingLines = lines.join('\n') | ||
|
||
var body = `### Validation Report for ${model.file} | ||
#### ${firstLine}` | ||
|
||
if(remainingLines !== '') { | ||
body = body + `\n\`\`\`ttl\n${remainingLines}\n\`\`\`` | ||
} | ||
|
||
return body | ||
})) | ||
} | ||
|
||
async function validateAllInputs() { | ||
return Promise.all( | ||
added.concat(modified).map((file) => { | ||
return validateModel(file) | ||
.then(result => { | ||
validationOutput.push({ | ||
file: file, | ||
response: result | ||
}) | ||
}) | ||
}) | ||
) | ||
} | ||
|
||
async function validateModel(file) { | ||
return new Promise((resolve, reject) => { | ||
if (path.extname(file) === ".ttl") { | ||
console.log(`Validating TTL file ${path.basename(file)}`) | ||
|
||
exec(`java -jar ${bammSdkPath} aspect ${file} validate`, (error, stdout, stderr) => { | ||
if (stderr) { | ||
reject(stderr) | ||
} | ||
|
||
resolve(stdout) | ||
}) | ||
} | ||
}) | ||
|
||
} | ||
|
||
async function asyncBammSdkDownload(url) { | ||
return new Promise((resolve, reject) => { | ||
downloadBammSdk(url, resolve, reject) | ||
}) | ||
} | ||
|
||
async function downloadBammSdk(url, resolve, reject) { | ||
https.get(url, (response) => { | ||
if (response.statusCode >= 400) { | ||
reject("Could not download BAMM SDK v${bammVersion}") | ||
} | ||
|
||
if (response.statusCode > 300 && response.statusCode < 400 && !!response.headers.location) { | ||
downloadBammSdk(response.headers.location, resolve, reject) | ||
} else { | ||
console.log(`Starting download of BAMM SDK v${bammVersion}`) | ||
|
||
const filePath = fs.createWriteStream(bammSdkPath); | ||
|
||
response.pipe(filePath) | ||
filePath.on('finish', () => { | ||
filePath.close() | ||
console.log(`Downloaded BAMM SDK v${bammVersion}`) | ||
resolve(`Downloaded BAMM SDK v${bammVersion}`) | ||
}) | ||
} | ||
}) | ||
} |
Oops, something went wrong.