@@ -11,13 +11,22 @@ import { Job, JobStatus } from '../../../src/entities/job';
1111import { ECSContainer } from '../../../src/services/containerServices' ;
1212import { SQSConnector } from '../../../src/services/queue' ;
1313import { Batch } from '../../../src/services/batch' ;
14+ import { APIGatewayEvent , APIGatewayProxyResult , SQSEvent , SQSRecord } from 'aws-lambda' ;
1415
15- export const TriggerLocalBuild = async ( event : any = { } , context : any = { } ) : Promise < any > => {
16- const client = new mongodb . MongoClient ( c . get ( 'dbUrl' ) ) ;
17- await client . connect ( ) ;
18- const db = client . db ( c . get ( 'dbName' ) ) ;
16+ export const TriggerLocalBuild = async ( event : APIGatewayEvent ) : Promise < APIGatewayProxyResult > => {
1917 const consoleLogger = new ConsoleLogger ( ) ;
2018 const sqs = new SQSConnector ( consoleLogger , c ) ;
19+
20+ if ( ! event . body ) {
21+ const err = 'Trigger local build does not have a body in event payload' ;
22+ consoleLogger . error ( 'TriggerLocalBuildError' , err ) ;
23+ return {
24+ statusCode : 400 ,
25+ headers : { 'Content-Type' : 'text/plain' } ,
26+ body : err ,
27+ } ;
28+ }
29+
2130 const body = JSON . parse ( event . body ) ;
2231 try {
2332 consoleLogger . info ( body . jobId , 'enqueuing Job' ) ;
@@ -38,26 +47,23 @@ export const TriggerLocalBuild = async (event: any = {}, context: any = {}): Pro
3847 }
3948} ;
4049
41- // TODO: use @types /aws-lambda
42- export const HandleJobs = async ( event : any = { } ) : Promise < any > => {
50+ export const HandleJobs = async ( event : SQSEvent ) : Promise < void > => {
4351 /**
4452 * Check the status of the incoming jobs
4553 * if it is inqueue start a task
4654 * if it is inprogress call NotifyBuildProgress
4755 * if it is completed call NotifyBuildSummary
4856 */
49- const messages : JobQueueMessage [ ] = event . Records ;
57+ const messages = event . Records ;
5058 await Promise . all (
51- messages . map ( async ( message : any ) => {
59+ messages . map ( async ( message : SQSRecord ) => {
5260 const consoleLogger = new ConsoleLogger ( ) ;
5361 const body = JSON . parse ( message . body ) ;
54- let queueUrl = '' ;
5562 const jobId = body [ 'jobId' ] ;
5663 const jobStatus = body [ 'jobStatus' ] ;
5764 try {
5865 switch ( jobStatus ) {
5966 case JobStatus [ JobStatus . inQueue ] :
60- queueUrl = c . get ( 'jobsQueueUrl' ) ;
6167 await NotifyBuildProgress ( jobId ) ;
6268 // start the task , don't start the process before processing the notification
6369 const ecsServices = new ECSContainer ( c , consoleLogger ) ;
@@ -68,7 +74,6 @@ export const HandleJobs = async (event: any = {}): Promise<any> => {
6874 consoleLogger . info ( jobId , JSON . stringify ( res ) ) ;
6975 break ;
7076 case JobStatus [ JobStatus . inProgress ] :
71- queueUrl = c . get ( 'jobUpdatesQueueUrl' ) ;
7277 await NotifyBuildProgress ( jobId ) ;
7378 break ;
7479 case JobStatus [ JobStatus . timedOut ] :
@@ -80,9 +85,7 @@ export const HandleJobs = async (event: any = {}): Promise<any> => {
8085 break ;
8186 case JobStatus [ JobStatus . failed ] :
8287 case JobStatus [ JobStatus . completed ] :
83- queueUrl = c . get ( 'jobUpdatesQueueUrl' ) ;
84- await NotifyBuildSummary ( jobId ) ;
85- await SubmitArchiveJob ( jobId ) ;
88+ await Promise . all ( [ NotifyBuildSummary ( jobId ) , SubmitArchiveJob ( jobId ) ] ) ;
8689 break ;
8790 default :
8891 consoleLogger . error ( jobId , 'Invalid status' ) ;
@@ -134,42 +137,35 @@ async function stopECSTask(taskId: string, consoleLogger: ConsoleLogger) {
134137 await ecs . stopZombieECSTask ( taskId ) ;
135138}
136139
137- async function retry ( message : JobQueueMessage , consoleLogger : ConsoleLogger , url : string ) : Promise < any > {
138- try {
139- const tries = message . tries ;
140- // TODO: c.get('maxRetries') is of type 'Unknown', needs validation
141- if ( tries < c . get ( 'maxRetries' ) ) {
142- const sqs = new SQSConnector ( consoleLogger , c ) ;
143- message [ 'tries' ] += 1 ;
144- let retryDelay = 10 ;
145- if ( c . get ( 'retryDelay' ) ) {
146- retryDelay = c . get ( 'retryDelay' ) ;
147- }
148- await sqs . sendMessage ( message , url , retryDelay * tries ) ;
149- }
150- } catch ( err ) {
151- consoleLogger . error ( message [ 'jobId' ] , err ) ;
152- }
153- }
154- async function NotifyBuildSummary ( jobId : string ) : Promise < any > {
140+ async function NotifyBuildSummary ( jobId : string ) : Promise < void > {
155141 const consoleLogger = new ConsoleLogger ( ) ;
156142 const client = new mongodb . MongoClient ( c . get ( 'dbUrl' ) ) ;
157143 await client . connect ( ) ;
158144 const db = client . db ( c . get ( 'dbName' ) ) ;
159145 const env = c . get < string > ( 'env' ) ;
160146
161147 const jobRepository = new JobRepository ( db , c , consoleLogger ) ;
162- // TODO: Make fullDocument be of type Job, validate existence
163148 const fullDocument = await jobRepository . getJobById ( jobId ) ;
149+
150+ if ( ! fullDocument ) {
151+ consoleLogger . error (
152+ `NotifyBuildSummary_${ jobId } ` ,
153+ `Notify build summary failed. Job does not exist for Job ID: ${ jobId } `
154+ ) ;
155+ return ;
156+ }
157+
164158 const repoName = fullDocument . payload . repoName ;
165159 const username = fullDocument . user ;
166160 const slackConnector = new SlackConnector ( consoleLogger , c ) ;
167161 const repoEntitlementRepository = new RepoEntitlementsRepository ( db , c , consoleLogger ) ;
168162 const entitlement = await repoEntitlementRepository . getSlackUserIdByGithubUsername ( username ) ;
163+
169164 if ( ! entitlement ?. [ 'slack_user_id' ] ) {
170165 consoleLogger . error ( username , 'Entitlement failed' ) ;
171166 return ;
172167 }
168+
173169 await slackConnector . sendMessage (
174170 await prepSummaryMessage (
175171 env ,
@@ -181,9 +177,6 @@ async function NotifyBuildSummary(jobId: string): Promise<any> {
181177 ) ,
182178 entitlement [ 'slack_user_id' ]
183179 ) ;
184- return {
185- statusCode : 200 ,
186- } ;
187180}
188181
189182export const extractUrlFromMessage = ( fullDocument ) : string [ ] => {
@@ -201,12 +194,11 @@ async function prepSummaryMessage(
201194 failed = false
202195) : Promise < string > {
203196 const urls = extractUrlFromMessage ( fullDocument ) ;
204- let mms_urls = [ null , null ] ;
197+ let mms_urls : Array < string | null > = [ null , null ] ;
205198 // mms-docs needs special handling as it builds two sites (cloudmanager & ops manager)
206199 // so we need to extract both URLs
207200 if ( repoName === 'mms-docs' ) {
208201 if ( urls . length >= 2 ) {
209- // TODO: Type 'string[]' is not assignable to type 'null[]'.
210202 mms_urls = urls . slice ( - 2 ) ;
211203 }
212204 }
@@ -257,15 +249,24 @@ function prepProgressMessage(
257249 }
258250}
259251
260- async function NotifyBuildProgress ( jobId : string ) : Promise < any > {
252+ async function NotifyBuildProgress ( jobId : string ) : Promise < void > {
261253 const consoleLogger = new ConsoleLogger ( ) ;
262254 const client = new mongodb . MongoClient ( c . get ( 'dbUrl' ) ) ;
263255 await client . connect ( ) ;
264256 const db = client . db ( c . get ( 'dbName' ) ) ;
265257 const slackConnector = new SlackConnector ( consoleLogger , c ) ;
266258 const jobRepository = new JobRepository ( db , c , consoleLogger ) ;
267- // TODO: Make fullDocument be of type Job, validate existence
259+
268260 const fullDocument = await jobRepository . getJobById ( jobId ) ;
261+
262+ if ( ! fullDocument ) {
263+ consoleLogger . error (
264+ `NotifyBuildProgress_${ jobId } ` ,
265+ `Notify build progress failed. Job does not exist for Job ID: ${ jobId } `
266+ ) ;
267+ return ;
268+ }
269+
269270 const jobTitle = fullDocument . title ;
270271 const username = fullDocument . user ;
271272 const repoEntitlementRepository = new RepoEntitlementsRepository ( db , c , consoleLogger ) ;
@@ -274,7 +275,8 @@ async function NotifyBuildProgress(jobId: string): Promise<any> {
274275 consoleLogger . error ( username , 'Entitlement Failed' ) ;
275276 return ;
276277 }
277- const resp = await slackConnector . sendMessage (
278+
279+ await slackConnector . sendMessage (
278280 prepProgressMessage (
279281 c . get ( 'dashboardUrl' ) ,
280282 jobId ,
@@ -284,9 +286,6 @@ async function NotifyBuildProgress(jobId: string): Promise<any> {
284286 ) ,
285287 entitlement [ 'slack_user_id' ]
286288 ) ;
287- return {
288- statusCode : 200 ,
289- } ;
290289}
291290
292291function getMongoClient ( config : IConfig ) : mongodb . MongoClient {
@@ -317,6 +316,15 @@ async function SubmitArchiveJob(jobId: string) {
317316 branches : new BranchRepository ( db , c , consoleLogger ) ,
318317 } ;
319318 const job = await models . jobs . getJobById ( jobId ) ;
319+
320+ if ( ! job ) {
321+ consoleLogger . error (
322+ `SubmitArchiveJob_${ jobId } ` ,
323+ `Submit archive job failed. Job does not exist for Job ID: ${ jobId } `
324+ ) ;
325+ return ;
326+ }
327+
320328 const repo = await models . branches . getRepo ( job . payload . repoName ) ;
321329
322330 /* NOTE
0 commit comments