Skip to content
This repository was archived by the owner on Jan 10, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions lib/services/codebuild.js
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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