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

Add support for AWS CodeBuild #135

Merged
merged 3 commits into from
Sep 20, 2019
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
1 change: 1 addition & 0 deletions lib/detect.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ var services = {
gitlab: require('./services/gitlab'),
heroku: require('./services/heroku'),
teamcity: require('./services/teamcity'),
codebuild: require('./services/codebuild'),
}

var detectProvider = function() {
Expand Down
42 changes: 42 additions & 0 deletions lib/services/codebuild.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
module.exports = {
detect: function() {
return !!process.env.CODEBUILD_CI
},

configuration: function() {
console.log(' AWS CodeBuild Detected')
return {
service: 'codebuild',
build: process.env.CODEBUILD_BUILD_ID,
job: process.env.CODEBUILD_BUILD_ID,
commit: process.env.CODEBUILD_RESOLVED_SOURCE_VERSION,
branch: detectBranchName(),
pr: detectPRNumber(),
slug: detectRepoSlug(),
}
function detectBranchName() {
if (process.env.CODEBUILD_WEBHOOK_HEAD_REF) {
return process.env.CODEBUILD_WEBHOOK_HEAD_REF.replace(
/^refs\/heads\//,
''
)
}
throw new Error('Cannot detect branch name.')
}
function detectPRNumber() {
if (process.env.CODEBUILD_SOURCE_VERSION) {
return process.env.CODEBUILD_SOURCE_VERSION.replace(/^pr\//, '')
}
throw new Error('Cannot detect PR number.')
}
function detectRepoSlug() {
if (process.env.CODEBUILD_SOURCE_REPO_URL) {
return process.env.CODEBUILD_SOURCE_REPO_URL.replace(
/^.*github.com\//,
''
).replace(/\.git$/, '')
}
throw new Error('Cannot detect repository slug.')
}
},
}
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

53 changes: 53 additions & 0 deletions test/services/codebuild.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
var codebuild = require('../../lib/services/codebuild')

describe('AWS CodeBuild Provider', function() {
it('can detect codebuild', function() {
process.env.CODEBUILD_CI = 'true'
expect(codebuild.detect()).toBe(true)
})

it('can get codebuild env info', function() {
process.env.CODEBUILD_CI = 'true'
process.env.CODEBUILD_BUILD_ID =
'my-project:e016b9d9-f2c8-4749-8373-7ca673b6d969'
process.env.CODEBUILD_RESOLVED_SOURCE_VERSION =
'39ec2418eca4c539d765574a1c68f3bd77e8c549'
process.env.CODEBUILD_WEBHOOK_HEAD_REF = 'refs/heads/master'
process.env.CODEBUILD_SOURCE_VERSION = 'pr/1'
process.env.CODEBUILD_SOURCE_REPO_URL =
'https://github.com/my-org/my-project.git'
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 branch name cannot be detected', function() {
delete process.env.CODEBUILD_WEBHOOK_HEAD_REF
expect(function() {
codebuild.configuration()
}).toThrow()
})

it('throws if pr number cannot be detected', function() {
process.env.CODEBUILD_WEBHOOK_HEAD_REF = 'refs/heads/master'
delete process.env.CODEBUILD_SOURCE_VERSION
expect(function() {
codebuild.configuration()
}).toThrow()
})

it('throws if slug cannot be detected', function() {
process.env.CODEBUILD_RESOLVED_SOURCE_VERSION =
'39ec2418eca4c539d765574a1c68f3bd77e8c549'
delete process.env.CODEBUILD_SOURCE_REPO_URL
expect(function() {
codebuild.configuration()
}).toThrow()
})
})