@@ -2,15 +2,56 @@ import { firebase, functions } from '../config/firebase';
22import { Request } from 'firebase-functions/lib/providers/https' ;
33import { Response } from 'express-serve-static-core' ;
44import { Token } from '../config/token' ;
5+ import { BuildInfo , CiBuilds , ImageType } from '../model/ciBuilds' ;
6+ import { CiJobs } from '../model/ciJobs' ;
7+ import { Discord } from '../config/discord' ;
8+ import { EditorVersionInfo } from '../model/editorVersionInfo' ;
9+ import { RepoVersionInfo } from '../model/repoVersions' ;
510
611export const reportNewBuild = functions . https . onRequest ( async ( req : Request , res : Response ) => {
7- if ( ! Token . isValid ( req . header ( 'Authorisation' ) ) ) {
8- firebase . logger . warn ( 'unauthorised request' , req ) ;
9- res . status ( 403 ) . send ( 'Unauthorized' ) ;
10- return ;
11- }
12+ try {
13+ if ( ! Token . isValid ( req . header ( 'authorization' ) ) ) {
14+ firebase . logger . warn ( 'unauthorised request' , req . headers ) ;
15+ res . status ( 403 ) . send ( 'Unauthorized' ) ;
16+ return ;
17+ }
18+
19+ const { body } = req ;
20+ const { buildId, jobId, imageType, baseOs, repoVersion, editorVersion, targetPlatform } = body ;
21+ const buildInfo : BuildInfo = {
22+ baseOs,
23+ repoVersion,
24+ editorVersion,
25+ targetPlatform,
26+ } ;
1227
13- firebase . logger . info ( 'new build reported' , req ) ;
28+ if ( jobId === 'dryRun' ) {
29+ await createDryRunJob ( imageType , editorVersion ) ;
30+ }
1431
15- res . status ( 200 ) . send ( 'OK' ) ;
32+ await CiJobs . markJobAsInProgress ( jobId ) ;
33+ await CiBuilds . registerNewBuild ( buildId , jobId , imageType , buildInfo ) ;
34+
35+ firebase . logger . info ( 'new build reported' , body ) ;
36+ res . status ( 200 ) . send ( 'OK' ) ;
37+ } catch ( err ) {
38+ const message = `
39+ Something went wrong while wrong while reporting a new build.
40+ ${ err . message } (${ err . status } )\n${ err . stackTrace }
41+ ` ;
42+ firebase . logger . error ( message ) ;
43+ await Discord . sendAlert ( message ) ;
44+ res . status ( 500 ) . send ( 'Something went wrong' ) ;
45+ }
1646} ) ;
47+
48+ const createDryRunJob = async ( imageType : ImageType , editorVersion : string ) => {
49+ const repoVersionInfo = await RepoVersionInfo . getLatest ( ) ;
50+
51+ if ( imageType === 'editor' ) {
52+ const editorVersionInfo = await EditorVersionInfo . get ( editorVersion ) ;
53+ await CiJobs . create ( imageType , repoVersionInfo , editorVersionInfo ) ;
54+ } else {
55+ await CiJobs . create ( imageType , repoVersionInfo ) ;
56+ }
57+ } ;
0 commit comments