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

Commit

Permalink
Merge pull request #149 from aiell0/master
Browse files Browse the repository at this point in the history
Handle builds triggered by AWS SDK.
  • Loading branch information
eddiemoore committed Jan 31, 2020
2 parents 62389fa + 8acbe96 commit 7c4cdc4
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 8 deletions.
8 changes: 5 additions & 3 deletions lib/services/codebuild.js
@@ -1,3 +1,5 @@
var git = require('../git.js')

module.exports = {
detect: function() {
return !!process.env.CODEBUILD_CI
Expand All @@ -21,13 +23,13 @@ module.exports = {
''
)
}
throw new Error('Cannot detect branch name.')
return git.branch()
}
function detectPRNumber() {
if (process.env.CODEBUILD_SOURCE_VERSION) {
if (process.env.CODEBUILD_WEBHOOK_HEAD_REF) {
return process.env.CODEBUILD_SOURCE_VERSION.replace(/^pr\//, '')
}
throw new Error('Cannot detect PR number.')
return undefined
}
function detectRepoSlug() {
if (process.env.CODEBUILD_SOURCE_REPO_URL) {
Expand Down
36 changes: 31 additions & 5 deletions test/services/codebuild.test.js
@@ -1,4 +1,8 @@
var codebuild = require('../../lib/services/codebuild')
var git = require('../../lib/git.js')

// Set all module functions to jest.fn
jest.mock('../../lib/git.js')

describe('AWS CodeBuild Provider', function() {
it('can detect codebuild', function() {
Expand Down Expand Up @@ -29,17 +33,39 @@ describe('AWS CodeBuild Provider', function() {

it('throws if branch name cannot be detected', function() {
delete process.env.CODEBUILD_WEBHOOK_HEAD_REF
git.branch.mockImplementation(function() {
throw new Error()
})
expect(function() {
codebuild.configuration()
}).toThrow()
})

it('throws if pr number cannot be detected', function() {
it('Test build triggered via AWS SDK', function() {
delete process.env.CODEBUILD_WEBHOOK_HEAD_REF
git.branch.mockReturnValue('master')
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('Test build triggered via Github Webhook', function() {
process.env.CODEBUILD_WEBHOOK_HEAD_REF = 'refs/heads/master'
delete process.env.CODEBUILD_SOURCE_VERSION
expect(function() {
codebuild.configuration()
}).toThrow()
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: '1',
slug: 'my-org/my-project',
})
})

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

0 comments on commit 7c4cdc4

Please sign in to comment.