-
-
Save danggrianto/84e9887f1df1a4e7a44aded27966e138 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 | |
//please requist your app token from | |
//https://trello.com/1/connect?key=yourkey&name=git-hook&expiration=never&response_type=token&scope=read,write | |
var key = "your key"; | |
var token = "your token"; | |
//https://trello.com/board/-/4e9003324a517dad44465056 | |
var board_id = "4e9003324a517dad44465056"; | |
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; | |
}); | |
git.on('exit', function(){ | |
var m = data.match(/(card|close|archive|fix)e?s? \D?([0-9]+)\s+:?\s+(.+)/i); | |
if(m){ | |
var commit = data.match(/commit\s+(.+)/i)[1]; | |
var author = data.match(/author:\s+([^<]+)/i)[1]; | |
var date = data.match(/date:\s+(.+)/i)[1]; | |
var msg = m[3]; | |
//get card | |
get_card(board_id, m[2],function(err, data){ | |
if(err) wran("[ERROR] Cannot find card matching ID " + m[2]); | |
else{ | |
if(m[1].toLowerCase() === 'close'){ | |
update_card(data['id'], {closed:true}, function(err, data){ | |
if(err) throw err; | |
if(data.closed){ | |
info('Congratulation! Close card:' + m[2] + "\n" + data.name); | |
}else{ | |
warn('Close does not worked correct.'); | |
} | |
}); | |
} | |
if(/^fix/i.exec(m[1])){ | |
var text = 'Commit by : '+ author + '\nDate : ' + date + '\nCommit : '+ commit + '\nMessage : ' + msg | |
add_comment(data['id'], text, function (err,data){ | |
if(err) throw err; | |
else info('Submit message for ' + m[2] + " : \n" + data.data.text); | |
}); | |
} | |
} | |
}); | |
}else{ | |
warn('Do you work for production?'); | |
} | |
}); | |
var partition = "***************************************************************************"; | |
function info(msg){ | |
console.log(partition); | |
console.log(meg); | |
console.log(partition); | |
} | |
function warn(msg){ | |
console.warn(partition); | |
console.warn(meg); | |
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) | |
} |
You're on the right track, when you have your short URL that looks like
https://trello.com/b/PxEQPoMz/reports
Take that URL and add .json to the end as follows:
https://trello.com/b/PxEQPoMz/reports.json
Within the raw JSON dump you get when pulling up this new URL, you will see a field called idList. This is the list ID.
The Trello API getting started guide mentions this on the first page, may be worth a read
https://developers.trello.com/get-started/start-building
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Steps: