Skip to content

Commit 3d83b4f

Browse files
Set service_name and/or repo_token from .coveralls.yml regardless of if $COVERALLS_REPO_TOKEN is set (#272)
* using TRAVIS_COMMIT environment variable for git_commit * Setting repo token and service name from .coveralls.yml regardless of if $COVERALLS_REPO_TOKEN * only set options.repo_token from coveralls_yaml_conf if set in .coveralls.yml * Update lib/getOptions.js Co-Authored-By: Derek Herman <[email protected]>
1 parent 710c504 commit 3d83b4f

File tree

2 files changed

+53
-16
lines changed

2 files changed

+53
-16
lines changed

Diff for: lib/getOptions.js

+30-16
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ const getBaseOptions = cb => {
2626
options.service_name = 'travis-ci';
2727
options.service_job_id = process.env.TRAVIS_JOB_ID;
2828
options.service_pull_request = process.env.TRAVIS_PULL_REQUEST;
29-
git_commit = 'HEAD';
29+
git_commit = process.env.TRAVIS_COMMIT || 'HEAD';
3030
git_branch = process.env.TRAVIS_BRANCH;
3131
}
3232

@@ -142,9 +142,6 @@ const getBaseOptions = cb => {
142142
}
143143

144144
options.run_at = process.env.COVERALLS_RUN_AT || JSON.stringify(new Date()).slice(1, -1);
145-
if (process.env.COVERALLS_SERVICE_NAME) {
146-
options.service_name = process.env.COVERALLS_SERVICE_NAME;
147-
}
148145

149146
if (process.env.COVERALLS_SERVICE_JOB_ID) {
150147
options.service_job_id = process.env.COVERALLS_SERVICE_JOB_ID;
@@ -162,24 +159,41 @@ const getBaseOptions = cb => {
162159
options.parallel = true;
163160
}
164161

165-
// try to get the repo token as an environment variable
166-
if (process.env.COVERALLS_REPO_TOKEN) {
167-
options.repo_token = process.env.COVERALLS_REPO_TOKEN;
168-
} else {
169-
// try to get the repo token from a .coveralls.yml file
162+
// load a .coveralls.yml file
163+
const coveralls_yaml_conf = (() => {
170164
const yml = path.join(process.cwd(), '.coveralls.yml');
171165
try {
172166
if (fs.statSync(yml).isFile()) {
173-
const coveralls_yaml_conf = yaml.safeLoad(fs.readFileSync(yml, 'utf8'));
174-
options.repo_token = coveralls_yaml_conf.repo_token;
175-
if (coveralls_yaml_conf.service_name) {
176-
options.service_name = coveralls_yaml_conf.service_name;
177-
}
167+
return yaml.safeLoad(fs.readFileSync(yml, 'utf8'));
178168
}
179169
} catch (_) {
180-
logger.warn('Repo token could not be determined. Continuing without it. ' +
181-
'This is necessary for private repos only, so may not be an issue at all.');
170+
logger.debug('No valid .coveralls.yml file found');
171+
}
172+
})();
173+
174+
// try to get repo token and service name from .coveralls.yml file
175+
if (coveralls_yaml_conf) {
176+
if (coveralls_yaml_conf.repo_token) {
177+
options.repo_token = coveralls_yaml_conf.repo_token;
182178
}
179+
if (coveralls_yaml_conf.service_name) {
180+
options.service_name = coveralls_yaml_conf.service_name;
181+
}
182+
}
183+
184+
// try to get the repo token as an environment variable
185+
if (process.env.COVERALLS_REPO_TOKEN) {
186+
options.repo_token = process.env.COVERALLS_REPO_TOKEN;
187+
}
188+
189+
if ('travis-pro' === options.service_name && !options.repo_token) {
190+
logger.warn('Repo token could not be determined. Continuing without it. ' +
191+
'This is necessary for private repos only, so may not be an issue at all.');
192+
}
193+
194+
// try to get the service name as an environment variable
195+
if (process.env.COVERALLS_SERVICE_NAME) {
196+
options.service_name = process.env.COVERALLS_SERVICE_NAME;
183197
}
184198

185199
if (process.env.COVERALLS_FLAG_NAME) {

Diff for: test/getOptions.js

+23
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ describe('getBaseOptions', () => {
4545
it('should set service_name and service_job_id if it\'s running on travis-ci', done => {
4646
testTravisCi(getBaseOptions, done);
4747
});
48+
it('should set service_name and service_job_id if it\'s running on travis-pro', done => {
49+
testTravisPro(getBaseOptions, done);
50+
});
4851
it('should set service_name and service_job_id if it\'s running on jenkins', done => {
4952
testJenkins(getBaseOptions, done);
5053
});
@@ -137,6 +140,9 @@ describe('getOptions', () => {
137140
it('should set service_name and service_job_id if it\'s running on travis-ci', done => {
138141
testTravisCi(getOptions, done);
139142
});
143+
it('should set service_name and service_job_id if it\'s running on travis-pro', done => {
144+
testTravisPro(getOptions, done);
145+
});
140146
it('should set service_name and service_job_id if it\'s running on jenkins', done => {
141147
testJenkins(getOptions, done);
142148
});
@@ -350,6 +356,23 @@ const testTravisCi = (sut, done) => {
350356
});
351357
};
352358

359+
const testTravisPro = (sut, done) => {
360+
const file = path.join(process.cwd(), '.coveralls.yml');
361+
const service_name = 'travis-pro';
362+
fs.writeFileSync(file, `service_name: ${service_name}`);
363+
process.env.TRAVIS = 'TRUE';
364+
process.env.TRAVIS_JOB_ID = '1234';
365+
process.env.TRAVIS_COMMIT = 'a12s2d3df4f435g45g45g67h5g6';
366+
sut((err, options) => {
367+
should.not.exist(err);
368+
options.service_name.should.equal(service_name);
369+
options.service_job_id.should.equal('1234');
370+
options.git.head.id.should.equal('a12s2d3df4f435g45g45g67h5g6');
371+
fs.unlinkSync(file);
372+
done();
373+
});
374+
};
375+
353376
const testJenkins = (sut, done) => {
354377
process.env.JENKINS_URL = 'something';
355378
process.env.BUILD_ID = '1234';

0 commit comments

Comments
 (0)