@@ -4,6 +4,7 @@ const os = require('os')
44const { sep } = require ( 'path' )
55const newman = require ( 'newman' )
66const AWS = require ( 'aws-sdk' )
7+ const path = require ( 'path' )
78const codedeploy = new AWS . CodeDeploy ( { apiVersion : '2014-10-06' , region : 'us-west-2' } )
89const s3 = new AWS . S3 ( { apiVersion : '2014-10-06' , region : 'us-west-2' } )
910
@@ -28,70 +29,53 @@ exports.handler = async function (event, context) {
2829 // store the error so that we can update codedeploy lifecycle if there are any errors including errors from downloading files
2930 let error
3031
31- const postmanCollections = process . env . POSTMAN_COLLECTIONS
32- if ( ! postmanCollections ) {
33- error = new Error ( 'Env variable POSTMAN_COLLECTIONS is required' )
34- } else {
35- const postmanList = JSON . parse ( postmanCollections )
36- const promises = [ timer ]
37- for ( const each of postmanList ) {
38- if ( each . collection . includes ( '.json' ) ) {
39- promises . push ( downloadFileFromBucket ( each . collection ) )
40- each . collection = `${ tmpDir } ${ sep } ${ each . collection } `
41- } else {
42- promises . push ( downloadFileFromPostman ( 'collection' , each . collection ) )
43- each . collection = `${ tmpDir } ${ sep } ${ each . collection } .json`
44- }
45- if ( each . environment ) { // environment can be null
46- if ( each . environment . includes ( '.json' ) ) {
47- promises . push ( downloadFileFromBucket ( each . environment ) )
48- each . environment = `${ tmpDir } ${ sep } ${ each . environment } `
32+ try {
33+ const postmanCollections = process . env . POSTMAN_COLLECTIONS
34+ if ( ! postmanCollections ) {
35+ error = new Error ( 'Env variable POSTMAN_COLLECTIONS is required' )
36+ } else {
37+ const postmanList = JSON . parse ( postmanCollections )
38+ const promises = [ timer ]
39+ for ( const each of postmanList ) {
40+ if ( each . collection . includes ( '.json' ) ) {
41+ promises . push ( downloadFileFromBucket ( each . collection ) )
42+ each . collection = `${ tmpDir } ${ sep } ${ path . basename ( each . collection ) } `
4943 } else {
50- promises . push ( downloadFileFromPostman ( 'environment' , each . environment ) )
51- each . environment = `${ tmpDir } ${ sep } ${ each . environment } .json`
44+ promises . push ( downloadFileFromPostman ( 'collection' , each . collection ) )
45+ each . collection = `${ tmpDir } ${ sep } ${ path . basename ( each . collection ) } .json`
46+ }
47+ if ( each . environment ) { // environment can be null
48+ if ( each . environment . includes ( '.json' ) ) {
49+ promises . push ( downloadFileFromBucket ( each . environment ) )
50+ each . environment = `${ tmpDir } ${ sep } ${ path . basename ( each . environment ) } `
51+ } else {
52+ promises . push ( downloadFileFromPostman ( 'environment' , each . environment ) )
53+ each . environment = `${ tmpDir } ${ sep } ${ path . basename ( each . environment ) } .json`
54+ }
5255 }
5356 }
54- }
55- // make sure all files are downloaded and we wait for 10 seconds before executing postman tests
56- await Promise . all ( promises )
57-
58- console . log ( 'starting postman tests ...' )
59- if ( ! error ) {
60- // no need to run tests if files weren't downloaded correctly
61- for ( const each of postmanList ) {
62- if ( ! error ) {
63- // don't run later collections if previous one errored out
64- await runTest ( each . collection , each . environment ) . catch ( err => {
65- error = err
66- } )
57+ // make sure all files are downloaded and we wait for 10 seconds before executing postman tests
58+ await Promise . all ( promises )
59+
60+ console . log ( 'starting postman tests ...' )
61+ if ( ! error ) {
62+ // no need to run tests if files weren't downloaded correctly
63+ for ( const each of postmanList ) {
64+ if ( ! error ) {
65+ // don't run later collections if previous one errored out
66+ await runTest ( each . collection , each . environment ) . catch ( err => {
67+ error = err
68+ } )
69+ }
6770 }
6871 }
72+ console . log ( 'postman tests finished' )
6973 }
70- console . log ( 'postman tests finished' )
71- }
72-
73- if ( deploymentId ) {
74- console . log ( 'starting to update CodeDeploy lifecycle event hook status...' )
75- const params = {
76- deploymentId : event . DeploymentId ,
77- lifecycleEventHookExecutionId : event . LifecycleEventHookExecutionId ,
78- status : error ? 'Failed' : 'Succeeded'
79- }
80- try {
81- const data = await codedeploy . putLifecycleEventHookExecutionStatus ( params ) . promise ( )
82- console . log ( data )
83- } catch ( err ) {
84- console . log ( err , err . stack )
85- throw err
86- }
87- } else if ( combinedRunner ) {
88- return {
89- passed : ! error
90- }
91- } else {
92- console . log ( 'No deployment ID found in the event. Skipping update to CodeDeploy lifecycle hook...' )
74+ await updateRunner ( deploymentId , combinedRunner , event , error )
75+ } catch ( e ) {
76+ await updateRunner ( deploymentId , combinedRunner , event , true )
77+ throw e
9378 }
94-
9579 if ( error ) throw error // Cause the lambda to "fail"
9680}
9781
@@ -116,6 +100,9 @@ async function downloadFileFromPostman (type, id) {
116100}
117101
118102async function downloadFileFromBucket ( key ) {
103+ // Stripping relative path off of key.
104+ key = path . basename ( key )
105+
119106 const filename = `${ tmpDir } ${ sep } ${ key } `
120107 console . log ( `started download for ${ key } from s3 bucket` )
121108
@@ -137,7 +124,9 @@ async function downloadFileFromBucket (key) {
137124
138125function newmanRun ( options ) {
139126 return new Promise ( ( resolve , reject ) => {
140- newman . run ( options , err => { err ? reject ( err ) : resolve ( ) } )
127+ newman . run ( options , err => {
128+ err ? reject ( err ) : resolve ( )
129+ } )
141130 } )
142131}
143132
@@ -157,6 +146,30 @@ async function runTest (postmanCollection, postmanEnvironment) {
157146 }
158147}
159148
149+ async function updateRunner ( deploymentId , combinedRunner , event , error ) {
150+ if ( deploymentId ) {
151+ console . log ( 'starting to update CodeDeploy lifecycle event hook status...' )
152+ const params = {
153+ deploymentId : deploymentId ,
154+ lifecycleEventHookExecutionId : event . LifecycleEventHookExecutionId ,
155+ status : error ? 'Failed' : 'Succeeded'
156+ }
157+ try {
158+ const data = await codedeploy . putLifecycleEventHookExecutionStatus ( params ) . promise ( )
159+ console . log ( data )
160+ } catch ( err ) {
161+ console . log ( err , err . stack )
162+ throw err
163+ }
164+ } else if ( combinedRunner ) {
165+ return {
166+ passed : ! error
167+ }
168+ } else {
169+ console . log ( 'No deployment ID found in the event. Skipping update to CodeDeploy lifecycle hook...' )
170+ }
171+ }
172+
160173function sleep ( ms ) {
161174 console . log ( 'started sleep timer' )
162175 return new Promise ( resolve => setTimeout ( args => {
@@ -165,4 +178,4 @@ function sleep (ms) {
165178 } , ms ) )
166179}
167180
168- // exports.handler({}, {})
181+ // exports.handler({}, {}).then(() => {})
0 commit comments