Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Git commit hook for trello.com
#!/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)
}
@alexjamesbrown

This comment has been minimized.

Copy link

alexjamesbrown commented Dec 25, 2013

@mashihua are there any setup instructions for this?

@dineshsaxena

This comment has been minimized.

Copy link

dineshsaxena commented Mar 3, 2014

How can i close my issue from trello api to github?

@geraldvillorente

This comment has been minimized.

Copy link

geraldvillorente commented Aug 21, 2014

Would be good if there is setup instructions for this.

@geraldvillorente

This comment has been minimized.

Copy link

geraldvillorente commented Aug 21, 2014

Alright I think I figure it out.

Steps:

  1. Install node-trello - sudo npm install -g node-trello
  2. Get your key here - https://trello.com/1/appKey/generate
  3. Allow Git-hook to use your account - https://trello.com/1/connect?key=KEY_YOU_OBTAINED_FROM_STEP_TWO&name=git-hook&expiration=never&response_type=token&scope=read,write

And click the Allow button which will generate a token. Use this token in your post-commit hook.
4. Copy the post-commit script in your .git/hooks directory and make sure it is executable (e.g chmod +x /path/to/repo/.git/hooks/post-commit)
5. Put your key and token in the post-commit hook.
6. Get your board id - https://trello.com/docs/gettingstarted/index.html#getting-a-token-from-a-user

  • Request a token granting read-only access forever - https://trello.com/1/authorize?key=KEY_YOU_OBTAINED_FROM_STEP_TWO&name=BOARD_NAME&expiration=never&response_type=token
  • Get the board id by getting membership record - https://trello.com/1/members/my/boards?key=KEY_YOU_OBTAINED_FROM_STEP_TWO&token=TOKEN_YOU_OBTAINED_FROM_STEP_SIX_A

Get the id that accompanied by the name you specified in step 6.a.

See this doc - https://trello.com/docs/gettingstarted/index.html#getting-an-application-key

That's all.

@geraldvillorente

This comment has been minimized.

Copy link

geraldvillorente commented Aug 21, 2014

Fixes typo

#!/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) warn("[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(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)
}
@goldan

This comment has been minimized.

Copy link

goldan commented Oct 14, 2015

@mashihua, thank you for the script and @geraldvillorente for installation instructions!
Could you please give instruction how to use it?
The regexp seems to expect the card ID as an integer number. But currently trello cards have alphanumeric IDs (e.g. https://trello.com/c/rYgiTbGG/).

@goldan

This comment has been minimized.

Copy link

goldan commented Oct 28, 2015

The script works great, thank you!
But quite often the data is empty string (though the commit message is not empty). If I try to commit & undo several times, finally it works. Has this happened to anybody?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.