Skip to content
This repository has been archived by the owner on Jan 10, 2023. It is now read-only.

Commit

Permalink
Verify source version before parsing PR
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
RoboCafaz committed Mar 5, 2020
1 parent ebe132e commit ef348ec
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
5 changes: 4 additions & 1 deletion lib/services/codebuild.js
Expand Up @@ -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
Expand Down
17 changes: 16 additions & 1 deletion test/services/codebuild.test.js
Expand Up @@ -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',
Expand All @@ -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'
Expand Down

0 comments on commit ef348ec

Please sign in to comment.