Hemanth.HM

A Computer Polyglot, CLI + WEB ♥'r.

Grunt Tweet Git Commit

| Comments

This is one of those fun time CLI hacks, it helps you to automatically tweet about every git commits that happen in the current repo.

I shall not say much about grunt.js or git hooks in the post, but rather will let the code do the talking.

Prereq :

1
2
npm install ntwitter
npm install -g grunt-cli

Keys listed below can be obtained from dev.twitter.com after setting up a new App

Custom grunt tweet task :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
grunt.registerTask('tweet', 'Tweet the commit info', function () {
     var twitter = require('twitter');
     var shell = require('shelljs');
     var commit_comment = shell.exec('git log -1 --pretty=%B').output.replace(/(\r\n|\n|\r)/gm,'');
     var repo = shell.exec(' basename $(git remote show -n origin | grep Fetch | cut -d: -f2-)').output.replace(/(\r\n|\n|\r)/gm,'');
     var author = shell.exec("git --no-pager show -s --format='%an'").output.replace(/(\r\n|\n|\r)/gm,'');
     var tweet = util.format('%s on %s by %s ', commit_comment, repo, author);

    var twit = new twitter({
      consumer_key: 'Twitter',
      consumer_secret: 'API',
      access_token_key: 'keys',
      access_token_secret: 'go here'
    });

    twit
      .verifyCredentials(function (err, data) {
          console.log(data);
      })
      .updateStatus(tweet,
           function (err, data) {
                  console.log(data);
       });

  });

Have a pre-commit hooks at .git/hooks/pre-commit as grunt tweet would tweet on every commit!

The tweet would be something like : 'jshint suggestions from @passy on yo.git by Hemanth.HM'

Comments