@@ -7,6 +7,7 @@ import { IRepoConnector } from "../services/repo";
77import { IFileSystemServices } from "../services/fileServices" ;
88import { AutoBuilderError , InvalidJobError , JobStoppedError , PublishError } from "../errors/errors" ;
99import { IConfig } from "config" ;
10+ import { IJobValidator } from "./jobValidator" ;
1011var fs = require ( 'fs' ) ;
1112
1213export abstract class JobHandler {
@@ -19,7 +20,7 @@ export abstract class JobHandler {
1920 protected get commandExecutor ( ) : IJobCommandExecutor {
2021 return this . _commandExecutor ;
2122 }
22-
23+
2324 protected _cdnConnector : ICDNConnector ;
2425
2526 private _repoConnector : IRepoConnector ;
@@ -34,9 +35,9 @@ export abstract class JobHandler {
3435 return this . _jobRepository ;
3536 }
3637 private _fileSystemServices : IFileSystemServices ;
37-
38+
3839 private _shouldStop : boolean ;
39-
40+
4041 private _stopped : boolean ;
4142 public get stopped ( ) : boolean {
4243 return this . _stopped ;
@@ -45,12 +46,14 @@ export abstract class JobHandler {
4546 this . _stopped = value ;
4647 }
4748
49+ private _validator : IJobValidator ;
50+
4851 protected _config : IConfig ;
4952
50- protected name :string ;
53+ protected name : string ;
5154
5255 constructor ( job : IJob , config : IConfig , jobRepository : JobRepository , fileSystemServices : IFileSystemServices , commandExecutor : IJobCommandExecutor ,
53- cdnConnector : ICDNConnector , repoConnector : IRepoConnector , logger : IJobRepoLogger ) {
56+ cdnConnector : ICDNConnector , repoConnector : IRepoConnector , logger : IJobRepoLogger , validator : IJobValidator ) {
5457 this . _commandExecutor = commandExecutor ;
5558 this . _cdnConnector = cdnConnector ;
5659 this . _repoConnector = repoConnector ;
@@ -60,11 +63,12 @@ export abstract class JobHandler {
6063 this . _fileSystemServices = fileSystemServices ;
6164 this . _shouldStop = false ;
6265 this . _config = config ;
66+ this . _validator = validator ;
6367 }
6468
6569 abstract prepStageSpecificNextGenCommands ( ) : void ;
6670
67- private async update ( publishResult :CommandExecutorResponse ) : Promise < void > {
71+ private async update ( publishResult : CommandExecutorResponse ) : Promise < void > {
6872 if ( publishResult && publishResult . status === 'success' ) {
6973 let files = this . _fileSystemServices . getFilesInDirectory ( `./${ this . currJob . payload . repoName } /build/public` , '' , null , null ) ;
7074 await this . jobRepository . updateWithCompletionStatus ( this . currJob . _id , files ) ;
@@ -92,7 +96,7 @@ export abstract class JobHandler {
9296 @throwIfJobInterupted ( )
9397 private async logError ( error ) : Promise < void > {
9498 await this . _logger . save ( this . currJob . _id , `${ '(BUILD)' . padEnd ( 15 ) } failed with code: ${ error . code } . ` ) ;
95- await this . _logger . save ( this . currJob . _id , `${ '(BUILD)' . padEnd ( 15 ) } stdErr: ${ error . stderr } ` ) ;
99+ await this . _logger . save ( this . currJob . _id , `${ '(BUILD)' . padEnd ( 15 ) } stdErr: ${ error . stderr } ` ) ;
96100 }
97101
98102 @throwIfJobInterupted ( )
@@ -164,13 +168,17 @@ export abstract class JobHandler {
164168 @throwIfJobInterupted ( )
165169 private async prepNextGenBuild ( ) : Promise < void > {
166170 if ( this . isbuildNextGen ( ) ) {
171+ await this . _validator . throwIfBranchNotConfigured ( this . currJob ) ;
167172 await this . constructPrefix ( ) ;
168173 if ( ! this . currJob . payload . aliased || ( this . currJob . payload . aliased && this . currJob . payload . primaryAlias ) ) {
169174 await this . constructManifestIndexPath ( ) ;
170175 }
171176 this . prepStageSpecificNextGenCommands ( ) ;
172177 this . constructEnvVars ( ) ;
173178 this . currJob . payload . isNextGen = true ;
179+ if ( this . _currJob . payload . jobType === 'productionDeploy' ) {
180+ this . _validator . throwIfItIsNotPublishable ( this . _currJob ) ;
181+ }
174182 } else {
175183 this . currJob . payload . isNextGen = false ;
176184 }
@@ -187,35 +195,35 @@ export abstract class JobHandler {
187195 if ( resp . status != 'success' ) {
188196 const error = new AutoBuilderError ( resp . error , "BuildError" )
189197 await this . logError ( error ) ;
190- throw error
198+ throw error
191199 }
192200 } else {
193201 const error = new AutoBuilderError ( "No commands to execute" , "BuildError" )
194202 await this . logError ( error ) ;
195- throw error
203+ throw error
196204 }
197205 return true ;
198206 }
199207
200208 private constructEnvVars ( ) : void {
201- let envVars = `GATSBY_PARSER_USER=${ this . _config . get < string > ( "GATSBY_PARSER_USER" ) } \nGATSBY_PARSER_BRANCH=${ this . currJob . payload . branchName } \n` ;
209+ let envVars = `GATSBY_PARSER_USER=${ this . _config . get < string > ( "GATSBY_PARSER_USER" ) } \nGATSBY_PARSER_BRANCH=${ this . currJob . payload . branchName } \n` ;
202210 const pathPrefix = this . currJob . payload . pathPrefix ;
203- if ( typeof pathPrefix !== 'undefined' && pathPrefix !== null ) {
204- envVars += `PATH_PREFIX=${ pathPrefix } \n`
211+ if ( typeof pathPrefix !== 'undefined' && pathPrefix !== null ) {
212+ envVars += `PATH_PREFIX=${ pathPrefix } \n`
205213 }
206214 // const snootyFrontEndVars = {
207215 // 'GATSBY_FEATURE_FLAG_CONSISTENT_NAVIGATION': this._config.get<boolean>("gatsbyConsitentNavFlag"),
208216 // 'GATSBY_FEATURE_FLAG_SDK_VERSION_DROPDOWN': this._config.get<boolean>("gatsbySDKVersionDropdownFlag"),
209217
210218 // };
211-
219+
212220 // for (const[envName, envValue] of Object.entries(snootyFrontEndVars)) {
213221 // if (envValue) envVars += `${envName}=TRUE\n`;
214222 // }
215- this . _fileSystemServices . writeToFile ( `repos/${ this . currJob . payload . repoName } /.env.production` , envVars , { encoding : 'utf8' , flag : 'w' } ) ;
223+ this . _fileSystemServices . writeToFile ( `repos/${ this . currJob . payload . repoName } /.env.production` , envVars , { encoding : 'utf8' , flag : 'w' } ) ;
216224 }
217225
218- protected getPathPrefix ( ) : Promise < string | undefined > {
226+ protected getPathPrefix ( ) : Promise < string | undefined > {
219227 return Promise . resolve ( undefined ) ;
220228 }
221229
@@ -230,45 +238,46 @@ export abstract class JobHandler {
230238 protected prepBuildCommands ( ) : void {
231239 this . currJob . buildCommands = [
232240 `. /venv/bin/activate` ,
233- `cd repos/${ this . currJob . payload . repoName } ` ,
241+ `cd repos/${ this . currJob . payload . repoName } ` ,
234242 `rm -f makefile` ,
235243 `make html`
236244 ] ;
237245 }
238246
239247 @throwIfJobInterupted ( )
240248 protected async build ( ) : Promise < boolean > {
241- this . cleanup ( ) ;
242- await this . cloneRepo ( this . _config . get < string > ( "repo_dir" ) ) ;
243- this . _logger . info ( this . _currJob . _id , "Cloned Repo" ) ;
244- await this . commitCheck ( ) ;
245- this . _logger . info ( this . _currJob . _id , "Checked Commit" ) ;
246- await this . pullRepo ( ) ;
247- this . _logger . info ( this . _currJob . _id , "Pulled Repo" ) ;
248- await this . _repoConnector . applyPatch ( this . currJob ) ;
249- this . _logger . info ( this . _currJob . _id , "Patch Applied" ) ;
250- await this . downloadMakeFile ( ) ;
251- this . _logger . info ( this . _currJob . _id , "Downloaded Makefile" ) ;
252- this . prepBuildCommands ( ) ;
253- this . _logger . info ( this . _currJob . _id , "Prepared Build commands" ) ;
254- await this . prepNextGenBuild ( ) ;
255- this . _logger . info ( this . _currJob . _id , "Prepared Next Gen build" ) ;
256- return await this . executeBuild ( ) ;
249+ this . cleanup ( ) ;
250+ await this . cloneRepo ( this . _config . get < string > ( "repo_dir" ) ) ;
251+ this . _logger . info ( this . _currJob . _id , "Cloned Repo" ) ;
252+ await this . commitCheck ( ) ;
253+ this . _logger . info ( this . _currJob . _id , "Checked Commit" ) ;
254+ await this . pullRepo ( ) ;
255+ this . prepBuildCommands ( ) ;
256+ this . _logger . info ( this . _currJob . _id , "Prepared Build commands" ) ;
257+ await this . prepNextGenBuild ( ) ;
258+ this . _logger . info ( this . _currJob . _id , "Pulled Repo" ) ;
259+ await this . _repoConnector . applyPatch ( this . currJob ) ;
260+ this . _logger . info ( this . _currJob . _id , "Patch Applied" ) ;
261+ await this . downloadMakeFile ( ) ;
262+ this . _logger . info ( this . _currJob . _id , "Downloaded Makefile" ) ;
263+
264+ this . _logger . info ( this . _currJob . _id , "Prepared Next Gen build" ) ;
265+ return await this . executeBuild ( ) ;
257266 }
258267
259268 @throwIfJobInterupted ( )
260269 protected async deployGeneric ( ) : Promise < CommandExecutorResponse > {
261270 this . prepDeployCommands ( ) ;
262271 await this . _logger . save ( this . currJob . _id , `${ this . _config . get < string > ( 'stage' ) . padEnd ( 15 ) } Pushing to ${ this . name } ` ) ;
263-
264- if ( this . currJob . deployCommands && this . currJob . deployCommands . length > 0 ) {
272+
273+ if ( this . currJob . deployCommands && this . currJob . deployCommands . length > 0 ) {
265274 const resp = await this . _commandExecutor . execute ( this . currJob . deployCommands )
266- if ( resp && resp . error && typeof ( resp . error ) === 'string' && resp . error . indexOf ( 'ERROR' ) !== - 1 ) {
275+ if ( resp && resp . error && typeof ( resp . error ) === 'string' && resp . error . indexOf ( 'ERROR' ) !== - 1 ) {
267276 await this . _logger . save ( this . currJob . _id , `${ this . _config . get < string > ( 'stage' ) . padEnd ( 15 ) } Failed to push to ${ this . name } ` ) ;
268277 throw new PublishError ( `Failed pushing to ${ this . name } : ${ resp . error } ` )
269- }
270- await this . _logger . save ( this . currJob . _id , `${ this . _config . get < string > ( 'stage' ) . padEnd ( 15 ) } Finished pushing to ${ this . name } ` ) ;
271- await this . _logger . save ( this . currJob . _id , `${ this . _config . get < string > ( 'stage' ) . padEnd ( 15 ) } Staging push details:\n\n${ resp . output } ` ) ;
278+ }
279+ await this . _logger . save ( this . currJob . _id , `${ this . _config . get < string > ( 'stage' ) . padEnd ( 15 ) } Finished pushing to ${ this . name } ` ) ;
280+ await this . _logger . save ( this . currJob . _id , `${ this . _config . get < string > ( 'stage' ) . padEnd ( 15 ) } Staging push details:\n\n${ resp . output } ` ) ;
272281 return resp ;
273282
274283 } else {
0 commit comments