From 3d83b4f29f2dbf6f4a642ba24976d4ecbd5d56b1 Mon Sep 17 00:00:00 2001 From: Ben Petty <22062601+benpetty@users.noreply.github.com> Date: Thu, 19 Mar 2020 10:06:13 -0700 Subject: [PATCH] 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 --- lib/getOptions.js | 46 ++++++++++++++++++++++++++++++---------------- test/getOptions.js | 23 +++++++++++++++++++++++ 2 files changed, 53 insertions(+), 16 deletions(-) diff --git a/lib/getOptions.js b/lib/getOptions.js index ac12fc65..c4a3e319 100644 --- a/lib/getOptions.js +++ b/lib/getOptions.js @@ -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; } @@ -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; @@ -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) { diff --git a/test/getOptions.js b/test/getOptions.js index becb31fb..5850dc25 100644 --- a/test/getOptions.js +++ b/test/getOptions.js @@ -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); }); @@ -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); }); @@ -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';