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

Commit

Permalink
Add support for AWS CodeBuild
Browse files Browse the repository at this point in the history
  • Loading branch information
iansu committed Jun 18, 2019
1 parent 8371836 commit 3faacd4
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 2 deletions.
3 changes: 2 additions & 1 deletion lib/detect.js
Expand Up @@ -13,7 +13,8 @@ var services = {
snap: require('./services/snap'),
gitlab: require('./services/gitlab'),
heroku: require('./services/heroku'),
teamcity: require('./services/teamcity')
teamcity: require('./services/teamcity'),
codebuild: require('./services/codebuild'),
}

var detectProvider = function() {
Expand Down
42 changes: 42 additions & 0 deletions lib/services/codebuild.js
@@ -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.

50 changes: 50 additions & 0 deletions test/services/codebuild.test.js
@@ -0,0 +1,50 @@
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() {
delete process.env.CODEBUILD_SOURCE_VERSION
expect(function() {
codebuild.configuration()
}).toThrow()
})

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

0 comments on commit 3faacd4

Please sign in to comment.