Skip to content

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also .

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also .
  • 3 commits
  • 4 files changed
  • 0 commit comments
  • 2 contributors
Commits on Jul 27, 2012
@shama shama Add file.comment method 1f631dc
Commits on Jul 28, 2012
@shama shama Add files[*].comments and files[*].comments[*].reply() 65368e1
Commits on Jul 30, 2012
@fat Merge pull request #6 from shama/diff-comment
Add commentOnDiff method
f0c26b7
Showing with 119 additions and 3 deletions.
  1. +3 −0 README.md
  2. +43 −0 examples/comment-on-diff.js
  3. +25 −2 lib/pull-request.js
  4. +48 −1 lib/util/github.js
View
3 README.md
@@ -172,6 +172,7 @@ When testing pull-requests, your function will be passed an object with the foll
+ issue.files[*].status - the status of the file (modified, deleted, etc.)
+ issue.files[*].changes - the number of changes made in a file
+ issue.files[*].deletions - the number of deletions made in a file
++ issue.files[*].comments - an array of line comments on the file
+ issue.commits - an array of git commits
+ issue.commits[*].sha - the commit sha
+ issue.commits[*].commit - a commit object
@@ -216,4 +217,6 @@ The following convenience methods are made available on all haunt objects. You c
+ issue.close - closes an issue/pull-request
+ issue.assign - (accepts a username) assigns an issue/pull-request
+ issue.comment - (accepts a string) comments on an issue/pull-request
++ issue.files[*].comment - (accepts an comment and line number) comment on a given line number in a diff of a pull-request
++ issue.files[*].comments[*].reply - (accepts a string) reply to a comment in a diff on a pull request
+ issue.reportFailures - generic test failure message, which notifies a user what failed based on mocha reporter.
View
43 examples/comment-on-diff.js
@@ -0,0 +1,43 @@
+// COMMENT ON DIFF EXAMPLE
+// =======================
+
+var assert = require('assert');
+
+module.exports = {
+
+ 'pull-requests': {
+
+ 'should not use deprecated path.exists(Sync)': function (pull) {
+ pull.files.forEach(function (file) {
+ findOnLine(/path\.exists|path\.existsSync/, file.patch, function(err, lineNum) {
+
+ file.comment(
+ 'Yo dude path.exists(Sync) has been deprecated in nodejs v0.8, use fs.exists(Sync) instead.',
+ lineNum
+ )
+
+ })
+ })
+ }
+
+ }
+
+}
+
+// find stuff in a diff and return the line number
+function findOnLine(find, patch, cb) {
+ if (find.test(patch)) {
+ var lineNum = 0
+ patch.split('\n').forEach(function(line) {
+ var range = /\@\@ \-\d+,\d+ \+(\d+),\d+ \@\@/g.exec(line)
+ if (range) {
+ lineNum = Number(range[1]) - 1
+ } else if (line.substr(0, 1) !== '-') {
+ lineNum++
+ }
+ if (find.test(line)) {
+ return cb(null, lineNum)
+ }
+ })
+ }
+}
View
27 lib/pull-request.js
@@ -83,8 +83,13 @@ PullRequest.prototype.getPaths = function (callback) {
github.getFiles(this.repo.data.owner.login, this.repo.data.name, this.data.number, function (err, files) {
if (err) return callback(err);
- this.exports.files = files;
- callback(null, files);
+
+ github.getReviewComments(this.repo.data.owner.login, this.repo.data.name, this.data.number, function(err, comments) {
+ files = attachCommentsToFile.call(this, files, comments);
+ this.exports.files = files;
+ callback(null, files);
+ }.bind(this));
+
}.bind(this));
}
@@ -99,4 +104,22 @@ PullRequest.prototype.getCommits = function (callback) {
}
+// attach review comments to the related file
+function attachCommentsToFile(files, comments) {
+ return files.filter(function(file) {
+ return file.comments = comments.filter(function(comment) {
+ if (file.filename === comment.path) {
+ return comment.reply = attachCommentReply.call(this, comment);
+ }
+ }.bind(this));
+ }.bind(this));
+}
+
+// reply to a comment on a diff
+function attachCommentReply(comment) {
+ return function reply(body, callback) {
+ github.replyToReviewComment(this.repo.data.owner.login, this.repo.data.name, this.data.number, comment.id, body, callback);
+ }.bind(this);
+}
+
module.exports = PullRequest;
View
49 lib/util/github.js
@@ -146,6 +146,27 @@ module.exports.getComments = function (user, repo, number, page, comments, callb
});
};
+// fetches comments on all files in a pull request
+module.exports.getReviewComments = function (user, repo, number, callback) {
+ var page = 1;
+ var comments = [];
+ (function getReviewComments() {
+ var options = {
+ user: user,
+ repo: repo,
+ number: number,
+ page: page,
+ per_page: 50
+ };
+ github('/repos/:user/:repo/pulls/:number/comments', options, function (err, result) {
+ if (err) return callback(err);
+ comments = comments.concat(result);
+ page++;
+ if (result.length === options.per_page) getReviewComments();
+ else callback(err, comments);
+ });
+ }());
+};
// fetches as many files as possible
module.exports.getFiles = function (user, repo, number, page, files, callback) {
@@ -166,12 +187,12 @@ module.exports.getFiles = function (user, repo, number, page, files, callback) {
github('/repos/:user/:repo/pulls/:number/files', options, function (err, result) {
if (err) return callback(err);
files = files.concat(result);
+ files.map(function(file) { return file.comment = commentOnFile(user, repo, number); });
if (result.length === 50) module.exports.getFiles(user, repo, number, page + 1, files, callback);
else callback(err, files);
});
};
-
// fetches as many commits as possible
module.exports.getCommits = function (user, repo, number, page, commits, callback) {
if (typeof page == 'function') {
@@ -234,4 +255,30 @@ module.exports.commentOnIssue = function (user, repo, number, body, callback) {
number: number,
method: 'POST'
}, callback);
+}
+
+module.exports.replyToReviewComment = function(user, repo, number, comment_id, body, callback) {
+ github('/repos/:user/:repo/pulls/:number/comments', {
+ user: user,
+ repo: repo,
+ number: number,
+ body: body,
+ in_reply_to: comment_id,
+ method: 'POST'
+ }, callback);
+}
+
+function commentOnFile(user, repo, number) {
+ return function(body, position, callback) {
+ github('/repos/:user/:repo/pulls/:number/comments', {
+ user: user,
+ repo: repo,
+ number: number,
+ body: body,
+ position: position,
+ commit_id: this.sha,
+ path: this.filename,
+ method: 'POST'
+ }, callback);
+ }
}

No commit comments for this range

Something went wrong with that request. Please try again.