diff --git a/configs/.env b/configs/.env index 20dc6cc03..e7730a7e5 100644 --- a/configs/.env +++ b/configs/.env @@ -39,6 +39,7 @@ SLACK_BOT_USERNAME="runnabot" HIPCHAT_BOT_USERNAME="runnabot" ENABLE_BUILDS_ON_GIT_PUSH=false ENABLE_NOTIFICATIONS_ON_GIT_PUSH=false +ENABLE_NEW_BRANCH_BUILDS_ON_GIT_PUSH=false POLL_MONGO_TIMEOUT="30 minutes" GITHUB_SCOPE="user:email,repo,repo_deployment,read:repo_hook" GITHUB_HOOK_SECRET="3V3RYTHINGisAW3S0ME!" diff --git a/configs/.env.production b/configs/.env.production index 74e188c6a..1f4992b0c 100644 --- a/configs/.env.production +++ b/configs/.env.production @@ -30,4 +30,5 @@ CAYLEY="http://cayley.runnable.io" DOCKER_IMAGE_BUILDER_CACHE="/git-cache" ENABLE_BUILDS_ON_GIT_PUSH=true ENABLE_NOTIFICATIONS_ON_GIT_PUSH=true +ENABLE_NEW_BRANCH_BUILDS_ON_GIT_PUSH=false GITHUB_DEPLOY_KEYS_POOL_SIZE=100 \ No newline at end of file diff --git a/lib/models/notifications/notifier.js b/lib/models/notifications/notifier.js index 7f54d378a..ac7e46759 100644 --- a/lib/models/notifications/notifier.js +++ b/lib/models/notifications/notifier.js @@ -24,9 +24,12 @@ Handlebars.registerHelper('moreChangesSlack', function(repo, commitLog) { Handlebars.registerHelper('encode', function (str) { - return encodeURIComponent(str); + // we do double encoding here for angular because + // browser would automatically replace `%2F` to `/` and angular router will fail + return encodeURIComponent(encodeURIComponent(str)); }); + /** * Slack requires light escaping with just 3 rules: * & replaced with & diff --git a/lib/routes/actions/github.js b/lib/routes/actions/github.js index 144cba7d9..1d2f25a1c 100644 --- a/lib/routes/actions/github.js +++ b/lib/routes/actions/github.js @@ -60,17 +60,22 @@ app.post('/actions/github/', mw.res.status(202), mw.res.send('Deleted the branch; no work to be done.')), parseGitHubPushData, - instances.findInstancesLinkedToBranch('githubPushInfo.repo', 'githubPushInfo.branch'), - // check if there are instances that follow specific branch - mw.req('instances.length').validate(validations.equals(0)) + mw.req('githubPushInfo.commitLog.length').validate(validations.equals(0)) .then( - // no instances found. This can be push to the new branch - newBranch() - ) + mw.res.status(202), + mw.res.send('No commits pushed; no work to be done.')) .else( - // instances following particular branch were found. Redeploy them with the new code - followBranch('instances') - ) + instances.findInstancesLinkedToBranch('githubPushInfo.repo', 'githubPushInfo.branch'), + // check if there are instances that follow specific branch + mw.req('instances.length').validate(validations.equals(0)) + .then( + // no instances found. This can be push to the new branch + newBranch() + ) + .else( + // instances following particular branch were found. Redeploy them with the new code + followBranch('instances') + )) ), mw.res.status(501), mw.res.send('No action set up for that payload.')); @@ -88,7 +93,7 @@ function parseGitHubPushData (req, res, next) { branch : req.body.ref.replace('refs/heads/', ''), commit : req.body.head_commit.id, headCommit: req.body.head_commit, - commitLog : req.body.commits, + commitLog : req.body.commits || [], user: req.body.sender }; next(); @@ -96,6 +101,16 @@ function parseGitHubPushData (req, res, next) { function newBranch () { return flow.series( + function (req, res, next) { + // our env parsing cannot parse boolean correctly atm + if (process.env.ENABLE_NEW_BRANCH_BUILDS_ON_GIT_PUSH !== 'true') { + res.status(202); + res.send('New branch builds are disabled for now'); + } else { + next(); + } + }, + // TODO: ask praful again about creating builds for all branches instances.findContextVersionsForRepo('githubPushInfo.repo'), mw.req().set('contextVersionIds', 'instances'), diff --git a/test/actions-github.js b/test/actions-github.js index ab3f3f770..5e51c3c69 100644 --- a/test/actions-github.js +++ b/test/actions-github.js @@ -1,3 +1,4 @@ +'use strict'; var Lab = require('lab'); var describe = Lab.experiment; var it = Lab.test; @@ -76,10 +77,10 @@ describe('Github - /actions/github', function () { process.env.ENABLE_BUILDS_ON_GIT_PUSH = ctx.originalBuildsOnPushSetting; done(); }); - it('should just say hi if hooks are disabled', function (done) { + it('should send response immediately if hooks are disabled', function (done) { var options = hooks(ctx.contextVersion.json()).push; options.json.ref = 'refs/heads/someotherbranch'; - // require('./fixtures/mocks/github/users-username')(101, 'bkendall'); + require('./fixtures/mocks/github/users-username')(101, 'bkendall'); request.post(options, function (err, res) { if (err) { done(err); @@ -93,6 +94,55 @@ describe('Github - /actions/github', function () { }); }); + describe('ignore hooks without commits data', function () { + var ctx = {}; + beforeEach(function (done) { + process.env.ENABLE_BUILDS_ON_GIT_PUSH = 'true'; + multi.createInstance(function (err, instance, build, user, modelsArr) { + ctx.contextVersion = modelsArr[0]; + ctx.context = modelsArr[1]; + ctx.build = build; + ctx.user = user; + done(err); + }); + }); + + it('should send response immediately there are no commits data ([]) in the payload ', function (done) { + var options = hooks(ctx.contextVersion.json()).push; + options.json.ref = 'refs/heads/someotherbranch'; + options.json.commits = []; + require('./fixtures/mocks/github/users-username')(101, 'bkendall'); + request.post(options, function (err, res) { + if (err) { + done(err); + } + else { + expect(res.statusCode).to.equal(202); + expect(res.body).to.equal('No commits pushed; no work to be done.'); + done(); + } + }); + }); + + it('should send response immediately there are no commits data (null) in the payload ', function (done) { + var options = hooks(ctx.contextVersion.json()).push; + options.json.ref = 'refs/heads/someotherbranch'; + options.json.commits = null; + require('./fixtures/mocks/github/users-username')(101, 'bkendall'); + request.post(options, function (err, res) { + if (err) { + done(err); + } + else { + expect(res.statusCode).to.equal(202); + expect(res.body).to.equal('No commits pushed; no work to be done.'); + done(); + } + }); + }); + }); + + // describe('push', function () { // var ctx = {}; // beforeEach(function (done) { diff --git a/unit/notifier.js b/unit/notifier.js index aa24b5425..c80fc99a7 100644 --- a/unit/notifier.js +++ b/unit/notifier.js @@ -47,7 +47,7 @@ describe('Notifier', function () { message += '<' + headCommit.url + '|changes>'; message += ' (init & commit & push) to CodeNow/api (develop) are ready.\n'; message += ''; + message += ' (init & commit & push) to CodeNow/api (feature-1/fix) are ready.\n'; + message += ''; text += ' (init & commit <p>Hello</p> and '; text += ')'; - text += ' to CodeNow/api (develop) are deployed on servers:'; + text += ' to CodeNow/api (feature-1/fix) are deployed on servers:'; expect(text).to.equal(message.text); expect(message.attachments.length).to.equal(1); var attachment = message.attachments[0]; @@ -116,7 +153,7 @@ describe('Notifier', function () { }], repo: 'CodeNow/api', repoName: 'api', - branch: 'develop', + branch: 'feature-1/fix', commit: 'a240edf982d46720,1845b3bf10ccbe16f6049ea9', headCommit: headCommit, user: { @@ -131,9 +168,9 @@ describe('Notifier', function () { hipchat.send = function (text, cb) { var message = 'podviaznikov\'s '; message += 'changes'; - message += ' (hey there) to Runnable/api (develop) are ready.\n'; - message += 'Choose a server to run develop.'; + message += ' (hey there) to Runnable/api (feature-1/fix) are ready.\n'; + message += 'Choose a server to run feature-1/fix.'; expect(text).to.equal(message); cb(); }; @@ -146,7 +183,7 @@ describe('Notifier', function () { commitLog: [headCommit], repo: 'Runnable/api', repoName: 'api', - branch: 'develop', + branch: 'feature-1/fix', commit: 'a240edf982d467201845b3bf10ccbe16f6049ea9', headCommit: headCommit, user: { @@ -164,7 +201,7 @@ describe('Notifier', function () { hipchat.send = function (text, cb) { var message = 'podviaznikov\'s '; message += 'changes'; - message += ' (init) to CodeNow/api (develop) are deployed on servers:
\n '; + message += ' (init) to CodeNow/api (feature-1/fix) are deployed on servers:
\n '; message += 'instance1
\n '; message += 'instance2
\n'; @@ -180,7 +217,7 @@ describe('Notifier', function () { commitLog: [headCommit], repo: 'CodeNow/api', repoName: 'api', - branch: 'develop', + branch: 'feature-1/fix', commit: 'a240edf982d467201845b3bf10ccbe16f6049ea9', headCommit: headCommit, user: { @@ -252,7 +289,7 @@ describe('Notifier', function () { }); done(); }); - }, 2200); + }, 2500); }); }); });