From 2b60a9d7a60f7d5c416cb621f34e0ba03ef188eb Mon Sep 17 00:00:00 2001 From: Mikhail Gozhev Date: Tue, 7 Aug 2018 13:31:35 +0300 Subject: [PATCH] fix issue w/ saving only 30 comments --- src/index.js | 63 +++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 48 insertions(+), 15 deletions(-) diff --git a/src/index.js b/src/index.js index 88d11fb..9d7c020 100644 --- a/src/index.js +++ b/src/index.js @@ -108,22 +108,55 @@ module.exports = function (token, options, cb) { } function getComments (issue, repo, cb) { - var url = '' - if (repo.issue === 'all') { - url = issue.comments_url - } else { - url = base + '/repos/' + repo.user + '/' + repo.name + '/issues/' + repo.issue + '/comments' + issue.comments = [] + + var get_issues_since = function(since) + { + var url = '' + var query = since === '' ? '' : '?since=' + since + + if (repo.issue === 'all') { + url = issue.comments_url + query + } else { + url = base + '/repos/' + repo.user + '/' + repo.name + '/issues/' + repo.issue + '/comments' + query + } + + //console.log('request ' + query) + + var on_response = function (err, resp, body) { + if (err) return cb(err, 'Error in request for comments.') + var comments = body + + /* remove duplicates */ + var dups = 0; + if (issue.comments.length > 0 && comments.length > 0) { + var last_id = issue.comments[issue.comments.length - 1].id + while (comments.length > 0 && comments[0].id <= last_id) { + comments.shift() + ++dups + } + } + //console.log('got ' + comments.length + ' new comments' + // + (dups ? ' (' + dups + ' duplicates was dropped)' : '')); + + issue.comments = issue.comments.concat(comments) + if (comments.length <= 1) { + //console.log('total got ' + issue.comments.length + ' comments') + issue.comments.forEach(function (comment) { + comment.created_at = new Date(comment.created_at).toLocaleDateString() + }) + issueData.push(issue) + cb() + } else { + var last = issue.comments[issue.comments.length - 1] + get_issues_since(last.created_at) + } + } + + request(url, { json: true, headers: headers }, on_response) } - request(url, { json: true, headers: headers }, function (err, resp, body) { - if (err) return cb(err, 'Error in request for comments.') - - issue.comments = body - issue.comments.forEach(function (comment) { - comment.created_at = new Date(comment.created_at).toLocaleDateString() - }) - issueData.push(issue) - cb() - }) + + get_issues_since('') } function writeData (options, cb) {