From d8dcb3a25c0fa128e19a30d07de5a0012bbdc9c4 Mon Sep 17 00:00:00 2001 From: Andrew Ovens Date: Tue, 11 Aug 2020 12:51:53 -0600 Subject: [PATCH 1/5] Add AWS CodeBuild support --- README.md | 1 + lib/getOptions.js | 12 ++++++++++++ test/getOptions.js | 36 ++++++++++++++++++++++++++++++++++++ 3 files changed, 49 insertions(+) diff --git a/README.md b/README.md index c0cb324c..54f22335 100644 --- a/README.md +++ b/README.md @@ -15,6 +15,7 @@ * [Buildkite](https://buildkite.com/) * [GitHub Actions CI](https://github.com/features/actions) * [CodeFresh](https://codefresh.io/) +* [AWS CodeBuild](https://docs.aws.amazon.com/codebuild/index.html) ## Installation: diff --git a/lib/getOptions.js b/lib/getOptions.js index 3ac55794..aa10afd9 100644 --- a/lib/getOptions.js +++ b/lib/getOptions.js @@ -143,6 +143,18 @@ const getBaseOptions = cb => { git_message = process.env.CF_COMMIT_MESSAGE; } + if (process.env.CODEBUILD_BUILD_ARN) { + options.service_name = 'AWS CodeBuild'; + options.service_job_id = process.env.CODEBUILD_BUILD_ID; + + if (process.env.CODEBUILD_WEBHOOK_TRIGGER && process.env.CODEBUILD_WEBHOOK_TRIGGER.startsWith('pr/')) { + options.service_pull_request = process.env.CODEBUILD_WEBHOOK_TRIGGER.substr(3); + } + + git_commit = process.env.CODEBUILD_RESOLVED_SOURCE_VERSION; + git_branch = process.env.CODEBUILD_SOURCE_VERSION; + } + options.run_at = process.env.COVERALLS_RUN_AT || JSON.stringify(new Date()).slice(1, -1); if (process.env.COVERALLS_SERVICE_NUMBER) { diff --git a/test/getOptions.js b/test/getOptions.js index 5bf0312f..ab866c9c 100644 --- a/test/getOptions.js +++ b/test/getOptions.js @@ -69,6 +69,9 @@ describe('getBaseOptions', () => { it('should set service_name and service_job_id if it\'s running on Azure Pipelines', done => { testAzurePipelines(getBaseOptions, done); }); + it('should set service_name and service_job_id if it\'s running on AWS CodeBuild', done => { + testAwsCodeBuild(getBaseOptions, done); + }); }); describe('getOptions', () => { @@ -179,6 +182,9 @@ describe('getOptions', () => { it('should set service_name and service_job_id if it\'s running via Azure Pipelines', done => { testAzurePipelines(getOptions, done); }); + it('should set service_name and service_job_id if it\'s running via AWS CodeBuild', done => { + testAwsCodeBuild(getOptions, done); + }); it('should set service_name and service_job_id if it\'s running via CodeFresh', done => { testCodefresh(getOptions, done); }); @@ -713,6 +719,36 @@ const testAzurePipelines = (sut, done) => { }); }; +const testAwsCodeBuild = (sut, done) => { + process.env.CODEBUILD_BUILD_ARN = 'arn:build:1'; + process.env.CODEBUILD_BUILD_ID = '1234'; + process.env.CODEBUILD_RESOLVED_SOURCE_VERSION = 'e3e3e3e3e3e3e3e3e'; + process.env.CODEBUILD_SOURCE_VERSION = 'hotfix'; + process.env.CODEBUILD_WEBHOOK_TRIGGER = 'pr/123'; + + const git = { + head: { + id: 'e3e3e3e3e3e3e3e3e', + author_name: 'Unknown Author', + author_email: '', + committer_name: 'Unknown Committer', + committer_email: '', + message: 'Unknown Commit Message' + }, + branch: 'hotfix', + remotes: [] + }; + + sut((err, options) => { + should.not.exist(err); + options.service_name.should.equal('AWS CodeBuild'); + options.service_job_id.should.equal('1234'); + options.service_pull_request.should.equal('123'); + options.git.should.eql(git); + done(); + }); +}; + const testCodefresh = (sut, done) => { process.env.CF_BRANCH = 'hotfix'; process.env.CF_REVISION = 'e3e3e3e3e3e3e3e3e'; From 0b2c812f60ab9e19182d83c72b792571cfed312c Mon Sep 17 00:00:00 2001 From: Andrew Ovens Date: Tue, 11 Aug 2020 13:49:53 -0600 Subject: [PATCH 2/5] Use more reliable way of getting branch name for CodeBuild --- lib/getOptions.js | 4 +++- test/getOptions.js | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/getOptions.js b/lib/getOptions.js index aa10afd9..ae7761d0 100644 --- a/lib/getOptions.js +++ b/lib/getOptions.js @@ -152,7 +152,9 @@ const getBaseOptions = cb => { } git_commit = process.env.CODEBUILD_RESOLVED_SOURCE_VERSION; - git_branch = process.env.CODEBUILD_SOURCE_VERSION; + git_branch = process.env.CODEBUILD_WEBHOOK_HEAD_REF && process.env.CODEBUILD_WEBHOOK_HEAD_REF.startsWith('refs/heads/') ? + process.env.CODEBUILD_WEBHOOK_HEAD_REF.substr(11) : + process.env.CODEBUILD_SOURCE_VERSION; } options.run_at = process.env.COVERALLS_RUN_AT || JSON.stringify(new Date()).slice(1, -1); diff --git a/test/getOptions.js b/test/getOptions.js index ab866c9c..41001d8e 100644 --- a/test/getOptions.js +++ b/test/getOptions.js @@ -723,7 +723,7 @@ const testAwsCodeBuild = (sut, done) => { process.env.CODEBUILD_BUILD_ARN = 'arn:build:1'; process.env.CODEBUILD_BUILD_ID = '1234'; process.env.CODEBUILD_RESOLVED_SOURCE_VERSION = 'e3e3e3e3e3e3e3e3e'; - process.env.CODEBUILD_SOURCE_VERSION = 'hotfix'; + process.env.CODEBUILD_WEBHOOK_HEAD_REF = 'refs/heads/hotfix'; process.env.CODEBUILD_WEBHOOK_TRIGGER = 'pr/123'; const git = { From e6ddbc8e65fd622705201ae7fc47a1f371d9959a Mon Sep 17 00:00:00 2001 From: Andrew Ovens Date: Wed, 12 Aug 2020 12:13:29 -0600 Subject: [PATCH 3/5] Add COVERALLS_PROJECT_ROOT_PATH option --- README.md | 1 + lib/convertLcovToCoveralls.js | 6 +++--- lib/getOptions.js | 4 ++++ 3 files changed, 8 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 54f22335..5c124300 100644 --- a/README.md +++ b/README.md @@ -51,6 +51,7 @@ There are optional environment variables for other build systems as well: - `COVERALLS_SERVICE_JOB_NUMBER` (a number that uniquely identifies the build's job) - `COVERALLS_RUN_AT` (a date string for the time that the job ran. RFC 3339 dates work. This defaults to your build system's date/time if you don't set it) - `COVERALLS_PARALLEL` (set to `true` when running jobs in parallel, requires a completion webhook. More info here: ) +- `COVERALLS_PROJECT_ROOT_PATH` (an absolute path to the root of your project. Useful if you have multiple projects in one repo (ie: /frontend, /backend) or run your coverage from a subdirectory) ### GitHub Actions CI diff --git a/lib/convertLcovToCoveralls.js b/lib/convertLcovToCoveralls.js index bcbb9933..466376a1 100644 --- a/lib/convertLcovToCoveralls.js +++ b/lib/convertLcovToCoveralls.js @@ -27,7 +27,7 @@ const detailsToBranches = details => { return branches; }; -const convertLcovFileObject = (file, filepath) => { +const convertLcovFileObject = (file, filepath, projectRootPath) => { const rootpath = filepath; filepath = path.resolve(rootpath, file.file); const source = fs.readFileSync(filepath, 'utf8'); @@ -36,7 +36,7 @@ const convertLcovFileObject = (file, filepath) => { const branches = detailsToBranches(file.branches.details); return { - name: path.relative(rootpath, path.resolve(rootpath, file.file)).split(path.sep).join('/'), + name: path.relative(projectRootPath || rootpath, path.resolve(rootpath, file.file)).split(path.sep).join('/'), source, coverage, branches @@ -112,7 +112,7 @@ const convertLcovToCoveralls = (input, options, cb) => { file.file = cleanFilePath(file.file); const currentFilePath = path.resolve(filepath, file.file); if (fs.existsSync(currentFilePath)) { - postJson.source_files.push(convertLcovFileObject(file, filepath)); + postJson.source_files.push(convertLcovFileObject(file, filepath, options.project_root_path)); } }); diff --git a/lib/getOptions.js b/lib/getOptions.js index ae7761d0..03c39eaf 100644 --- a/lib/getOptions.js +++ b/lib/getOptions.js @@ -183,6 +183,10 @@ const getBaseOptions = cb => { options.parallel = true; } + if (process.env.COVERALLS_PROJECT_ROOT_PATH) { + options.project_root_path = process.env.COVERALLS_PROJECT_ROOT_PATH; + } + // load a .coveralls.yml file const coveralls_yaml_conf = (() => { const yml = path.join(process.cwd(), '.coveralls.yml'); From 40f1cb3bb10ef28b54f4a84a40d2a6b6cb42d2c0 Mon Sep 17 00:00:00 2001 From: Andrew Ovens Date: Wed, 12 Aug 2020 12:18:56 -0600 Subject: [PATCH 4/5] Add test for project root --- test/convertLcovToCoveralls.js | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/test/convertLcovToCoveralls.js b/test/convertLcovToCoveralls.js index 2adaa835..b59ab116 100644 --- a/test/convertLcovToCoveralls.js +++ b/test/convertLcovToCoveralls.js @@ -72,6 +72,19 @@ describe('convertLcovToCoveralls', () => { }); }); + it('should allow specifying the project root', done => { + delete process.env.TRAVIS; + const lcovpath = path.join(__dirname, './fixtures/onefile.lcov'); + const input = fs.readFileSync(lcovpath, 'utf8'); + const libpath = 'test/fixtures/lib'; + convertLcovToCoveralls(input, { filepath: libpath, project_root_path: path.join(__dirname, '..') }, (err, output) => { + should.not.exist(err); + output.source_files[0].name.should.equal('test/fixtures/lib/index.js'); + output.source_files[0].source.split('\n').length.should.equal(173); + done(); + }); + }); + it('should convert absolute input paths to relative', done => { delete process.env.TRAVIS; const lcovpath = path.join(__dirname, './fixtures/istanbul.lcov'); From 35d7cf9ea42fa9ada9a77df646f4c6e479b378d9 Mon Sep 17 00:00:00 2001 From: Andrew Ovens Date: Tue, 10 Nov 2020 10:44:05 -0700 Subject: [PATCH 5/5] Allow overriding CI values for commit or branch --- lib/getOptions.js | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/lib/getOptions.js b/lib/getOptions.js index 03c39eaf..2eb5968a 100644 --- a/lib/getOptions.js +++ b/lib/getOptions.js @@ -171,6 +171,14 @@ const getBaseOptions = cb => { options.service_job_id = process.env.COVERALLS_SERVICE_JOB_ID; } + if (process.env.COVERALLS_GIT_COMMIT) { + git_commit = process.env.COVERALLS_GIT_COMMIT; + } + + if (process.env.COVERALLS_GIT_BRANCH) { + git_branch = process.env.COVERALLS_GIT_BRANCH; + } + if (!git_commit || !git_branch) { const data = detectLocalGit(); if (data) {