diff --git a/app-backend/src/routes/WebhooksRouter.js b/app-backend/src/routes/WebhooksRouter.js index 54b5302..e80056e 100644 --- a/app-backend/src/routes/WebhooksRouter.js +++ b/app-backend/src/routes/WebhooksRouter.js @@ -7,6 +7,31 @@ const ApiStatusCodes = require('../api/ApiStatusCodes'); const Logger = require('../utils/Logger'); const CaptainConstants = require('../utils/CaptainConstants'); + +router.post('/triggerbuild', function (req, res, next) { + + // find which branch is pushed + // inject it in locals.pushedBranches + + let isGithub = req.header('X-GitHub-Event') === 'push'; + let isBitbucket = (req.header('X-Event-Key') === 'repo:push') && req.header('X-Request-UUID') && req.header('X-Hook-UUID'); + + res.locals.pushedBranches = []; + + if (isGithub) { + let ref = req.body.ref; // "ref/heads/somebranch" + res.locals.pushedBranches.push(ref.substring(11, ref.length)); + } + else if (isBitbucket) { + for (let i = 0; i < req.body.push.changes.length; i++) { + res.locals.pushedBranches.push(req.body.push.changes[i].new.name); + } + } + + next(); + +}); + router.post('/triggerbuild', function (req, res, next) { res.sendStatus(200); @@ -15,7 +40,7 @@ router.post('/triggerbuild', function (req, res, next) { let app = res.locals.app; let namespace = res.locals.user.namespace; - if (!app || !serviceManager || !namespace|| !appName) { + if (!app || !serviceManager || !namespace || !appName) { Logger.e('Something went wrong during trigger build. Cannot extract app information from the payload.'); return; } @@ -28,14 +53,34 @@ router.post('/triggerbuild', function (req, res, next) { }) .then(function (repoInfo) { - return serviceManager.createImage(appName, { - repoInfo: repoInfo - }); + // if we didn't detect any branches, the POST might have come from another source that we don't + // explicitly support. Therefore, we just let it go through and triggers a build anyways + if (res.locals.pushedBranches.length > 0) { + let branchIsTracked = false; - }) - .then(function (version) { + for (let i = 0; i < res.locals.pushedBranches.length; i++) { + if (res.locals.pushedBranches[i] === repoInfo.branch) { + branchIsTracked = true; + break; + } + } - return serviceManager.ensureServiceInitedAndUpdated(appName, version); + // POST call was triggered due to another branch being pushed. We don't need to trigger the build. + if (!branchIsTracked) { + return; + } + + } + + return serviceManager + .createImage(appName, { + repoInfo: repoInfo + }) + .then(function (version) { + + return serviceManager.ensureServiceInitedAndUpdated(appName, version); + + }); }) .catch(function (error) { diff --git a/app-backend/src/user/TemplateHelper.js b/app-backend/src/user/TemplateHelper.js index 3178214..dbce883 100644 --- a/app-backend/src/user/TemplateHelper.js +++ b/app-backend/src/user/TemplateHelper.js @@ -16,6 +16,7 @@ function getTagsForImage(imageBaseName, url, allTags) { function (error, response, body) { if (error || !body) { + Logger.e(error); reject(error); return; } @@ -29,6 +30,7 @@ function getTagsForImage(imageBaseName, url, allTags) { } if (!results) { + Logger.e('NO RESULT'); reject(new Error('NO RESULT')); return; }