Skip to content

Commit 8895a01

Browse files
committed
adding aws codebuild provider
1 parent bc58377 commit 8895a01

File tree

3 files changed

+136
-0
lines changed

3 files changed

+136
-0
lines changed

src/ci_providers/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const providerAzurepipelines = require('./provider_azurepipelines')
33
const providerBitbucket = require('./provider_bitbucket')
44
const providerBuildkite = require('./provider_buildkite')
55
const providerCircleci = require('./provider_circleci')
6+
const providerCodeBuild = require('./provider_codebuild')
67
const providerDrone = require('./provider_drone')
78
const providerGitHubactions = require('./provider_githubactions')
89
const providerGitLabci = require('./provider_gitlabci')
@@ -17,6 +18,7 @@ const providers = [
1718
providerBitbucket,
1819
providerBuildkite,
1920
providerCircleci,
21+
providerCodeBuild,
2022
providerDrone,
2123
providerGitHubactions,
2224
providerGitLabci,
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
function detect(envs) {
2+
return !!envs.CODEBUILD_CI
3+
}
4+
5+
function _getBuild(inputs) {
6+
const { args, envs } = inputs
7+
return args.build || envs.CODEBUILD_BUILD_ID
8+
}
9+
10+
// eslint-disable-next-line no-unused-vars
11+
function _getBuildURL(inputs) {
12+
return ''
13+
}
14+
15+
function _getBranch(inputs) {
16+
const { args, envs } = inputs
17+
return args.branch || envs.CODEBUILD_WEBHOOK_HEAD_REF
18+
? envs.CODEBUILD_WEBHOOK_HEAD_REF.replace(/^refs\/heads\//, '')
19+
: ''
20+
}
21+
22+
function _getJob(envs) {
23+
return envs.CODEBUILD_BUILD_ID
24+
}
25+
26+
function _getPR(inputs) {
27+
const { args, envs } = inputs
28+
return args.pr ||
29+
(envs.CODEBUILD_WEBHOOK_HEAD_REF &&
30+
envs.CODEBUILD_SOURCE_VERSION.startsWith('pr/'))
31+
? envs.CODEBUILD_SOURCE_VERSION.replace(/^pr\//, '')
32+
: ''
33+
}
34+
35+
function _getService() {
36+
return 'codebuild'
37+
}
38+
39+
function getServiceName() {
40+
return 'AWS CodeBuild'
41+
}
42+
43+
function _getSHA(inputs) {
44+
const { args, envs } = inputs
45+
return args.sha || envs.CODEBUILD_RESOLVED_SOURCE_VERSION || ''
46+
}
47+
48+
function _getSlug(inputs) {
49+
const { args, envs } = inputs
50+
return args.slug || envs.CODEBUILD_SOURCE_REPO_URL
51+
? envs.CODEBUILD_SOURCE_REPO_URL.replace(/^.*github.com\//, '').replace(
52+
/\.git$/,
53+
'',
54+
)
55+
: ''
56+
}
57+
58+
function getServiceParams(inputs) {
59+
return {
60+
branch: _getBranch(inputs),
61+
build: _getBuild(inputs),
62+
buildURL: _getBuildURL(inputs),
63+
commit: _getSHA(inputs),
64+
job: _getJob(inputs.envs),
65+
pr: _getPR(inputs),
66+
service: _getService(),
67+
slug: _getSlug(inputs),
68+
}
69+
}
70+
71+
module.exports = {
72+
detect,
73+
getServiceName,
74+
getServiceParams,
75+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
const td = require('testdouble')
2+
3+
const providerCodeBuild = require('../../src/ci_providers/provider_codebuild')
4+
5+
describe('CodeBuild Params', () => {
6+
afterEach(() => {
7+
td.reset()
8+
})
9+
10+
describe('detect()', () => {
11+
it('does not run without CodeBuild env variable', () => {
12+
const inputs = {
13+
args: {},
14+
envs: {},
15+
}
16+
const detected = providerCodeBuild.detect(inputs.envs)
17+
expect(detected).toBeFalsy()
18+
})
19+
20+
it('does run with CodeBuild env variable', () => {
21+
const inputs = {
22+
args: {},
23+
envs: {
24+
CI: true,
25+
CODEBUILD_CI: true,
26+
},
27+
}
28+
const detected = providerCodeBuild.detect(inputs.envs)
29+
expect(detected).toBeTruthy()
30+
})
31+
})
32+
33+
it('gets correct params', () => {
34+
const inputs = {
35+
args: {},
36+
envs: {
37+
CI: true,
38+
CODEBUILD_CI: true,
39+
CODEBUILD_WEBHOOK_HEAD_REF: 'refs/heads/master',
40+
CODEBUILD_RESOLVED_SOURCE_VERSION: 'testingsha',
41+
CODEBUILD_BUILD_ID: 2,
42+
CODEBUILD_SOURCE_VERSION: 'pr/1',
43+
CODEBUILD_SOURCE_REPO_URL: 'https://github.com/repo.git',
44+
},
45+
}
46+
const expected = {
47+
branch: 'master',
48+
build: 2,
49+
buildURL: '',
50+
commit: 'testingsha',
51+
job: 2,
52+
pr: '1',
53+
service: 'codebuild',
54+
slug: 'repo',
55+
}
56+
const params = providerCodeBuild.getServiceParams(inputs)
57+
expect(params).toMatchObject(expected)
58+
})
59+
})

0 commit comments

Comments
 (0)