PageRenderTime 60ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/githugs.js

https://github.com/jakemcgraw/githugs-public
JavaScript | 121 lines | 95 code | 13 blank | 13 comment | 8 complexity | 746380d23f012e444dac8859bef939d0 MD5 | raw file
  1. var GitHubApi = require("github").GitHubApi,
  2. jsdom = require('jsdom'),
  3. https = require('https'),
  4. config = require(__dirname + '/../config');
  5. var JQUERY = 'http://code.jquery.com/jquery-1.6.1.min.js';
  6. var GitHugs = exports.GitHugs = function() {
  7. // Keep yo hands out.
  8. this.jar = [];
  9. this.api = new GitHubApi(true);
  10. };
  11. GitHugs.prototype = {
  12. // Login to github.
  13. login: function(login, password, callback) {
  14. var self = this;
  15. self.get('/login', {}, function(err, data) {
  16. // Grab the login form and extract the authenticity token.
  17. jsdom.env(data, [JQUERY], function(errors, window) {
  18. var post_data = {
  19. authenticity_token: window.$('input[name="authenticity_token"]').val(),
  20. login: login,
  21. password: password
  22. };
  23. self.post('/session', post_data, function(err, data) {
  24. callback(err, data);
  25. });
  26. });
  27. });
  28. },
  29. // Comment on the latest commit for a branch.
  30. comment: function(user, repo, branch, comment, callback) {
  31. var self = this;
  32. // Login to github.
  33. self.login(config.github.login, config.github.password, function(err, data) {
  34. // Find the most recent commit by this user.
  35. self.api.getCommitApi().getBranchCommits(user, repo, branch, function(err, commits) {
  36. if (!err && commits.length) {
  37. // Comment on the commit with a hapy message.
  38. self.get(commits[0].url, {}, function(err, data) {
  39. jsdom.env(data, [JQUERY], function(errors, window) {
  40. var form = window.$('#comments').next('form');
  41. form.find('textarea').html(comment);
  42. self.post(form.attr('action'), form.serialize(), function(err, data) {
  43. callback(err, commits[0]);
  44. });
  45. });
  46. })
  47. } else {
  48. callback(error, null);
  49. }
  50. });
  51. });
  52. },
  53. // Make a POST request to github.
  54. post: function(url, data, callback) {
  55. if (typeof data === 'object') {
  56. var body = [];
  57. for (var key in data) {
  58. body.push(key + '=' + encodeURIComponent(data[key]));
  59. }
  60. data = body.join('&');
  61. }
  62. this.request('POST', url, data, callback);
  63. },
  64. // Make a GET request to github.
  65. get: function(url, data, callback) {
  66. var params = [];
  67. for (var key in data) {
  68. params.push(key + '=' + encodeURIComponent(data[key]));
  69. }
  70. this.request('GET', url + '?' + params.join('&'), false, callback);
  71. },
  72. // Make an HTTP request to github.
  73. request: function(type, url, data, callback) {
  74. var self = this,
  75. rxdata = '',
  76. options = {
  77. host: 'github.com',
  78. port: 443,
  79. path: url,
  80. method: type.toUpperCase(),
  81. headers: { 'Cookie': this.jar.join('; ') }
  82. };
  83. // Make an HTTPS request.
  84. var request = https.request(options, function(res) {
  85. if ('set-cookie' in res.headers) {
  86. self.jar = [];
  87. res.headers['set-cookie'].forEach(function(cookie) {
  88. var parts = cookie.split(/[;,] */);
  89. self.jar.push(parts[0].trim());
  90. });
  91. }
  92. // Accumulate response data.
  93. res.on('data', function(chunk) {
  94. rxdata += chunk;
  95. }).on('end', function() {
  96. callback(null, rxdata);
  97. });
  98. });
  99. request.on('error', function() {
  100. callback(true, null);
  101. });
  102. // Write POST body if necessary.
  103. if (data) {
  104. request.write(data);
  105. }
  106. request.end();
  107. }
  108. };