11'use strict'
22
33const AWS = require ( 'aws-sdk' ) ,
4- stepfunctions = new AWS . StepFunctions ( )
4+ codebuild = new AWS . CodeBuild ( )
55const GitHubApi = require ( 'github' ) ,
66 github = new GitHubApi ( { version : '3.0.0' } )
77const ghUrl = require ( 'parse-github-url' ) ,
@@ -26,39 +26,45 @@ exports.handler = (event, context, callback) => {
2626 callback ( )
2727 }
2828
29- // Create GitHub pending status
29+ // Execute CodeBuild Project
3030 const sha = ghEvent . pull_request ? ghEvent . pull_request . head . sha : ghEvent . head_commit . id
31- const p = new Promise ( ( resolve ) => {
32- github . repos . createStatus ( {
33- owner : repo . owner ,
34- repo : repo . name ,
35- sha : sha ,
36- state : 'pending' ,
37- target_url : `https://${ region } .console.aws.amazon.com/codebuild/home?region=${ region } #/builds` ,
38- context : 'codebuild' ,
39- description : 'AWS CodeBuild is preparing your tests...'
40- } ) . then ( ( data ) => {
41- console . log ( data )
42- resolve ( 'status created' )
43- } ) . catch ( ( err ) => {
44- console . log ( err )
45- resolve ( 'status creation failed' )
46- } )
47- } )
48-
49- // Execute Step Functions
50- p . then ( ( ) => {
51- const params = {
52- stateMachineArn : process . env . STEP_FUNCTIONS_ARN ,
53- input : JSON . stringify ( ghEvent )
54- }
55- stepfunctions . startExecution ( params , function ( err , data ) {
56- if ( err ) {
57- console . log ( err , err . stack )
31+ const params = {
32+ projectName : process . env . CODEBUILD_PROJECT_NAME ,
33+ sourceVersion : sha
34+ }
35+ codebuild . startBuild ( params , ( err , data ) => {
36+ // Create GitHub status
37+ if ( err ) {
38+ // If CodeBuild couldn't start a build, save GitHub status as 'error'
39+ console . log ( err , err . stack )
40+ github . repos . createStatus ( {
41+ owner : repo . owner ,
42+ repo : repo . name ,
43+ sha : sha ,
44+ state : 'error' ,
45+ target_url : `https://${ region } .console.aws.amazon.com/codebuild/home?region=${ region } #/builds` ,
46+ context : 'codebuild' ,
47+ description : 'AWS CodeBuild has failed to start your tests'
48+ } ) . then ( ( ) => {
5849 callback ( err )
59- } else {
50+ } ) . catch ( ( err ) => {
51+ callback ( err )
52+ } )
53+ } else {
54+ github . repos . createStatus ( {
55+ owner : repo . owner ,
56+ repo : repo . name ,
57+ sha : sha ,
58+ state : 'pending' ,
59+ target_url : `https://${ region } .console.aws.amazon.com/codebuild/home?region=${ region } #/builds/${ data . build . id } /view/new` ,
60+ context : 'codebuild' ,
61+ description : 'AWS CodeBuild is running your tests'
62+ } ) . then ( ( data ) => {
6063 callback ( null , data )
61- }
62- } )
64+ } ) . catch ( ( err ) => {
65+ console . log ( err , err . stack )
66+ callback ( err )
67+ } )
68+ }
6369 } )
6470}
0 commit comments