-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.js
70 lines (54 loc) · 2.08 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
'use strict';
const hostedGitInfo = require('hosted-git-info');
const fetchers = require('require-directory')(module, './lib', { recurse: false });
const shieldsIo = require('./lib/util/shieldsIo');
function tryBadges(options) {
const coverageBadge = options.badges && options.badges.find((badge) => badge.info.type === 'coverage');
const shieldsIoUrl = coverageBadge && coverageBadge.urls.content;
return shieldsIoUrl ? shieldsIo.request(shieldsIoUrl, options) : Promise.resolve(false);
}
function tryServices(gitInfo, options) {
const errors = [];
const promises = options.services.map((service) =>
fetchers[service] && fetchers[service](gitInfo, options)
.catch((err) => { errors.push(err); })
);
return Promise.all(promises)
.then((coverages) => {
const coverage = coverages.find((coverage) => coverage != null);
if (coverage != null) {
return coverage;
}
if (errors.length) {
throw Object.assign(new Error(`${errors.length} error(s) occurred while trying to fetch coverage`), { errors });
}
return null;
});
}
// -------------------------------------------------------------------------
function fetchCoverage(repositoryUrl, options) {
let gitInfo;
// Grab the git info, aborting immediately if the repository url is not valid
// See: https://github.com/npm/hosted-git-info/issues/15
try {
gitInfo = hostedGitInfo.fromUrl(repositoryUrl);
} catch (err) {
/* istanbul ignore next */
return Promise.resolve(null);
}
if (!gitInfo) {
return Promise.resolve(null);
}
options = {
branch: null,
badges: null,
services: ['codecov', 'coveralls', 'codeclimate', 'scrutinizer'],
got: { timeout: 15000 },
...options,
};
// Try fetching the coverage from the coverage badge if any
return tryBadges(options)
// Fallback to fetching from a variety of services
.then((coverage) => coverage === false ? tryServices(gitInfo, options) : coverage);
}
module.exports = fetchCoverage;