Skip to content

Commit 2c2acfb

Browse files
sairoutineÆndrew Rininsland
authored andcommitted
Add options for getting pulls
add listPulls test
1 parent c895f35 commit 2c2acfb

File tree

2 files changed

+55
-14
lines changed

2 files changed

+55
-14
lines changed

github.js

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -411,10 +411,46 @@
411411
// List all pull requests of a respository
412412
// -------
413413

414-
this.listPulls = function(state, cb) {
415-
_request('GET', repoPath + '/pulls' + (state ? '?state=' + state : ''), null, function(err, pulls, xhr) {
416-
if (err) return cb(err);
417-
cb(null, pulls, xhr);
414+
this.listPulls = function(options, cb) {
415+
options = options || {};
416+
var url = repoPath + "/pulls";
417+
var params = [];
418+
419+
if (typeof options === 'string') {
420+
// backward compatibility
421+
params.push('state=' + options);
422+
}
423+
else {
424+
if (options.state) {
425+
params.push("state=" + encodeURIComponent(options.state));
426+
}
427+
if (options.head) {
428+
params.push("head=" + encodeURIComponent(options.head));
429+
}
430+
if (options.base) {
431+
params.push("base=" + encodeURIComponent(options.base));
432+
}
433+
if (options.sort) {
434+
params.push("sort=" + encodeURIComponent(options.sort));
435+
}
436+
if (options.direction) {
437+
params.push("direction=" + encodeURIComponent(options.direction));
438+
}
439+
if (options.page) {
440+
params.push("page=" + options.page);
441+
}
442+
if (options.per_page) {
443+
params.push("per_page=" + options.per_page);
444+
}
445+
}
446+
447+
if (params.length > 0) {
448+
url += "?" + params.join("&");
449+
}
450+
451+
_request('GET', url, null, function(err, pulls, xhr) {
452+
if (err) return cb(err);
453+
cb(null, pulls, xhr);
418454
});
419455
};
420456

test/test.repo.js

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -210,10 +210,22 @@ describe('Creating new Github.Repository', function() {
210210
});
211211

212212
it('should list pulls on repo', function(done) {
213-
repo.listPulls('open', function(err) {
214-
should.not.exist(err);
213+
var repo = github.getRepo('michael', 'github');
214+
var options = {
215+
state: 'all',
216+
sort: 'updated',
217+
direction: 'desc',
218+
page: 1,
219+
per_page: 100
220+
};
215221

216-
// @TODO write better assertion
222+
repo.listPulls(options, function(err, pull_list) {
223+
should.not.exist(err);
224+
pull_list.should.be.instanceof(Array);
225+
pull_list.should.have.length(100);
226+
should.exist(pull_list[0].title);
227+
should.exist(pull_list[0].body);
228+
should.exist(pull_list[0].url);
217229
done();
218230
});
219231
});
@@ -251,13 +263,6 @@ describe('Creating new Github.Repository', function() {
251263
});
252264
});
253265

254-
it('should list pull requests on repo', function(done) {
255-
repo.listPulls('open', function(err) {
256-
should.not.exist(err);
257-
done();
258-
});
259-
});
260-
261266
it('should write author and committer to repo', function(done) {
262267
var options = {
263268
author: {

0 commit comments

Comments
 (0)