You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by ti...@apache.org on 2015/06/01 22:39:42 UTC
cordova-coho git commit: Improvements to coho list-pulls.
Repository: cordova-coho
Updated Branches:
refs/heads/master 1413128c1 -> aa7595464
Improvements to coho list-pulls.
1. Supports an environment variable (CORDOVA_GIT_ACCOUNT) to specify <username>:<password> or <apitoken> to avoid hitting GitHub rate limits.
2. Uses API to get most recent comment info, if available.
Project: http://git-wip-us.apache.org/repos/asf/cordova-coho/repo
Commit: http://git-wip-us.apache.org/repos/asf/cordova-coho/commit/aa759546
Tree: http://git-wip-us.apache.org/repos/asf/cordova-coho/tree/aa759546
Diff: http://git-wip-us.apache.org/repos/asf/cordova-coho/diff/aa759546
Branch: refs/heads/master
Commit: aa75954648eedf4cbd4200e6d3c3e20bdf78b403
Parents: 1413128
Author: Tim Barham <ti...@microsoft.com>
Authored: Fri May 29 10:12:25 2015 -0700
Committer: Tim Barham <ti...@microsoft.com>
Committed: Fri May 29 10:33:44 2015 -0700
----------------------------------------------------------------------
src/list-pulls.js | 83 ++++++++++++++++++++++++++++++++++++++++++--------
1 file changed, 70 insertions(+), 13 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/cordova-coho/blob/aa759546/src/list-pulls.js
----------------------------------------------------------------------
diff --git a/src/list-pulls.js b/src/list-pulls.js
index b0255f7..8aff9c4 100644
--- a/src/list-pulls.js
+++ b/src/list-pulls.js
@@ -23,7 +23,9 @@ var apputil = require('./apputil');
var flagutil = require('./flagutil');
var repoutil = require('./apputil');
-var GITHUB_API_URL = "https://api.github.com/";
+// Set env variable CORDOVA_GIT_ACCOUNT to <username>:<password> or <api-token> to avoid hitting GitHub rate limits.
+var GITHUB_ACCOUNT = process.env.CORDOVA_GIT_ACCOUNT ? process.env.CORDOVA_GIT_ACCOUNT + "@" : "";
+var GITHUB_API_URL = "https://" + GITHUB_ACCOUNT + "api.github.com/";
var GITHUB_ORGANIZATION = "apache";
var commentFailed = false;
@@ -33,21 +35,76 @@ function addLastCommentInfo(repo, pullRequests, callback) {
callback();
}
pullRequests.forEach(function(pullRequest) {
- // review_comments_url is always empty, so resort to scraping.
- request.get({ url: 'https://github.com/apache/' + repo + '/pull/' + pullRequest.number, headers: { 'User-Agent': 'Cordova Coho' }}, function(err, res, payload) {
- if (err) {
- if (!commentFailed) {
- commentFailed = true;
- console.warn('Pull request scrape request failed: ' + err);
+ var commentsUrl = pullRequest._links.comments && pullRequest._links.comments.href;
+ var reviewCommentsUrl = pullRequest._links.review_comments && pullRequest._links.review_comments.href;
+
+ if (commentsUrl && reviewCommentsUrl) {
+ // If comments and review_comments URLs are present, use them (more accurate than scraping)
+ getPullRequestComments(commentsUrl, function (comments) {
+ getPullRequestComments(reviewCommentsUrl, comments, function (comments) {
+ // If we have any comments, grab the user name from the most recent one. If not, we'll display the
+ // owner of the PR (the initial PR comment is not included in the list of comments we get).
+ comments = comments.sort(function (a, b) {
+ // For simplicity, we want to end up with the newest comment first, so reverse sort on create date.
+ return new Date(b.created_at) - new Date(a.created_at);
+ });
+ pullRequest.lastUpdatedBy = comments[0] ? comments[0].user.login : pullRequest.user.login;
+ if (--remaining === 0) {
+ callback();
+ }
+ });
+ });
+ } else {
+ // Otherwise, resort to scraping.
+ request.get({ url: 'https://github.com/apache/' + repo + '/pull/' + pullRequest.number, headers: { 'User-Agent': 'Cordova Coho' }}, function(err, res, payload) {
+ if (err) {
+ if (!commentFailed) {
+ commentFailed = true;
+ console.warn('Pull request scrape request failed: ' + err);
+ }
+ } else {
+ var m = /[\s\S]*timeline-comment-header[\s\S]*?"author".*?>(.*?)</.exec(payload);
+ pullRequest.lastUpdatedBy = m && m[1] || '';
}
- } else {
- var m = /[\s\S]*timeline-comment-header[\s\S]*?"author".*?>(.*?)</.exec(payload);
- pullRequest.lastUpdatedBy = m && m[1] || '';
+ if (--remaining === 0) {
+ callback();
+ }
+ });
+ }
+ });
+}
+
+function getPullRequestComments(url, existingComments, callback) {
+ if (GITHUB_ACCOUNT) {
+ url = url.replace('https://', 'https://' + GITHUB_ACCOUNT);
+ }
+
+ if (typeof existingComments === 'function') {
+ callback = existingComments;
+ existingComments = [];
+ }
+
+ request.get({
+ url: url,
+ headers: {'User-Agent': 'Cordova Coho'}
+ }, function (err, res, payload) {
+ if (err) {
+ if (!commentFailed) {
+ commentFailed = true;
+ console.warn('Getting pull request comments failed: ' + err);
}
- if (--remaining === 0) {
- callback();
+ callback(existingComments);
+ }
+ var comments = JSON.parse(payload);
+ if (!comments.forEach) {
+ // We don't have an array, so something failed
+ if (!commentFailed) {
+ commentFailed = true;
+ console.warn('Getting pull request comments failed: did not return an array');
}
- });
+ callback(existingComments);
+ }
+ callback(existingComments.concat(comments));
});
}
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@cordova.apache.org
For additional commands, e-mail: commits-help@cordova.apache.org