-
Notifications
You must be signed in to change notification settings - Fork 20
/
index.js
66 lines (55 loc) · 1.7 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
const clang_tools_bin_dir = require('clang-tools-prebuilt');
const path = require('path');
const proc = require('child_process');
const tmp = require('tmp');
const { GITHUB_EVENT_PATH, GITHUB_TOKEN, GITHUB_WORKSPACE } = process.env;
const event = require(GITHUB_EVENT_PATH);
const { repository } = event;
const {
owner: { login: owner }
} = repository;
const { name: repo } = repository;
const Octokit = require('@octokit/rest');
async function runClangTidy(files) {
var tmpFile = tmp.fileSync();
const tools_bin_path = path.join(clang_tools_bin_dir, 'clang-tidy');
const args = process.argv.slice(2)
.concat('-export-fixes=' + tmpFile.name)
.concat(files);
const child = proc.spawnSync(tools_bin_path, args, {
stdio: 'inherit',
cwd: GITHUB_WORKSPACE
});
return child.status;
}
async function run() {
if (!event.pull_request) {
throw new Error(`Clang-Tidy Action currently only supports pull requests`);
}
const octokit = new Octokit({
auth: GITHUB_TOKEN
});
const response = await octokit.pulls.listFiles({
owner,
repo,
pull_number: event.pull_request.number,
page: 0,
per_page: 300
});
const files = response.data;
/* regex for c, cc, h, hpp, etc... */
const pattern = /.*.[c|h](p{2})?/;
const filenames = files.map(file => file.filename)
.filter(filename => filename.match(pattern));
if (filenames.length == 0) {
console.log("No C/C++ files changed...");
process.exit(78);
}
const exitcode = await runClangTidy(files);
process.exit(exitcode);
}
run()
.catch(err => {
console.error(err);
process.exit(1);
});