Use output file instead of parsing stdout

pull/1786/head
Paul Sauve 8 years ago
parent 0787e51398
commit 735bb0fe10

@ -1,4 +1,5 @@
const path = require('path'); const path = require('path');
const fs = require('fs');
const childProcess = require('child_process'); const childProcess = require('child_process');
const fetch = require('node-fetch'); const fetch = require('node-fetch');
@ -14,6 +15,8 @@ if (pullRequest === 'false') {
process.exit(0); process.exit(0);
} }
const outputFile = path.join(process.cwd(), 'tmp', 'output.txt');
const args = [ const args = [
`--capabilities=${JSON.stringify([ `--capabilities=${JSON.stringify([
/* { /* {
@ -59,13 +62,12 @@ const args = [
}, },
])}`, ])}`,
`--server=http://${username}:${accessKey}@ondemand.saucelabs.com/wd/hub`, `--server=http://${username}:${accessKey}@ondemand.saucelabs.com/wd/hub`,
`--custom=${process.cwd()}` `--custom=${process.cwd()}`,
`--output=${outputFile}`,
]; ];
let stdout;
try { try {
stdout = childProcess.execFileSync(path.join(__dirname, 'benchmark.sh'), args, { childProcess.execFileSync(path.join(__dirname, 'benchmark.sh'), args, {
cwd: process.cwd(), cwd: process.cwd(),
stdio: 'inherit' stdio: 'inherit'
}); });
@ -73,74 +75,44 @@ try {
console.error('An error occurred running the benchmark!'); console.error('An error occurred running the benchmark!');
} }
if (typeof stdout !== 'string') { const githubUsername = 'Svelte-Bot';
stdout = stdout.toString('utf8'); const id = 29757693;
} const githubToken = process.env.GITHUB_ACCESS_TOKEN;
const outputs = [];
let index = 0;
let started = false;
// parse the output to find what we want fetch(`https://${githubUsername}:${githubToken}@api.github.com/repos/sveltejs/svelte/issues/${pullRequest}/comments`)
const split = stdout.split('\n'); .then(res => res.json())
split.forEach((line, lineNum) => { .then(res => {
if (started) { let addComment = false;
if (line.indexOf('Running capability') === 0 || line.indexOf('Took ') === 0) { let editId = null;
started = false;
index++;
} else {
if (outputs.length <= index) {
outputs[index] = [];
}
outputs[index].push(line);
}
} else if (lineNum > 0 && line === '' && split[lineNum - 1].indexOf('Testing ') === 0) {
started = true;
}
});
if (pullRequest !== 'false') { if (res.length === 0) {
const githubUsername = 'Svelte-Bot'; addComment = true;
const id = 29757693; } else if (res[res.length - 1].user.id !== id) {
const githubToken = process.env.GITHUB_ACCESS_TOKEN; addComment = true;
fetch(`https://${githubUsername}:${githubToken}@api.github.com/repos/sveltejs/svelte/issues/${pullRequest}/comments`) res.forEach(reply => {
.then(res => res.json()) if (reply.user.id === id) {
.then(res => { editId = reply.id;
let addComment = false; }
let editId = null; });
}
if (res.length === 0) {
addComment = true;
} else if (res[res.length - 1].user.id !== id) {
addComment = true;
res.forEach(reply => { if (addComment) {
if (reply.user.id === id) { const contents = '<details><summary>Benchmark Results</summary>' + fs.readFileSync(outputFile) + '</details>';
editId = reply.id; if (editId === null) {
} return fetch(`https://${githubUsername}:${githubToken}@api.github.com/repos/sveltejs/svelte/issues/${pullRequest}/comments`, {
method: 'POST',
body: JSON.stringify({
body: contents
})
});
} else {
return fetch(`https://${githubUsername}:${githubToken}@api.github.com/repos/sveltejs/svelte/issues/comments/${editId}`, {
method: 'PATCH',
body: JSON.stringify({
body: contents
})
}); });
} }
}
if (addComment) { });
const contents = '<details><summary>Benchmark Results</summary>' +
outputs.map(output => output.join('\n')).join('\n----------------------------------------------------------------------\n') +
'</details>';
if (editId === null) {
return fetch(`https://${githubUsername}:${githubToken}@api.github.com/repos/sveltejs/svelte/issues/${pullRequest}/comments`, {
method: 'POST',
body: JSON.stringify({
body: contents
})
});
} else {
return fetch(`https://${githubUsername}:${githubToken}@api.github.com/repos/sveltejs/svelte/issues/comments/${editId}`, {
method: 'PATCH',
body: JSON.stringify({
body: contents
})
});
}
}
});
}

Loading…
Cancel
Save