From 0f103fabfe5b21540a7dcac7ca84bdba0718ed11 Mon Sep 17 00:00:00 2001 From: Benjamin Bouvier Date: Fri, 24 Jun 2016 01:28:27 +0200 Subject: [PATCH] Fixes #4: Add branch name in push message, tidy up push hook; --- index.js | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/index.js b/index.js index ae28379..ffd8105 100644 --- a/index.js +++ b/index.js @@ -28,15 +28,28 @@ var handlers = { push: function(body) { var user = body.user_name; var projectName = body.project.name; + var commits = body.commits; + var numCommits = body.total_commits_count; - var msg = ['push on ' + projectName + ': ' + user + ' pushed ' + commits.length + ' commits.']; + var branchName = body.ref.replace('refs/heads/', ''); - if (commits.length < 4) { - msg = msg.concat(commits.map(formatCommit)); + var msg = []; + if (!numCommits) { + // Special case: a branch was created or deleted. + var action = 'created'; + if (body.after === '0000000000000000000000000000000000000000') + action = 'deleted'; + msg.push(projectName + ': ' + user + ' ' + action + ' branch ' + branchName); } else { - msg.push(formatCommit(commits[0]) + ' ...'); - msg.push(formatCommit(commits[commits.length - 1])); + var maybeS = numCommits === 1 ? '' : 's'; + msg.push('push on ' + projectName + '@' + branchName + ': ' + user + ' pushed ' + commits.length + ' commit' + maybeS + '.'); + if (numCommits < 4) { + msg = msg.concat(commits.map(formatCommit)); + } else { + msg.push(formatCommit(commits[0]) + ' ...'); + msg.push(formatCommit(commits[commits.length - 1])); + } } return msg;