@@ -4,6 +4,7 @@ const os = require('os')
4
4
const { sep } = require ( 'path' )
5
5
const newman = require ( 'newman' )
6
6
const AWS = require ( 'aws-sdk' )
7
+ const path = require ( 'path' )
7
8
const codedeploy = new AWS . CodeDeploy ( { apiVersion : '2014-10-06' , region : 'us-west-2' } )
8
9
const s3 = new AWS . S3 ( { apiVersion : '2014-10-06' , region : 'us-west-2' } )
9
10
@@ -28,70 +29,53 @@ exports.handler = async function (event, context) {
28
29
// store the error so that we can update codedeploy lifecycle if there are any errors including errors from downloading files
29
30
let error
30
31
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 ) } `
49
43
} 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
+ }
52
55
}
53
56
}
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
+ }
67
70
}
68
71
}
72
+ console . log ( 'postman tests finished' )
69
73
}
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
93
78
}
94
-
95
79
if ( error ) throw error // Cause the lambda to "fail"
96
80
}
97
81
@@ -116,6 +100,9 @@ async function downloadFileFromPostman (type, id) {
116
100
}
117
101
118
102
async function downloadFileFromBucket ( key ) {
103
+ // Stripping relative path off of key.
104
+ key = path . basename ( key )
105
+
119
106
const filename = `${ tmpDir } ${ sep } ${ key } `
120
107
console . log ( `started download for ${ key } from s3 bucket` )
121
108
@@ -137,7 +124,9 @@ async function downloadFileFromBucket (key) {
137
124
138
125
function newmanRun ( options ) {
139
126
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
+ } )
141
130
} )
142
131
}
143
132
@@ -157,6 +146,30 @@ async function runTest (postmanCollection, postmanEnvironment) {
157
146
}
158
147
}
159
148
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
+
160
173
function sleep ( ms ) {
161
174
console . log ( 'started sleep timer' )
162
175
return new Promise ( resolve => setTimeout ( args => {
@@ -165,4 +178,4 @@ function sleep (ms) {
165
178
} , ms ) )
166
179
}
167
180
168
- // exports.handler({}, {})
181
+ // exports.handler({}, {}).then(() => {})
0 commit comments