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

Commit 7c4cdc4

Browse files
authored
Merge pull request #149 from aiell0/master
Handle builds triggered by AWS SDK.
2 parents 62389fa + 8acbe96 commit 7c4cdc4

File tree

2 files changed

+36
-8
lines changed

2 files changed

+36
-8
lines changed

lib/services/codebuild.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
var git = require('../git.js')
2+
13
module.exports = {
24
detect: function() {
35
return !!process.env.CODEBUILD_CI
@@ -21,13 +23,13 @@ module.exports = {
2123
''
2224
)
2325
}
24-
throw new Error('Cannot detect branch name.')
26+
return git.branch()
2527
}
2628
function detectPRNumber() {
27-
if (process.env.CODEBUILD_SOURCE_VERSION) {
29+
if (process.env.CODEBUILD_WEBHOOK_HEAD_REF) {
2830
return process.env.CODEBUILD_SOURCE_VERSION.replace(/^pr\//, '')
2931
}
30-
throw new Error('Cannot detect PR number.')
32+
return undefined
3133
}
3234
function detectRepoSlug() {
3335
if (process.env.CODEBUILD_SOURCE_REPO_URL) {

test/services/codebuild.test.js

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
var codebuild = require('../../lib/services/codebuild')
2+
var git = require('../../lib/git.js')
3+
4+
// Set all module functions to jest.fn
5+
jest.mock('../../lib/git.js')
26

37
describe('AWS CodeBuild Provider', function() {
48
it('can detect codebuild', function() {
@@ -29,17 +33,39 @@ describe('AWS CodeBuild Provider', function() {
2933

3034
it('throws if branch name cannot be detected', function() {
3135
delete process.env.CODEBUILD_WEBHOOK_HEAD_REF
36+
git.branch.mockImplementation(function() {
37+
throw new Error()
38+
})
3239
expect(function() {
3340
codebuild.configuration()
3441
}).toThrow()
3542
})
3643

37-
it('throws if pr number cannot be detected', function() {
44+
it('Test build triggered via AWS SDK', function() {
45+
delete process.env.CODEBUILD_WEBHOOK_HEAD_REF
46+
git.branch.mockReturnValue('master')
47+
expect(codebuild.configuration()).toEqual({
48+
service: 'codebuild',
49+
build: 'my-project:e016b9d9-f2c8-4749-8373-7ca673b6d969',
50+
job: 'my-project:e016b9d9-f2c8-4749-8373-7ca673b6d969',
51+
commit: '39ec2418eca4c539d765574a1c68f3bd77e8c549',
52+
branch: 'master',
53+
pr: undefined,
54+
slug: 'my-org/my-project',
55+
})
56+
})
57+
58+
it('Test build triggered via Github Webhook', function() {
3859
process.env.CODEBUILD_WEBHOOK_HEAD_REF = 'refs/heads/master'
39-
delete process.env.CODEBUILD_SOURCE_VERSION
40-
expect(function() {
41-
codebuild.configuration()
42-
}).toThrow()
60+
expect(codebuild.configuration()).toEqual({
61+
service: 'codebuild',
62+
build: 'my-project:e016b9d9-f2c8-4749-8373-7ca673b6d969',
63+
job: 'my-project:e016b9d9-f2c8-4749-8373-7ca673b6d969',
64+
commit: '39ec2418eca4c539d765574a1c68f3bd77e8c549',
65+
branch: 'master',
66+
pr: '1',
67+
slug: 'my-org/my-project',
68+
})
4369
})
4470

4571
it('throws if slug cannot be detected', function() {

0 commit comments

Comments
 (0)