-
-
Save yeahmx91/4bd21cbc7f52895f67e86d31448427ec to your computer and use it in GitHub Desktop.
Git commit hook for trello.com
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env node | |
// Based on: https://gist.github.com/mashihua/2952814 | |
// get it here https://trello.com/app-key | |
var key = "your_key"; | |
// generate one here https://developers.trello.com/authorize | |
var token = "your_token"; | |
var repo_link = "http://gitlab/group/repo" | |
// get it here https://trello.com/b/xxxxxxx/board.json | |
var board_id = "your_id" | |
var Trello = require("node-trello"); | |
var t = new Trello(key, token); | |
var spawn = require('child_process').spawn; | |
var git = spawn('git', ['log' , '-1' , 'HEAD']); | |
var data = ""; | |
git.stdout.on('data',function(d){ | |
data += d; | |
var m = data.match(/#\s?[0-9]+/g); | |
if(!m){ | |
// JS Regex is stupid | |
m = data.match(/#[0-9]+/g); | |
} | |
var merge = data.match(/Merge/i); | |
if(merge){ | |
console.log('Ignoring merge commit'); | |
return; | |
} | |
if(m){ | |
var commit = data.match(/commit\s+(.+)/i)[1]; | |
var commit_link = repo_link + "/commit/" + commit; | |
var author = data.match(/author:\s+([^<]+)/i)[1]; | |
var date = data.match(/date:\s+(.+)/i)[1]; | |
var lines = data.split('\n'); | |
var msg = ""; | |
for(var i = 4;i < lines.length;i++){ | |
msg += lines[i].trim() + "\n"; | |
} | |
var cardNumber = m[0].replace(/#\s?/g, '').trim(); | |
console.log("CardNumber : " + cardNumber); | |
//get card | |
get_card(board_id, cardNumber,function(err, data){ | |
if(err) warn("[ERROR] Cannot find card matching ID " + cardNumber); | |
else{ | |
var today = new Date(); | |
var dd = today.getDate(); | |
var mm = today.getMonth()+1;//January is 0! | |
var yyyy = today.getFullYear(); | |
if(dd<10){dd='0'+dd} | |
if(mm<10){mm='0'+mm} | |
var text = 'Autor : '+ author + '\nFecha : ' + +mm+'/'+dd+'/'+yyyy + '\nCommit : ['+ commit + '](' + commit_link + ')\n Mensaje : ' + msg | |
add_comment(data['id'], text, function (err,data){ | |
if(err) throw err; | |
else info('Submit message for ' + cardNumber + " : \n" + data.data.text); | |
}); | |
} | |
}); | |
}else{ | |
console.log('No trello cards mentioned'); | |
} | |
}); | |
var partition = "***************************************************************************"; | |
function info(msg){ | |
console.log(partition); | |
console.log(msg); | |
console.log(partition); | |
} | |
function warn(msg){ | |
console.warn(partition); | |
console.warn(msg); | |
console.warn(partition); | |
} | |
function logfun(err, data){ | |
if(err) throw err; | |
console.log(data); | |
} | |
function get_card(board_id, card_id, fun){ | |
fun = fun || logfun; | |
t.get("/1/boards/"+ board_id +"/cards/"+card_id, fun); | |
} | |
function update_card(card_id, params, fun){ | |
fun = fun || logfun; | |
t.put("/1/cards/"+card_id, params, fun); | |
} | |
function add_comment(card_id, comment, fun ){ | |
fun = fun || logfun; | |
t.post("/1/card/"+card_id+ "/actions/comments", {text: comment}, fun); | |
} | |
function get_cards(list_id, fun){ | |
fun = fun || logfun; | |
t.get("/1/lists/"+list_id+"/cards", {fields: "idList,closed,name"}, fun) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env sh | |
export HOOK_NAME="post-to-trello" | |
export HOOK_ARGS=$@ | |
BASEDIR=$(dirname $0) | |
echo $BASEDIR | |
export PATH="/usr/local/bin:$PATH" | |
export NODE_PATH="/usr/local/lib/node_modules:$PATH" | |
echo hola! | |
"${BASEDIR}"/post-to-trello |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Deleted
git.on('exit', function(){...});
. The event wasn't triggered.You can find a tutorial here (thanks to Joel Herber).