From ef348ec82b8543448ee9ed2df647cca7d57956f6 Mon Sep 17 00:00:00 2001 From: robocafaz Date: Wed, 4 Mar 2020 22:24:58 -0500 Subject: [PATCH] Verify source version before parsing PR Non-PR hooks can trigger builds in which the CODEBUILD_SOURCE_VERSION would be populated as the commit SHA. Our current logic dictates that that commit SHA would be passed onto CodeCov as the PR number, which would always be an invalid value (must be a number, true, or falsy). This updates the logic so that it will ensure that the environment variable being checked actually matches the "pr/" pattern before trying to use it. If it does not, it will return undefined. --- lib/services/codebuild.js | 5 ++++- test/services/codebuild.test.js | 17 ++++++++++++++++- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/lib/services/codebuild.js b/lib/services/codebuild.js index 8126527a..30ef6443 100644 --- a/lib/services/codebuild.js +++ b/lib/services/codebuild.js @@ -26,7 +26,10 @@ module.exports = { return git.branch() } function detectPRNumber() { - if (process.env.CODEBUILD_WEBHOOK_HEAD_REF) { + if ( + process.env.CODEBUILD_WEBHOOK_HEAD_REF && + process.env.CODEBUILD_SOURCE_VERSION.startsWith('pr/') + ) { return process.env.CODEBUILD_SOURCE_VERSION.replace(/^pr\//, '') } return undefined diff --git a/test/services/codebuild.test.js b/test/services/codebuild.test.js index 5bf510c0..82518f46 100644 --- a/test/services/codebuild.test.js +++ b/test/services/codebuild.test.js @@ -55,7 +55,7 @@ describe('AWS CodeBuild Provider', function() { }) }) - it('Test build triggered via Github Webhook', function() { + it('Test PR build triggered via Github Webhook', function() { process.env.CODEBUILD_WEBHOOK_HEAD_REF = 'refs/heads/master' expect(codebuild.configuration()).toEqual({ service: 'codebuild', @@ -68,6 +68,21 @@ describe('AWS CodeBuild Provider', function() { }) }) + it('Test non-PR build triggered via Github Webhook', function() { + process.env.CODEBUILD_WEBHOOK_HEAD_REF = 'refs/heads/master' + process.env.CODEBUILD_SOURCE_VERSION = + '39ec2418eca4c539d765574a1c68f3bd77e8c549' + expect(codebuild.configuration()).toEqual({ + service: 'codebuild', + build: 'my-project:e016b9d9-f2c8-4749-8373-7ca673b6d969', + job: 'my-project:e016b9d9-f2c8-4749-8373-7ca673b6d969', + commit: '39ec2418eca4c539d765574a1c68f3bd77e8c549', + branch: 'master', + pr: undefined, + slug: 'my-org/my-project', + }) + }) + it('throws if slug cannot be detected', function() { process.env.CODEBUILD_RESOLVED_SOURCE_VERSION = '39ec2418eca4c539d765574a1c68f3bd77e8c549'