Skip to content

Commit

Permalink
Set service_name and/or repo_token from .coveralls.yml regardless of …
Browse files Browse the repository at this point in the history
…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 <derek.herman@xwp.co>
  • Loading branch information
benpetty and derekherman authored Mar 19, 2020
1 parent 710c504 commit 3d83b4f
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 16 deletions.
46 changes: 30 additions & 16 deletions lib/getOptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const getBaseOptions = cb => {
options.service_name = 'travis-ci';
options.service_job_id = process.env.TRAVIS_JOB_ID;
options.service_pull_request = process.env.TRAVIS_PULL_REQUEST;
git_commit = 'HEAD';
git_commit = process.env.TRAVIS_COMMIT || 'HEAD';
git_branch = process.env.TRAVIS_BRANCH;
}

Expand Down Expand Up @@ -142,9 +142,6 @@ const getBaseOptions = cb => {
}

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

if (process.env.COVERALLS_SERVICE_JOB_ID) {
options.service_job_id = process.env.COVERALLS_SERVICE_JOB_ID;
Expand All @@ -162,24 +159,41 @@ const getBaseOptions = cb => {
options.parallel = true;
}

// try to get the repo token as an environment variable
if (process.env.COVERALLS_REPO_TOKEN) {
options.repo_token = process.env.COVERALLS_REPO_TOKEN;
} else {
// try to get the repo token from a .coveralls.yml file
// load a .coveralls.yml file
const coveralls_yaml_conf = (() => {
const yml = path.join(process.cwd(), '.coveralls.yml');
try {
if (fs.statSync(yml).isFile()) {
const coveralls_yaml_conf = yaml.safeLoad(fs.readFileSync(yml, 'utf8'));
options.repo_token = coveralls_yaml_conf.repo_token;
if (coveralls_yaml_conf.service_name) {
options.service_name = coveralls_yaml_conf.service_name;
}
return yaml.safeLoad(fs.readFileSync(yml, 'utf8'));
}
} catch (_) {
logger.warn('Repo token could not be determined. Continuing without it. ' +
'This is necessary for private repos only, so may not be an issue at all.');
logger.debug('No valid .coveralls.yml file found');
}
})();

// try to get repo token and service name from .coveralls.yml file
if (coveralls_yaml_conf) {
if (coveralls_yaml_conf.repo_token) {
options.repo_token = coveralls_yaml_conf.repo_token;
}
if (coveralls_yaml_conf.service_name) {
options.service_name = coveralls_yaml_conf.service_name;
}
}

// try to get the repo token as an environment variable
if (process.env.COVERALLS_REPO_TOKEN) {
options.repo_token = process.env.COVERALLS_REPO_TOKEN;
}

if ('travis-pro' === options.service_name && !options.repo_token) {
logger.warn('Repo token could not be determined. Continuing without it. ' +
'This is necessary for private repos only, so may not be an issue at all.');
}

// try to get the service name as an environment variable
if (process.env.COVERALLS_SERVICE_NAME) {
options.service_name = process.env.COVERALLS_SERVICE_NAME;
}

if (process.env.COVERALLS_FLAG_NAME) {
Expand Down
23 changes: 23 additions & 0 deletions test/getOptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ describe('getBaseOptions', () => {
it('should set service_name and service_job_id if it\'s running on travis-ci', done => {
testTravisCi(getBaseOptions, done);
});
it('should set service_name and service_job_id if it\'s running on travis-pro', done => {
testTravisPro(getBaseOptions, done);
});
it('should set service_name and service_job_id if it\'s running on jenkins', done => {
testJenkins(getBaseOptions, done);
});
Expand Down Expand Up @@ -137,6 +140,9 @@ describe('getOptions', () => {
it('should set service_name and service_job_id if it\'s running on travis-ci', done => {
testTravisCi(getOptions, done);
});
it('should set service_name and service_job_id if it\'s running on travis-pro', done => {
testTravisPro(getOptions, done);
});
it('should set service_name and service_job_id if it\'s running on jenkins', done => {
testJenkins(getOptions, done);
});
Expand Down Expand Up @@ -350,6 +356,23 @@ const testTravisCi = (sut, done) => {
});
};

const testTravisPro = (sut, done) => {
const file = path.join(process.cwd(), '.coveralls.yml');
const service_name = 'travis-pro';
fs.writeFileSync(file, `service_name: ${service_name}`);
process.env.TRAVIS = 'TRUE';
process.env.TRAVIS_JOB_ID = '1234';
process.env.TRAVIS_COMMIT = 'a12s2d3df4f435g45g45g67h5g6';
sut((err, options) => {
should.not.exist(err);
options.service_name.should.equal(service_name);
options.service_job_id.should.equal('1234');
options.git.head.id.should.equal('a12s2d3df4f435g45g45g67h5g6');
fs.unlinkSync(file);
done();
});
};

const testJenkins = (sut, done) => {
process.env.JENKINS_URL = 'something';
process.env.BUILD_ID = '1234';
Expand Down

0 comments on commit 3d83b4f

Please # to comment.