-
Notifications
You must be signed in to change notification settings - Fork 47
/
index.js
175 lines (131 loc) · 4.84 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
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");
const { TIMEOUT } = require('dns');
var bulk = JSON.parse(core.getInput('bulk'))
var bammVersion = core.getInput('bamm_version')
var bammSdkPath = `${__dirname}/samm-cli-${bammVersion}.jar`;
var added = JSON.parse(core.getInput('added'))
var modified = JSON.parse(core.getInput('modified'))
var prNumber = core.getInput('pr_number')
main()
async function main() {
try {
await asyncBammSdkDownload(`https://github.com/eclipse-esmf/esmf-sdk/releases/download/v${bammVersion}/samm-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))
}
}
}
async function validateChanges(added, modified, prNumber) {
var validationOutput = []
added = added.filter(fileName => fileName.endsWith('.ttl'))
modified = modified.filter(fileName => fileName.endsWith('.ttl'))
await validateAllInputs(added, modified, validationOutput)
console.log(validationOutput)
var output = await produceValidationMarkdown(validationOutput)
writeOutputToFilesystem(JSON.stringify({
comments: output,
prNumber: prNumber
}), 'validation-output.json')
}
function writeOutputToFilesystem(output, filename) {
const archiveDir = "output"
if (!fs.existsSync(archiveDir)){
fs.mkdirSync(archiveDir);
}
fs.writeFileSync(`${archiveDir}/${filename}`, output)
}
function produceValidationMarkdown(validationOutput) {
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(added, modified, validationOutput) {
return Promise.all(
added.concat(modified).map((file) => {
return validateModel(file)
.then(result => {
validationOutput.push(result)
})
})
)
}
async function validateModel(file) {
return new Promise((resolve, reject) => {
console.log(`Validating TTL file ${file}`)
exec(`java -Dpolyglot.engine.WarnInterpreterOnly=false -jar ${bammSdkPath} aspect ${file} validate`, (error, stdout, stderr) => {
if (stderr) {
reject(stderr)
}
resolve({
file: file,
response: 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 SAMM 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 SAMM SDK v${bammVersion}`)
const filePath = fs.createWriteStream(bammSdkPath);
response.pipe(filePath)
filePath.on('finish', () => {
filePath.close()
console.log(`Downloaded SAMM SDK v${bammVersion}`)
resolve(`Downloaded SAMM SDK v${bammVersion}`)
})
}
})
}