forked from sveltejs/svelte
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtravis-bench.js
115 lines (104 loc) · 3.46 KB
/
travis-bench.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
const path = require('path');
const fs = require('fs');
const childProcess = require('child_process');
const fetch = require('node-fetch');
const username = process.env.BROWSER_USERNAME;
const accessKey = process.env.BROWSER_KEY;
const build = process.env.TRAVIS_BUILD_NUMBER;
const pullRequest = process.env.TRAVIS_PULL_REQUEST;
console.log(username.length, accessKey ? accessKey.length : typeof accessKey);
if (pullRequest === 'false') {
console.log('Benchmark skipped.');
process.exit(0);
}
const outputFile = path.join(process.cwd(), 'tmp', 'output.txt');
const defaultCap = {
'browserstack.user': username,
'browserstack.key': accessKey,
'browserstack.debug': 'true',
build
};
const args = [
`--capabilities=${JSON.stringify([
/* {
browserName: 'safari',
version: '10.0',
platform: 'macOS 10.12',
},
{
browserName: 'internet explorer',
version: '11.103',
platform: 'Windows 10',
},
*/
{
browserName: 'Firefox',
os: 'Windows',
os_version: '10',
},
{
browserName: 'chrome',
os: 'Windows',
os_version: '10',
},
].map(cap => Object.assign(cap, defaultCap)))}`,
`--server=http://hub-cloud.browserstack.com/wd/hub`,
`--custom=${process.cwd()}`,
`--output=${outputFile}`,
`--iterations=15`,
];
try {
childProcess.execFileSync(path.join(__dirname, 'benchmark.sh'), args, {
cwd: process.cwd(),
stdio: 'inherit'
});
} catch (err) {
console.error('An error occurred running the benchmark!');
}
if (!fs.existsSync(outputFile)) {
throw new Error('Benchmark failed.');
}
const githubUsername = 'Svelte-Bot';
const id = 29757693;
const githubToken = process.env.GITHUB_ACCESS_TOKEN;
console.log('GitHub token is of type', typeof githubToken);
const headers = {
'Authorization': `token ${githubToken}`
};
fetch(`https://api.github.com/repos/sveltejs/svelte/issues/${pullRequest}/comments`)
.then(res => res.json())
.then(res => {
let addComment = false;
let editId = null;
console.log('[DEBUG]', res);
if (res.length === 0) {
addComment = true;
} else if (res[res.length - 1].user.id === id) {
addComment = true;
editId = res[res.length - 1].id;
} else {
addComment = true;
}
if (addComment) {
const contents = '<details><summary>Benchmark Results</summary>```' + fs.readFileSync(outputFile).replace(/[\r\n]+/g, '\n') + '```</details>';
let action;
if (editId === null) {
action = fetch(`https://api.github.com/repos/sveltejs/svelte/issues/${pullRequest}/comments`, {
method: 'POST',
headers,
body: JSON.stringify({
body: contents
})
});
} else {
action = fetch(`https://api.github.com/repos/sveltejs/svelte/issues/comments/${editId}`, {
method: 'PATCH',
headers,
body: JSON.stringify({
body: contents
})
});
}
return action.then(res => res.json()).then(res => console.log('[DEBUG]', res));
}
});