diff --git a/package.json b/package.json index ea80a2a..7b359e4 100644 --- a/package.json +++ b/package.json @@ -8,7 +8,8 @@ "dependencies": { "coffee-script": "~1.6.3", "async": "~0.2.9", - "proxyquire": "~0.4.1" + "proxyquire": "~0.4.1", + "lodash": "~1.3.1" }, "devDependencies": { "mocha": "~1.12.0" diff --git a/src/issues.coffee b/src/issues.coffee new file mode 100644 index 0000000..4db8016 --- /dev/null +++ b/src/issues.coffee @@ -0,0 +1,35 @@ +#!/usr/bin/env coffee +{ _ } = require 'lodash' +async = require 'async' +req = require './request' + +module.exports = + # Used on an initial fetch of issues for a repo. + 'get_all': (opts, cb) -> + done = no + + # For each status... + one_status = (status, cb) -> + # Concat them here. + results = [] + # One pageful fetch (next pages in series). + do fetch_page = (page = 1) -> + req.all_issues { status: status, page: page }, (err, data) -> + # Request errors. + return cb err if err + # GitHub errors. + return cb data.message if data.message + # Empty? + return cb null, results unless data.length + # Concat. + results = results.concat data + # < 100 results? + return cb null, results if data.length < 100 + # Fetch the next page then. + fetch_page page + 1 + + # For each `open` and `closed` issues in parallel. + async.parallel [ + _.partial one_status, 'open' + _.partial one_status, 'closed' + ], cb \ No newline at end of file diff --git a/src/milestones.coffee b/src/milestones.coffee index 6fd1b22..83b0f69 100644 --- a/src/milestones.coffee +++ b/src/milestones.coffee @@ -2,6 +2,7 @@ req = require './request' module.exports = + # Used at initialization stage. 'get_current': (opts, cb) -> req.all_milestones opts, (err, data) -> # Request errors. diff --git a/src/request.coffee b/src/request.coffee index 94aeeda..1b31244 100644 --- a/src/request.coffee +++ b/src/request.coffee @@ -3,4 +3,5 @@ module.exports = 'all_milestones': (opts, cb) -> cb 'Not implemented' 'all_issues': (opts, cb) -> + # { direction: 'asc', per_page: 100 } cb 'Not implemented' \ No newline at end of file diff --git a/test/issues.coffee b/test/issues.coffee new file mode 100644 index 0000000..bf33449 --- /dev/null +++ b/test/issues.coffee @@ -0,0 +1,24 @@ +#!/usr/bin/env coffee +assert = require 'assert' +async = require 'async' +path = require 'path' +proxy = require 'proxyquire' + +req = {} + +issues = proxy path.resolve(__dirname, '../src/issues.coffee'), + './request': req + +module.exports = + 'all empty': (done) -> + called = 0 + req.all_issues = (opts, cb) -> + called += 1 + cb null, [] + + issues.get_all {}, (err, [ open, closed ]) -> + assert.ifError err + assert.equal called, 2 + assert.equal open.length, 0 + assert.equal closed.length, 0 + done.call null \ No newline at end of file