1 | 'use strict';
|
2 |
|
3 | const fs = require('fs');
|
4 | const path = require('path');
|
5 | const yaml = require('js-yaml');
|
6 | const logger = require('./logger')();
|
7 | const fetchGitData = require('./fetchGitData');
|
8 | const detectLocalGit = require('./detectLocalGit');
|
9 | const index = require('..');
|
10 |
|
11 | const getBaseOptions = cb => {
|
12 | const options = {};
|
13 | let git_commit = process.env.COVERALLS_GIT_COMMIT;
|
14 | let git_branch = process.env.COVERALLS_GIT_BRANCH;
|
15 | let git_committer_name;
|
16 | let git_committer_email;
|
17 | let git_message;
|
18 |
|
19 | const match = (process.env.CI_PULL_REQUEST || '').match(/(\d+)$/);
|
20 |
|
21 | if (match) {
|
22 | options.service_pull_request = match[1];
|
23 | }
|
24 |
|
25 | if (process.env.TRAVIS) {
|
26 | options.service_name = 'travis-ci';
|
27 | options.service_number = process.env.TRAVIS_BUILD_NUMBER;
|
28 | options.service_job_id = process.env.TRAVIS_JOB_ID;
|
29 | options.service_pull_request = process.env.TRAVIS_PULL_REQUEST;
|
30 | git_commit = 'HEAD';
|
31 | git_branch = process.env.TRAVIS_BRANCH;
|
32 | }
|
33 |
|
34 | if (process.env.DRONE) {
|
35 | options.service_name = 'drone';
|
36 | options.service_job_id = process.env.DRONE_BUILD_NUMBER;
|
37 | options.service_pull_request = process.env.DRONE_PULL_REQUEST;
|
38 | git_committer_name = process.env.DRONE_COMMIT_AUTHOR;
|
39 | git_committer_email = process.env.DRONE_COMMIT_AUTHOR_EMAIL;
|
40 | git_commit = process.env.DRONE_COMMIT;
|
41 | git_branch = process.env.DRONE_BRANCH;
|
42 | git_message = process.env.DRONE_COMMIT_MESSAGE;
|
43 | }
|
44 |
|
45 | if (process.env.JENKINS_URL || process.env.JENKINS_HOME) {
|
46 | options.service_name = 'jenkins';
|
47 | options.service_job_id = process.env.BUILD_ID;
|
48 | options.service_pull_request = process.env.CHANGE_ID || process.env.ghprbPullId;
|
49 | git_committer_name = process.env.CHANGE_AUTHOR;
|
50 | git_committer_email = process.env.CHANGE_AUTHOR_EMAIL;
|
51 | git_commit = process.env.GIT_COMMIT;
|
52 | git_branch = process.env.CHANGE_BRANCH || process.env.GIT_BRANCH || process.env.BRANCH_NAME;
|
53 | }
|
54 |
|
55 | if (process.env.CIRCLECI) {
|
56 | options.service_name = 'circleci';
|
57 | options.service_number = process.env.CIRCLE_WORKFLOW_ID;
|
58 | options.service_job_number = process.env.CIRCLE_BUILD_NUM;
|
59 |
|
60 | if (process.env.CI_PULL_REQUEST) {
|
61 | const pr = process.env.CI_PULL_REQUEST.split('/pull/');
|
62 | options.service_pull_request = pr[1];
|
63 | }
|
64 |
|
65 | git_commit = process.env.CIRCLE_SHA1;
|
66 | git_branch = process.env.CIRCLE_BRANCH;
|
67 | }
|
68 |
|
69 | if (process.env.CI_NAME && process.env.CI_NAME === 'codeship') {
|
70 | options.service_name = 'codeship';
|
71 | options.service_job_id = process.env.CI_BUILD_NUMBER;
|
72 | git_commit = process.env.CI_COMMIT_ID;
|
73 | git_branch = process.env.CI_BRANCH;
|
74 | git_committer_name = process.env.CI_COMMITTER_NAME;
|
75 | git_committer_email = process.env.CI_COMMITTER_EMAIL;
|
76 | git_message = process.env.CI_COMMIT_MESSAGE;
|
77 | }
|
78 |
|
79 | if (process.env.WERCKER) {
|
80 | options.service_name = 'wercker';
|
81 | options.service_job_id = process.env.WERCKER_BUILD_ID;
|
82 | git_commit = process.env.WERCKER_GIT_COMMIT;
|
83 | git_branch = process.env.WERCKER_GIT_BRANCH;
|
84 | }
|
85 |
|
86 | if (process.env.GITLAB_CI) {
|
87 | options.service_name = 'gitlab-ci';
|
88 | options.service_job_number = process.env.CI_BUILD_NAME;
|
89 | options.service_job_id = process.env.CI_BUILD_ID;
|
90 | options.service_pull_request = process.env.CI_MERGE_REQUEST_IID;
|
91 | git_commit = process.env.CI_BUILD_REF;
|
92 | git_branch = process.env.CI_BUILD_REF_NAME;
|
93 | }
|
94 |
|
95 | if (process.env.APPVEYOR) {
|
96 | options.service_name = 'appveyor';
|
97 | options.service_job_number = process.env.APPVEYOR_BUILD_NUMBER;
|
98 | options.service_job_id = process.env.APPVEYOR_BUILD_ID;
|
99 | git_commit = process.env.APPVEYOR_REPO_COMMIT;
|
100 | git_branch = process.env.APPVEYOR_REPO_BRANCH;
|
101 | }
|
102 |
|
103 | if (process.env.SURF_SHA1) {
|
104 | options.service_name = 'surf';
|
105 | git_commit = process.env.SURF_SHA1;
|
106 | git_branch = process.env.SURF_REF;
|
107 | }
|
108 |
|
109 | if (process.env.BUILDKITE) {
|
110 | options.service_name = 'buildkite';
|
111 | options.service_job_number = process.env.BUILDKITE_BUILD_NUMBER;
|
112 | options.service_job_id = process.env.BUILDKITE_BUILD_ID;
|
113 | options.service_pull_request = process.env.BUILDKITE_PULL_REQUEST;
|
114 | git_commit = process.env.BUILDKITE_COMMIT;
|
115 | git_branch = process.env.BUILDKITE_BRANCH;
|
116 | git_committer_name = process.env.BUILDKITE_BUILD_CREATOR;
|
117 | git_committer_email = process.env.BUILDKITE_BUILD_CREATOR_EMAIL;
|
118 | git_message = process.env.BUILDKITE_MESSAGE;
|
119 | }
|
120 |
|
121 | if (process.env.SEMAPHORE) {
|
122 | options.service_name = 'semaphore';
|
123 | options.service_job_id = process.env.SEMAPHORE_BUILD_NUMBER;
|
124 | git_commit = process.env.REVISION;
|
125 | git_branch = process.env.BRANCH_NAME;
|
126 | }
|
127 |
|
128 | if (process.env.TF_BUILD) {
|
129 | options.service_name = 'Azure Pipelines';
|
130 | options.service_job_id = process.env.BUILD_BUILDID;
|
131 | options.service_pull_request = process.env.SYSTEM_PULLREQUEST_PULLREQUESTNUMBER;
|
132 | git_commit = process.env.BUILD_SOURCEVERSION;
|
133 | git_branch = process.env.BUILD_SOURCEBRANCHNAME;
|
134 | }
|
135 |
|
136 | if (process.env.CF_BRANCH) {
|
137 | options.service_name = 'Codefresh';
|
138 | options.service_job_id = process.env.CF_BUILD_ID;
|
139 | options.service_pull_request = process.env.CF_PULL_REQUEST_ID;
|
140 | git_commit = process.env.CF_REVISION;
|
141 | git_branch = process.env.CF_BRANCH;
|
142 | git_committer_name = process.env.CF_COMMIT_AUTHOR;
|
143 | git_message = process.env.CF_COMMIT_MESSAGE;
|
144 | }
|
145 |
|
146 | options.run_at = process.env.COVERALLS_RUN_AT || JSON.stringify(new Date()).slice(1, -1);
|
147 |
|
148 | if (process.env.COVERALLS_SERVICE_NUMBER) {
|
149 | options.service_number = process.env.COVERALLS_SERVICE_NUMBER;
|
150 | }
|
151 |
|
152 | if (process.env.COVERALLS_SERVICE_JOB_NUMBER) {
|
153 | options.service_job_number = process.env.COVERALLS_SERVICE_JOB_NUMBER;
|
154 | }
|
155 |
|
156 | if (process.env.COVERALLS_SERVICE_JOB_ID) {
|
157 | options.service_job_id = process.env.COVERALLS_SERVICE_JOB_ID;
|
158 | }
|
159 |
|
160 | if (!git_commit || !git_branch) {
|
161 | const data = detectLocalGit();
|
162 | if (data) {
|
163 | git_commit = git_commit || data.git_commit;
|
164 | git_branch = git_branch || data.git_branch;
|
165 | }
|
166 | }
|
167 |
|
168 | if (process.env.COVERALLS_PARALLEL) {
|
169 | options.parallel = true;
|
170 | }
|
171 |
|
172 |
|
173 | const coveralls_yaml_conf = (() => {
|
174 | const yml = path.join(process.cwd(), '.coveralls.yml');
|
175 | try {
|
176 | if (fs.statSync(yml).isFile()) {
|
177 | return yaml.safeLoad(fs.readFileSync(yml, 'utf8'));
|
178 | }
|
179 | } catch (_) {
|
180 | logger.debug('No valid .coveralls.yml file found');
|
181 | }
|
182 | })();
|
183 |
|
184 |
|
185 | if (coveralls_yaml_conf) {
|
186 | if (coveralls_yaml_conf.repo_token) {
|
187 | options.repo_token = coveralls_yaml_conf.repo_token;
|
188 | }
|
189 |
|
190 | if (coveralls_yaml_conf.service_name) {
|
191 | options.service_name = coveralls_yaml_conf.service_name;
|
192 | }
|
193 | }
|
194 |
|
195 |
|
196 | if (process.env.COVERALLS_REPO_TOKEN) {
|
197 | options.repo_token = process.env.COVERALLS_REPO_TOKEN;
|
198 | }
|
199 |
|
200 | if (options.service_name === 'travis-pro' && !options.repo_token) {
|
201 | logger.warn('Repo token could not be determined. Continuing without it. ' +
|
202 | 'This is necessary for private repos only, so may not be an issue at all.');
|
203 | }
|
204 |
|
205 |
|
206 | if (process.env.COVERALLS_SERVICE_NAME) {
|
207 | options.service_name = process.env.COVERALLS_SERVICE_NAME;
|
208 | }
|
209 |
|
210 | if (process.env.COVERALLS_FLAG_NAME) {
|
211 | options.flag_name = process.env.COVERALLS_FLAG_NAME;
|
212 | }
|
213 |
|
214 | if (git_commit) {
|
215 | fetchGitData({
|
216 | head: {
|
217 | id: git_commit,
|
218 | committer_name: git_committer_name,
|
219 | committer_email: git_committer_email,
|
220 | message: git_message
|
221 | },
|
222 | branch: git_branch
|
223 | }, (err, git) => {
|
224 | if (err) {
|
225 | logger.warn('there was an error getting git data: ', err);
|
226 | } else {
|
227 | options.git = git;
|
228 | }
|
229 |
|
230 | return cb(err, options);
|
231 | });
|
232 | } else {
|
233 | return cb(null, options);
|
234 | }
|
235 | };
|
236 |
|
237 | const getOptions = (cb, _userOptions) => {
|
238 | if (!cb) {
|
239 | throw new Error('getOptions requires a callback');
|
240 | }
|
241 |
|
242 | const userOptions = _userOptions || {};
|
243 |
|
244 | getBaseOptions((err, options) => {
|
245 |
|
246 | const firstNonOptionArgument = index.options._[0];
|
247 |
|
248 | if (firstNonOptionArgument) {
|
249 | options.filepath = firstNonOptionArgument;
|
250 | }
|
251 |
|
252 |
|
253 | for (const option in userOptions) {
|
254 | if (Object.prototype.hasOwnProperty.call(userOptions, option)) {
|
255 | options[option] = userOptions[option];
|
256 | }
|
257 | }
|
258 |
|
259 | cb(err, options);
|
260 | });
|
261 | };
|
262 |
|
263 | module.exports.getBaseOptions = getBaseOptions;
|
264 | module.exports.getOptions = getOptions;
|