UNPKG

5.92 kBJavaScriptView Raw
1var fs = require('fs');
2var path = require('path');
3var yaml = require('js-yaml');
4var index = require('../index');
5var logger = require('./logger')();
6var fetchGitData = require('./fetchGitData');
7
8var getBaseOptions = function(cb){
9 var options = {};
10 var git_commit = process.env.COVERALLS_GIT_COMMIT;
11 var git_branch = process.env.COVERALLS_GIT_BRANCH;
12 var git_committer_name, git_committer_email, git_message;
13
14 var match = (process.env.CI_PULL_REQUEST || "").match(/(\d+)$/);
15
16 if (match) {
17 options.service_pull_request = match[1];
18 }
19
20 if (process.env.TRAVIS){
21 options.service_name = 'travis-ci';
22 options.service_job_id = process.env.TRAVIS_JOB_ID;
23 options.service_pull_request = process.env.TRAVIS_PULL_REQUEST;
24 git_commit = 'HEAD';
25 git_branch = process.env.TRAVIS_BRANCH;
26 }
27
28
29 if (process.env.DRONE){
30 options.service_name = 'drone';
31 options.service_job_id = process.env.DRONE_BUILD_NUMBER;
32 options.service_pull_request = process.env.DRONE_PULL_REQUEST;
33 git_committer_name = process.env.DRONE_COMMIT_AUTHOR;
34 git_committer_email = process.env.DRONE_COMMIT_AUTHOR_EMAIL;
35 git_commit = process.env.DRONE_COMMIT;
36 git_branch = process.env.DRONE_BRANCH;
37 git_message = process.env.DRONE_COMMIT_MESSAGE;
38 }
39
40 if (process.env.JENKINS_URL || process.env.JENKINS_HOME){
41 options.service_name = 'jenkins';
42 options.service_job_id = process.env.BUILD_ID;
43 options.service_pull_request = process.env.ghprbPullId;
44 git_commit = process.env.GIT_COMMIT;
45 git_branch = process.env.GIT_BRANCH || process.env.BRANCH_NAME;
46 }
47
48 if (process.env.CIRCLECI){
49 options.service_name = 'circleci';
50 options.service_job_id = process.env.CIRCLE_BUILD_NUM;
51
52 if (process.env.CI_PULL_REQUEST) {
53 var pr = process.env.CI_PULL_REQUEST.split('/pull/');
54 options.service_pull_request = pr[1];
55 }
56 git_commit = process.env.CIRCLE_SHA1;
57 git_branch = process.env.CIRCLE_BRANCH;
58 }
59
60 if (process.env.CI_NAME && process.env.CI_NAME === 'codeship'){
61 options.service_name = 'codeship';
62 options.service_job_id = process.env.CI_BUILD_NUMBER;
63 git_commit = process.env.CI_COMMIT_ID;
64 git_branch = process.env.CI_BRANCH;
65 git_committer_name = process.env.CI_COMMITTER_NAME;
66 git_committer_email = process.env.CI_COMMITTER_EMAIL;
67 git_message = process.env.CI_COMMIT_MESSAGE;
68 }
69
70 if (process.env.WERCKER){
71 options.service_name = 'wercker';
72 options.service_job_id = process.env.WERCKER_BUILD_ID;
73 git_commit = process.env.WERCKER_GIT_COMMIT;
74 git_branch = process.env.WERCKER_GIT_BRANCH;
75 }
76
77 if (process.env.GITLAB_CI){
78 options.service_name = 'gitlab-ci';
79 options.service_job_number = process.env.CI_BUILD_NAME;
80 options.service_job_id = process.env.CI_BUILD_ID;
81 git_commit = process.env.CI_BUILD_REF;
82 git_branch = process.env.CI_BUILD_REF_NAME;
83 }
84 if(process.env.APPVEYOR){
85 options.service_name = 'appveyor';
86 options.service_job_number = process.env.APPVEYOR_BUILD_NUMBER;
87 options.service_job_id = process.env.APPVEYOR_BUILD_ID;
88 git_commit = process.env.APPVEYOR_REPO_COMMIT;
89 git_branch = process.env.APPVEYOR_REPO_BRANCH;
90 }
91 if(process.env.SURF_SHA1){
92 options.service_name = 'surf';
93 git_commit = process.env.SURF_SHA1;
94 git_branch = process.env.SURF_REF;
95 }
96 options.run_at = process.env.COVERALLS_RUN_AT || JSON.stringify(new Date()).slice(1, -1);
97 if (process.env.COVERALLS_SERVICE_NAME){
98 options.service_name = process.env.COVERALLS_SERVICE_NAME;
99 }
100 if (process.env.COVERALLS_SERVICE_JOB_ID){
101 options.service_job_id = process.env.COVERALLS_SERVICE_JOB_ID;
102 }
103
104 if (!git_commit || !git_branch) {
105 var data = require('./detectLocalGit')();
106 if (data) {
107 git_commit = git_commit || data.git_commit;
108 git_branch = git_branch || data.git_branch;
109 }
110 }
111
112 if (process.env.COVERALLS_PARALLEL) {
113 options.parallel = true;
114 }
115
116 // try to get the repo token as an environment variable
117 if (process.env.COVERALLS_REPO_TOKEN) {
118 options.repo_token = process.env.COVERALLS_REPO_TOKEN;
119 } else {
120 // try to get the repo token from a .coveralls.yml file
121 var yml = path.join(process.cwd(), '.coveralls.yml');
122 try {
123 if (fs.statSync(yml).isFile()) {
124 var coveralls_yaml_conf = yaml.safeLoad(fs.readFileSync(yml, 'utf8'));
125 options.repo_token = coveralls_yaml_conf.repo_token;
126 if(coveralls_yaml_conf.service_name) {
127 options.service_name = coveralls_yaml_conf.service_name;
128 }
129 }
130 } catch(ex){
131 logger.warn("Repo token could not be determined. Continuing without it." +
132 "This is necessary for private repos only, so may not be an issue at all.");
133 }
134 }
135
136 if (git_commit){
137 fetchGitData({
138 head: {
139 id: git_commit,
140 committer_name: git_committer_name,
141 committer_email: git_committer_email,
142 message: git_message
143 },
144 branch: git_branch
145 }, function(err, git){
146 if (err){
147 logger.warn('there was an error getting git data: ', err);
148 } else {
149 options.git = git;
150 }
151 return cb(err, options);
152 });
153 } else {
154 return cb(null, options);
155 }
156};
157
158var getOptions = function(cb, _userOptions){
159 if (!cb){
160 throw new Error('getOptions requires a callback');
161 }
162
163 var userOptions = _userOptions || {};
164
165 getBaseOptions(function(err, options){
166 // minimist populates options._ with non-option command line arguments
167 var firstNonOptionArgument = index.options._[0];
168
169 if (firstNonOptionArgument)
170 options.filepath = firstNonOptionArgument;
171
172 // lodash or else would be better, but no need for the extra dependency
173 for (var option in userOptions) {
174 options[option] = userOptions[option];
175 }
176 cb(err, options);
177 });
178};
179
180module.exports.getBaseOptions = getBaseOptions;
181module.exports.getOptions = getOptions;