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

Commit ef348ec

Browse files
committed
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.
1 parent ebe132e commit ef348ec

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

lib/services/codebuild.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,10 @@ module.exports = {
2626
return git.branch()
2727
}
2828
function detectPRNumber() {
29-
if (process.env.CODEBUILD_WEBHOOK_HEAD_REF) {
29+
if (
30+
process.env.CODEBUILD_WEBHOOK_HEAD_REF &&
31+
process.env.CODEBUILD_SOURCE_VERSION.startsWith('pr/')
32+
) {
3033
return process.env.CODEBUILD_SOURCE_VERSION.replace(/^pr\//, '')
3134
}
3235
return undefined

test/services/codebuild.test.js

+16-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ describe('AWS CodeBuild Provider', function() {
5555
})
5656
})
5757

58-
it('Test build triggered via Github Webhook', function() {
58+
it('Test PR build triggered via Github Webhook', function() {
5959
process.env.CODEBUILD_WEBHOOK_HEAD_REF = 'refs/heads/master'
6060
expect(codebuild.configuration()).toEqual({
6161
service: 'codebuild',
@@ -68,6 +68,21 @@ describe('AWS CodeBuild Provider', function() {
6868
})
6969
})
7070

71+
it('Test non-PR build triggered via Github Webhook', function() {
72+
process.env.CODEBUILD_WEBHOOK_HEAD_REF = 'refs/heads/master'
73+
process.env.CODEBUILD_SOURCE_VERSION =
74+
'39ec2418eca4c539d765574a1c68f3bd77e8c549'
75+
expect(codebuild.configuration()).toEqual({
76+
service: 'codebuild',
77+
build: 'my-project:e016b9d9-f2c8-4749-8373-7ca673b6d969',
78+
job: 'my-project:e016b9d9-f2c8-4749-8373-7ca673b6d969',
79+
commit: '39ec2418eca4c539d765574a1c68f3bd77e8c549',
80+
branch: 'master',
81+
pr: undefined,
82+
slug: 'my-org/my-project',
83+
})
84+
})
85+
7186
it('throws if slug cannot be detected', function() {
7287
process.env.CODEBUILD_RESOLVED_SOURCE_VERSION =
7388
'39ec2418eca4c539d765574a1c68f3bd77e8c549'

0 commit comments

Comments
 (0)