-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
78 lines (64 loc) · 2.26 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
#!/usr/bin/env node
var program = require('commander');
var fs = require('fs');
var RepositoryInspector = require('./src/repository-inspector');
var path = require("path");
var url = require("url");
var http = require("https");
var fn;
program
.version('0.1.0')
.arguments('<coverage-file>')
.option('--access-token <token>')
.option('--api-url <url>', 'The base URL of the API.', 'https://scrutinizer-ci.com/api')
.option('--repository', 'The qualified repository name of your repository (GitHub: g/login/username; Bitbucket: b/login/username).')
.option('--revision', 'The revision that the code coverage information belongs to (defaults to git rev-parse HEAD).')
.option('--parent', 'The parent revision of the current revision.', [])
.action(function (coverageFile) {
fn = coverageFile;
})
.parse(process.argv);
if (!fn) {
console.error('No file specified');
process.exit(1);
}
var getBasePath = function () {
var dir = process.cwd();
while (dir) {
if (fs.existsSync(dir+'/.git')) {
return dir;
}
dir = path.dirname(dir);
}
throw new Error('Cannot determine base path');
};
var getCoverageData = function (filename) {
var outString = fs.readFileSync(filename).toString();
outString = outString.replace(new RegExp(getBasePath(), 'g'), '{scrutinizer_project_base_path}');
return new Buffer(outString).toString('base64');
};
var inspector = new RepositoryInspector(process.cwd());
var baseUrl = program.apiUrl;
var repositoryName = program.repository || inspector.getQualifiedName();
var data = {
'revision': program.revision || inspector.getCurrentRevision(),
'parents': program.parent || inspector.getCurrentParents(),
'coverage': {
'format': 'clover',
'data': getCoverageData(fn)
}
};
var options = url.parse(baseUrl+'/repositories/'+repositoryName+'/data/code-coverage'+(program.accessToken ? '?access_token='+program.accessToken : ''));
options.method = 'POST';
options.headers = {
'Content-Type': 'application/json'
};
var req = http.request(options, function (res) {
if (200 === res.statusCode) {
console.log('Upload went fine.')
} else {
console.log('Upload failed.')
}
});
req.write(JSON.stringify(data));
req.end();