@ -11,10 +11,30 @@ const mailSender = require("./mail-sender");
const settings = require ( "../settings" ) ;
const settings = require ( "../settings" ) ;
const codePostfix = "" ;
const codePostfix = "" ;
const mailLazinessLevel = 1000 ;
const maxDescriptionLength = 140 ;
const maxTmpcodepathLength = 15 ;
const twoDigits = 100 ;
const createFinalState = ( isSuccess ) => {
if ( isSuccess ) {
return "success" ;
}
return "error" ;
} ;
const createBuildDoneMessage = ( isSuccess , name ) => {
if ( isSuccess ) {
return ` Successfully built ${ name } ` ;
}
const notifyStatus = ( options , callback ) => {
return ` Build failed for ${ name } ` ;
} ;
const notifyStatus = ( options , notifyStatusCallback ) => {
const status = {
const status = {
"description" : String ( options . description || "" ) . substr ( 0 , 140 ) ,
"description" : String ( options . description || "" ) . substr ( 0 , maxDescriptionLength ) ,
"owner" : options . owner ,
"owner" : options . owner ,
"repo" : options . reponame ,
"repo" : options . reponame ,
"sha" : options . hash ,
"sha" : options . hash ,
@ -22,19 +42,27 @@ const notifyStatus = (options, callback) => {
"target_url" : ` ${ settings . siteRoot } status/ ${ options . owner } / ${ options . reponame } / ${ options . hash } `
"target_url" : ` ${ settings . siteRoot } status/ ${ options . owner } / ${ options . reponame } / ${ options . hash } `
} ;
} ;
settings . createGithub ( options . owner ) . repos . createStatus ( status , ( err ) => {
settings . createGithub ( options . owner ) . repos . createStatus ( status , ( cr eateStatusE rr) => {
if ( err ) {
if ( cr eateStatusE rr) {
console . log ( ` Error while creating status: ${ err } ` ) ;
console . log ( ` Error while creating status: ${ cr eateStatusE rr} ` ) ;
console . log ( status ) ;
console . log ( status ) ;
return c allback( err ) ;
return notifyStatusC allback( cr eateStatusE rr) ;
}
}
return c allback( ) ;
return notifyStatusC allback( ) ;
} ) ;
} ) ;
} ;
} ;
const build = ( options , callback ) => {
const wrapGitLoader = ( skipGitLoader ) => {
if ( ! skipGitLoader ) {
return gitLoader ;
}
return ( gitLoaderOptions , gitLoaderCallback ) => process . nextTick ( gitLoaderCallback ) ;
} ;
const build = ( options , buildCallback ) => {
const url = options . url ;
const url = options . url ;
const owner = options . owner ;
const owner = options . owner ;
const reponame = options . reponame ;
const reponame = options . reponame ;
@ -42,28 +70,26 @@ const build = (options, callback) => {
const branch = options . branch ;
const branch = options . branch ;
const skipGitLoader = options . skipGitLoader ;
const skipGitLoader = options . skipGitLoader ;
const local = path . join ( options . app . get ( "gitpath" ) , "r" ) ;
const local = path . join ( options . app . get ( "gitpath" ) , "r" ) ;
const tmp = path . join ( options . app . get ( "tmpcodepath" ) , rev . substr ( 0 , 15 ) ) ;
const tmp = path . join ( options . app . get ( "tmpcodepath" ) , rev . substr ( 0 , maxTmpcodepathLength ) ) ;
const exported = tmp + codePostfix ;
const exported = tmp + codePostfix ;
const release = path . join ( options . app . get ( "releasepath" ) , owner , reponame , branch , rev ) ;
const release = path . join ( options . app . get ( "releasepath" ) , owner , reponame , branch , rev ) ;
const statusQueue = async . queue ( ( task , callback ) => task ( callback ) , 1 ) ;
const statusQueue = async . queue ( ( task , queueCallback ) => task ( queueCallback ) , 1 ) ;
const actualGitLoader = skipGitLoader
const actualGitLoader = wrapGitLoader ( skipGitLoader ) ;
? ( options , callback ) => process . nextTick ( callback )
: gitLoader ;
const date = new Date ( ) ;
const date = new Date ( ) ;
const versionMajor = date . getFullYear ( ) ;
const versionMajor = date . getFullYear ( ) ;
const versionMinor = date . getMonth ( ) + 1 ;
const versionMinor = date . getMonth ( ) + 1 ;
const versionBuild = date . getDate ( ) ;
const versionBuild = date . getDate ( ) ;
const versionRev = ( date . getHours ( ) * 100 ) + date . getMinutes ( ) ;
const versionRev = ( date . getHours ( ) * twoDigits ) + date . getMinutes ( ) ;
const version = ` ${ versionMajor } . ${ versionMinor } . ${ versionBuild } . ${ versionRev } ` ;
const version = ` ${ versionMajor } . ${ versionMinor } . ${ versionBuild } . ${ versionRev } ` ;
const versionInfo = ` ${ version } ; built from ${ rev } ; repository: ${ owner } / ${ reponame } ; branch: ${ branch } ` ;
const versionInfo = ` ${ version } ; built from ${ rev } ; repository: ${ owner } / ${ reponame } ; branch: ${ branch } ` ;
statusQueue . push ( ( c allback) => notifyStatus ( {
statusQueue . push ( ( queueC allback) => notifyStatus ( {
"description" : "Preparing to build..." ,
"description" : "Preparing to build..." ,
"hash" : rev ,
"hash" : rev ,
owner ,
owner ,
reponame ,
reponame ,
"state" : "pending"
"state" : "pending"
} , c allback) ) ;
} , queueC allback) ) ;
fse . mkdirsSync ( release ) ;
fse . mkdirsSync ( release ) ;
@ -71,57 +97,60 @@ const build = (options, callback) => {
fse . mkdirsSync ( path . join ( options . app . get ( "releasepath" ) , owner , reponame , "$revs" ) ) ;
fse . mkdirsSync ( path . join ( options . app . get ( "releasepath" ) , owner , reponame , "$revs" ) ) ;
fs . writeFileSync ( path . join ( options . app . get ( "releasepath" ) , owner , reponame , "$revs" , ` ${ rev } .branch ` ) , branch ) ;
fs . writeFileSync ( path . join ( options . app . get ( "releasepath" ) , owner , reponame , "$revs" , ` ${ rev } .branch ` ) , branch ) ;
const done = ( err , result ) => {
const createErrorMessageForMail = ( doneErr ) => {
const errorMessage = result && result . errors
if ( ! doneErr ) {
? ( ( result . errors . $allMessages || [ ] ) [ 0 ] || { } ) . message
return "" ;
: err ;
}
const warnMessage = result && result . warns
? ( ( result . warns . $allMessages || [ ] ) [ 0 ] || { } ) . message
return ` Error message: ${ doneErr } \r \n \r \n ` ;
: err ;
} ;
const infoMessage = result && result . infos
? ( ( result . infos . $allMessages || [ ] ) . slice ( - 1 ) [ 0 ] || { } ) . message
const createResultMessageForMail = ( result ) => {
: err ;
if ( ! result || ! result . messages || ! result . messages . $allMessages ) {
return JSON . stringify ( result , null , " " ) ;
reportProcessor . writeReport ( release , err , result , ( writeErr ) => {
}
statusQueue . push ( ( callback ) => async . parallel ( [
( callback ) => notifyStatus ( {
return result . messages . $allMessages . map ( ( msg ) => ` ${ msg . prefix } \t ${ msg . message } ` ) . join ( "\r\n" ) ;
} ;
const done = ( doneErr , result ) => {
const allErrors = ( ( result || { } ) . errors || { } ) . $allMessages || [ ] ;
const allWarns = ( ( result || { } ) . warns || { } ) . $allMessages || [ ] ;
const allInfos = ( ( result || { } ) . infos || { } ) . $allMessages || [ ] ;
const errorMessage = ( allErrors [ 0 ] || { } ) . message || doneErr ;
const warnMessage = ( allWarns [ 0 ] || { } ) . message ;
const infoMessage = ( allInfos [ allInfos . length - 1 ] || { } ) . message ;
reportProcessor . writeReport ( release , doneErr , result , ( writeErr ) => {
statusQueue . push ( ( queueCallback ) => async . parallel ( [
( parallelCallback ) => notifyStatus ( {
"description" : errorMessage || warnMessage || infoMessage || "Success" ,
"description" : errorMessage || warnMessage || infoMessage || "Success" ,
"hash" : rev ,
"hash" : rev ,
owner ,
owner ,
reponame ,
reponame ,
"state" : err
"state" : createFinalState ( ! doneErr )
? "error"
} , parallelCallback ) ,
: "success"
( parallelCallback ) => mailSender . send ( {
} , callback ) ,
( callback ) => mailSender . send ( {
"from" : settings . smtp . sender ,
"from" : settings . smtp . sender ,
"headers" : { "X-Laziness-level" : 1000 } ,
"headers" : { "X-Laziness-level" : mailLazinessLevel } ,
"subject" : ` ${ err ? "Build failed for" : "Successfully built" } ${ owner } / ${ reponame } / ${ branch } ` ,
"subject" : createBuildDoneMessage ( doneErr , ` ${ owner } / ${ reponame } / ${ branch } ` ) ,
"text" : ` Build status URL: ${ settings . siteRoot } status/ ${ owner } / ${ reponame } / ${ rev } \r \n \r \n `
"text" : ` Build status URL: ${ settings . siteRoot } status/ ${ owner } / ${ reponame } / ${ rev } \r \n \r \n ${ createErrorMessageForMail ( doneErr ) } ${ createResultMessageForMail ( result ) } ` ,
+ (
err
? ` Error message: ${ err } \r \n \r \n `
: "" )
+ (
( ! result || ! result . messages || ! result . messages . $allMessages )
? JSON . stringify ( result , null , 4 )
: result . messages . $allMessages . map ( ( msg ) => ` ${ msg . prefix } \t ${ msg . message } ` ) . join ( "\r\n" ) ) ,
"to" : settings . smtp . receiver
"to" : settings . smtp . receiver
} , c allback) ,
} , parallelCallback ) ,
( c allback) => {
( parallelCallback ) => {
if ( err ) {
if ( doneErr ) {
return process . nextTick ( c allback) ;
return process . nextTick ( parallelCallback ) ;
}
}
return fse . remove ( tmp , c allback) ;
return fse . remove ( tmp , parallelCallback ) ;
}
}
] , c allback) ) ;
] , queueC allback) ) ;
if ( writeErr ) {
if ( writeErr ) {
return c allback( writeErr ) ;
return buildC allback( writeErr ) ;
}
}
return c allback( err , result ) ;
return buildC allback( don eE rr, result ) ;
} ) ;
} ) ;
} ;
} ;
@ -131,11 +160,11 @@ const build = (options, callback) => {
"hash" : rev ,
"hash" : rev ,
local ,
local ,
"remote" : ` ${ url } .git `
"remote" : ` ${ url } .git `
} , ( err ) => {
} , ( gitLoad erE rr) => {
if ( err ) {
if ( gitLoad erE rr) {
console . log ( err ) ;
console . log ( gitLoad erE rr) ;
return done ( ` Git fetch error: ${ err } ` ) ;
return done ( ` Git fetch error: ${ gitLoad erE rr} ` ) ;
}
}
console . log ( "Done loading from git" ) ;
console . log ( "Done loading from git" ) ;
@ -145,9 +174,9 @@ const build = (options, callback) => {
return done ( null , "MBSNotFound" ) ;
return done ( null , "MBSNotFound" ) ;
}
}
return fs . readFile ( path . join ( exported , "mbs.json" ) , ( err , data ) => {
return fs . readFile ( path . join ( exported , "mbs.json" ) , ( r eadE rr, data ) => {
if ( err ) {
if ( r eadE rr) {
return done ( err , "MBSUnableToRead" ) ;
return done ( r eadE rr, "MBSUnableToRead" ) ;
}
}
let task = null ;
let task = null ;
@ -169,12 +198,12 @@ const build = (options, callback) => {
rev ,
rev ,
tmp ,
tmp ,
versionInfo
versionInfo
} , ( err , result ) => {
} , ( proc essE rr, result ) => {
if ( err ) {
if ( proc essE rr) {
return done ( err , result ) ;
return done ( proc essE rr, result ) ;
}
}
return done ( err , result ) ;
return done ( proc essE rr, result ) ;
} ) ;
} ) ;
} ) ;
} ) ;
} ) ;
} ) ;