@ -1,24 +1,21 @@
"use strict" ;
"use strict" ;
const fs = require ( 'fs' ) ;
const fs = require ( "fs" ) ;
const _ = require ( 'underscore' ) ;
const _ = require ( "underscore" ) ;
const settings = require ( '../settings' ) ;
const settings = require ( "../settings" ) ;
const featureNamePattern = /^feature-(\d+)(?:-[a-zA-Z0-9]+)+$/ ;
const featureNamePattern = /^feature-(\d+)(?:-[a-zA-Z0-9]+)+$/ ;
const versionNamePattern = /^v\d+(\.\d+)*$/ ;
const versionNamePattern = /^v\d+(\.\d+)*$/ ;
const masterNamePattern = /^master$/ ;
const masterNamePattern = /^master$/ ;
const writeComment = function ( options , message , callback ) {
const writeComment = ( options , message , callback ) => options . github . issues . createComment ( {
return options . github . issues . createComment ( {
user : options . baseRepoOptions . owner ,
user : options . baseRepoOptions . owner ,
repo : options . baseRepoOptions . reponame ,
repo : options . baseRepoOptions . reponame ,
number : options . number ,
number : options . number ,
body : message
body : message
} , callback ) ;
} , callback ) ;
} ;
const closePullRequest = function ( options , message , callback ) {
const closePullRequest = ( options , message , callback ) => writeComment ( options , message , ( err ) => {
return writeComment ( options , message , function ( err ) {
if ( err ) {
if ( err ) {
return callback ( err ) ;
return callback ( err ) ;
}
}
@ -30,14 +27,12 @@ const closePullRequest = function (options, message, callback) {
state : "closed"
state : "closed"
} , callback ) ;
} , callback ) ;
} ) ;
} ) ;
} ;
const checkHasIssue = function ( options , issueNumber , callback ) {
const checkHasIssue = ( options , issueNumber , callback ) => options . github . issues . getRepoIssue ( {
return options . github . issues . getRepoIssue ( {
user : options . baseRepoOptions . owner ,
user : options . baseRepoOptions . owner ,
repo : options . baseRepoOptions . reponame ,
repo : options . baseRepoOptions . reponame ,
number : issueNumber
number : issueNumber
} , function ( err , result ) {
} , ( err , result ) => {
if ( err && err . code !== 404 ) {
if ( err && err . code !== 404 ) {
return callback ( err ) ;
return callback ( err ) ;
}
}
@ -52,23 +47,20 @@ const checkHasIssue = function (options, issueNumber, callback) {
return callback ( undefined , true , result . title ) ;
return callback ( undefined , true , result . title ) ;
} ) ;
} ) ;
} ;
const checkHasReleases = function ( options , callback ) {
const checkHasReleases = ( options , callback ) => options . github . releases . listReleases ( {
return options . github . releases . listReleases ( {
owner : options . baseRepoOptions . owner ,
owner : options . baseRepoOptions . owner ,
repo : options . baseRepoOptions . reponame ,
repo : options . baseRepoOptions . reponame ,
per _page : 1
per _page : 1
} , function ( err , result ) {
} , ( err , result ) => {
if ( err ) {
if ( err ) {
return callback ( err ) ;
return callback ( err ) ;
}
}
return callback ( undefined , result && result . length ) ;
return callback ( undefined , result && result . length ) ;
} ) ;
} ) ;
} ;
const checkPullRequest = function ( options , callback ) {
const checkPullRequest = ( options , callback ) => {
const head = options . headRepoOptions ;
const head = options . headRepoOptions ;
const base = options . baseRepoOptions ;
const base = options . baseRepoOptions ;
@ -81,7 +73,7 @@ const checkPullRequest = function (options, callback) {
return closePullRequest ( options , "Only merging from version to master is allowed" , callback ) ;
return closePullRequest ( options , "Only merging from version to master is allowed" , callback ) ;
}
}
return checkHasReleases ( options , function ( err , hasReleases ) {
return checkHasReleases ( options , ( err , hasReleases ) => {
if ( err ) {
if ( err ) {
return writeComment ( options , "Unable to check for releases" , callback ) ;
return writeComment ( options , "Unable to check for releases" , callback ) ;
}
}
@ -107,7 +99,7 @@ const checkPullRequest = function (options, callback) {
}
}
const issueNumber = featureNamePattern . exec ( head . branchname ) [ 1 ] ;
const issueNumber = featureNamePattern . exec ( head . branchname ) [ 1 ] ;
return checkHasIssue ( options , issueNumber , function ( err , hasIssue , issueTitle ) {
return checkHasIssue ( options , issueNumber , ( err , hasIssue , issueTitle ) => {
if ( err ) {
if ( err ) {
return writeComment ( options , "Unable to check for issue:\r\n\r\n" + err . message , callback ) ;
return writeComment ( options , "Unable to check for issue:\r\n\r\n" + err . message , callback ) ;
}
}
@ -117,7 +109,7 @@ const checkPullRequest = function (options, callback) {
}
}
const shouldHaveReleases = versionNamePattern . test ( base . branchname ) ;
const shouldHaveReleases = versionNamePattern . test ( base . branchname ) ;
return checkHasReleases ( options , function ( err , hasReleases ) {
return checkHasReleases ( options , ( err , hasReleases ) => {
if ( err ) {
if ( err ) {
return writeComment ( options , "Unable to check for releases" , callback ) ;
return writeComment ( options , "Unable to check for releases" , callback ) ;
}
}
@ -139,15 +131,15 @@ const checkPullRequest = function (options, callback) {
} ) ;
} ) ;
} ;
} ;
const getStatusMessageFromRelease = function ( app , options , callback ) {
const getStatusMessageFromRelease = ( app , options , callback ) => {
const releaseDir = app . get ( 'releasepath' ) + "/" + options . owner + "/" + options . reponame + "/" + options . branch + "/" + options . rev ;
const releaseDir = app . get ( "releasepath" ) + "/" + options . owner + "/" + options . reponame + "/" + options . branch + "/" + options . rev ;
const reportFile = releaseDir + "/report.json" ;
const reportFile = releaseDir + "/report.json" ;
options . attemptsGetReport = ( options . attemptsGetReport || 0 ) + 1 ;
options . attemptsGetReport = ( options . attemptsGetReport || 0 ) + 1 ;
fs . exists ( reportFile , function ( exists ) {
fs . exists ( reportFile , ( exists ) => {
if ( ! exists ) {
if ( ! exists ) {
return fs . exists ( releaseDir , function ( dirExists ) {
return fs . exists ( releaseDir , ( dirExists ) => {
if ( ! dirExists ) {
if ( ! dirExists ) {
return callback ( "Release directory not found. Probably repository hooks are not configured" ) ;
return callback ( "Release directory not found. Probably repository hooks are not configured" ) ;
}
}
@ -156,14 +148,11 @@ const getStatusMessageFromRelease = function (app, options, callback) {
}
}
//maybe it is building right now
//maybe it is building right now
return setTimeout ( function ( ) {
return setTimeout ( ( ) => getStatusMessageFromRelease ( app , options , callback ) , 10000 ) ;
getStatusMessageFromRelease ( app , options , callback ) ;
} , 10000 ) ;
} ) ;
} ) ;
}
}
return setTimeout ( function ( ) {
return setTimeout ( ( ) => fs . readFile ( reportFile , ( err , dataBuffer ) => {
return fs . readFile ( reportFile , function ( err , dataBuffer ) {
if ( err ) {
if ( err ) {
return callback ( err ) ;
return callback ( err ) ;
}
}
@ -178,9 +167,9 @@ const getStatusMessageFromRelease = function (app, options, callback) {
}
}
if ( report . result && ( ( report . result . errors || { } ) . $allMessages || [ ] ) . length + ( ( report . result . warns || { } ) . $allMessages || [ ] ) . length > 0 ) {
if ( report . result && ( ( report . result . errors || { } ) . $allMessages || [ ] ) . length + ( ( report . result . warns || { } ) . $allMessages || [ ] ) . length > 0 ) {
return callback ( _ . map (
return callback ( _ . map (
( report . result . errors || { } ) . $allMessages || [ ] , function ( message ) { return "ERR: " + message . message ; }
( report . result . errors || { } ) . $allMessages || [ ] , ( message ) => "ERR: " + message . message
) . concat ( _ . map (
) . concat ( _ . map (
( report . result . warns || { } ) . $allMessages || [ ] , function ( message ) { return "WARN: " + message . message ; }
( report . result . warns || { } ) . $allMessages || [ ] , ( message ) => "WARN: " + message . message
) ) . join ( "\r\n" ) ) ;
) ) . join ( "\r\n" ) ) ;
}
}
if ( ! report . result || report . err ) {
if ( ! report . result || report . err ) {
@ -190,18 +179,15 @@ const getStatusMessageFromRelease = function (app, options, callback) {
return callback ( undefined , report . result . infos . $allMessages [ report . result . infos . $allMessages . length - 1 ] . message ) ;
return callback ( undefined , report . result . infos . $allMessages [ report . result . infos . $allMessages . length - 1 ] . message ) ;
}
}
return callback ( undefined , "OK" ) ;
return callback ( undefined , "OK" ) ;
} ) ;
} ) , 1000 ) ;
} , 1000 ) ;
} ) ;
} ) ;
} ;
} ;
exports . commentOnPullRequest = function ( options , callback ) {
exports . commentOnPullRequest = ( options , callback ) => {
options . github = settings . createGithub ( options . baseRepoOptions . owner ) ;
options . github = settings . createGithub ( options . baseRepoOptions . owner ) ;
return checkPullRequest ( options , function ( err , successMessage ) {
return checkPullRequest ( options , ( err , successMessage ) => getStatusMessageFromRelease ( options . app , options . headRepoOptions , ( err , successMessage ) => {
getStatusMessageFromRelease ( options . app , options . headRepoOptions , function ( err , successMessage ) {
const message = err ? ( "Was not built:\r\n\r\n```\r\n" + err . replace ( /```/g , "` ` `" ) + "\r\n```\r\n\r\nDO NOT MERGE!" ) : ( "Build OK\r\n\r\n" + successMessage ) ;
const message = err ? ( "Was not built:\r\n\r\n```\r\n" + err . replace ( /```/g , '` ` `' ) + "\r\n```\r\n\r\nDO NOT MERGE!" ) : ( "Build OK\r\n\r\n" + successMessage ) ;
const statusUrlMessage = "Build status URL: " + settings . siteRoot + "status/" + options . headRepoOptions . owner + "/" + options . headRepoOptions . reponame + "/" + options . headRepoOptions . rev + "\r\n\r\n" ;
const statusUrlMessage = "Build status URL: " + settings . siteRoot + "status/" + options . headRepoOptions . owner + "/" + options . headRepoOptions . reponame + "/" + options . headRepoOptions . rev + "\r\n\r\n" ;
return writeComment ( options , message + "\r\n\r\n" + statusUrlMessage , callback ) ;
return writeComment ( options , message + "\r\n\r\n" + statusUrlMessage , callback ) ;
} ) ;
} ) ) ;
} ) ;
} ;
} ;