Skip to content

Commit 7441bed

Browse files
Merge pull request #194 from eddiemoore/feature/provider-codebuild
Adding AWS Codebuild provider
2 parents 9ee77b4 + f1559db commit 7441bed

File tree

3 files changed

+180
-0
lines changed

3 files changed

+180
-0
lines changed

src/ci_providers/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const providerBitbucket = require('./provider_bitbucket')
44
const providerBuildkite = require('./provider_buildkite')
55
const providerCircleci = require('./provider_circleci')
66
const providerCirrus = require('./provider_cirrus')
7+
const providerCodeBuild = require('./provider_codebuild')
78
const providerDrone = require('./provider_drone')
89
const providerGitHubactions = require('./provider_githubactions')
910
const providerGitLabci = require('./provider_gitlabci')
@@ -21,6 +22,7 @@ const providers = [
2122
providerBuildkite,
2223
providerCircleci,
2324
providerCirrus,
25+
providerCodeBuild,
2426
providerDrone,
2527
providerGitHubactions,
2628
providerGitLabci,
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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 (
18+
args.branch ||
19+
(envs.CODEBUILD_WEBHOOK_HEAD_REF
20+
? envs.CODEBUILD_WEBHOOK_HEAD_REF.replace(/^refs\/heads\//, '')
21+
: '')
22+
)
23+
}
24+
25+
function _getJob(envs) {
26+
return envs.CODEBUILD_BUILD_ID
27+
}
28+
29+
function _getPR(inputs) {
30+
const { args, envs } = inputs
31+
return (
32+
args.pr ||
33+
(envs.CODEBUILD_SOURCE_VERSION &&
34+
envs.CODEBUILD_SOURCE_VERSION.startsWith('pr/')
35+
? envs.CODEBUILD_SOURCE_VERSION.replace(/^pr\//, '')
36+
: '')
37+
)
38+
}
39+
40+
function _getService() {
41+
return 'codebuild'
42+
}
43+
44+
function getServiceName() {
45+
return 'AWS CodeBuild'
46+
}
47+
48+
function _getSHA(inputs) {
49+
const { args, envs } = inputs
50+
return args.sha || envs.CODEBUILD_RESOLVED_SOURCE_VERSION || ''
51+
}
52+
53+
function _getSlug(inputs) {
54+
const { args, envs } = inputs
55+
return (
56+
args.slug ||
57+
(envs.CODEBUILD_SOURCE_REPO_URL
58+
? envs.CODEBUILD_SOURCE_REPO_URL.replace(/^.*github.com\//, '').replace(
59+
/\.git$/,
60+
'',
61+
)
62+
: '')
63+
)
64+
}
65+
66+
function getServiceParams(inputs) {
67+
return {
68+
branch: _getBranch(inputs),
69+
build: _getBuild(inputs),
70+
buildURL: _getBuildURL(inputs),
71+
commit: _getSHA(inputs),
72+
job: _getJob(inputs.envs),
73+
pr: _getPR(inputs),
74+
service: _getService(),
75+
slug: _getSlug(inputs),
76+
}
77+
}
78+
79+
module.exports = {
80+
detect,
81+
getServiceName,
82+
getServiceParams,
83+
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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+
describe('getServiceParams()', () => {
34+
it('gets correct params', () => {
35+
const inputs = {
36+
args: {},
37+
envs: {
38+
CI: true,
39+
CODEBUILD_CI: true,
40+
CODEBUILD_WEBHOOK_HEAD_REF: 'refs/heads/master',
41+
CODEBUILD_RESOLVED_SOURCE_VERSION: 'testingsha',
42+
CODEBUILD_BUILD_ID: 2,
43+
CODEBUILD_SOURCE_VERSION: 'pr/1',
44+
CODEBUILD_SOURCE_REPO_URL: 'https://github.com/repo.git',
45+
},
46+
}
47+
const expected = {
48+
branch: 'master',
49+
build: 2,
50+
buildURL: '',
51+
commit: 'testingsha',
52+
job: 2,
53+
pr: '1',
54+
service: 'codebuild',
55+
slug: 'repo',
56+
}
57+
const params = providerCodeBuild.getServiceParams(inputs)
58+
expect(params).toMatchObject(expected)
59+
})
60+
61+
it('gets correct params for overrides', () => {
62+
const inputs = {
63+
args: {
64+
branch: 'branch',
65+
build: 3,
66+
pr: '7',
67+
sha: 'testsha',
68+
slug: 'testOrg/testRepo',
69+
},
70+
envs: {
71+
CI: true,
72+
CODEBUILD_CI: true,
73+
CODEBUILD_WEBHOOK_HEAD_REF: 'refs/heads/master',
74+
CODEBUILD_RESOLVED_SOURCE_VERSION: 'testingsha',
75+
CODEBUILD_BUILD_ID: 2,
76+
CODEBUILD_SOURCE_VERSION: 'pr/1',
77+
CODEBUILD_SOURCE_REPO_URL: 'https://github.com/repo.git',
78+
},
79+
}
80+
const expected = {
81+
branch: 'branch',
82+
build: 3,
83+
buildURL: '',
84+
commit: 'testsha',
85+
job: 2,
86+
pr: '7',
87+
service: 'codebuild',
88+
slug: 'testOrg/testRepo',
89+
}
90+
91+
const params = providerCodeBuild.getServiceParams(inputs)
92+
expect(params).toMatchObject(expected)
93+
})
94+
})
95+
})

0 commit comments

Comments
 (0)