Skip to content

Commit

Permalink
Add fetchContributors to Repository
Browse files Browse the repository at this point in the history
  • Loading branch information
twidi committed Feb 13, 2013
1 parent d6b5e7a commit 8091cad
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 0 deletions.
50 changes: 50 additions & 0 deletions gh3.js
Expand Up @@ -307,6 +307,11 @@
return item.name == name;
});
},
getByLogin : function (login) {
return _.find(Gh3.Users.users, function (item) {
return item.login == login;
});
},
each : function (callback) {
_.each(Gh3.Users.users, function (user) {
callback(user);
Expand Down Expand Up @@ -825,6 +830,51 @@
if (callback) { callback(new Error(res)); }
}
});
},
fetchContributors : function (callback) {
var that = this;
that.contributors = [];

Gh3.Helper.callHttpApi({
service : "repos/"+that.user.login+"/"+that.name+"/contributors",
success : function(res) {
_.each(res.data, function (contributor) {
that.contributors.push(new Gh3.User(contributor.login, contributor));
});

if (callback) { callback(null, that); }
},
error : function (res) {
if (callback) { callback(new Error(res)); }
}
});

},
getContributors : function () { return this.contributors; },
getContributorByName : function (name) {
return _.find(this.contributors, function (contributor) {
return contributor.name == name;
});
},
getContributorByLogin : function (login) {
return _.find(this.contributors, function (contributor) {
return contributor.login == login;
});
},
eachContributor : function (callback) {
_.each(this.contributors, function (contributor) {
callback(contributor);
});
},
reverseContributors : function () {
this.contributors.reverse();
},
sortContributors : function (comparison_func) {
if (comparison_func) {
this.contributors.sort(comparison_func);
} else {
this.contributors.sort();
}
}

},{});
Expand Down
33 changes: 33 additions & 0 deletions tests/spec/gh3Spec.js
Expand Up @@ -295,6 +295,39 @@ describe("get gh3 readme", function() {

});

describe("get gh3 contributors", function() {

var k33g = new Gh3.User("k33g")
, gh3Repo = new Gh3.Repository("gh3", k33g)
, contributors = [];

it("should have a 3 contributors, including k33g, with number of contributions", function () {

runs(function () {

gh3Repo.fetchContributors(function (err, res) {
if(err) { throw "outch ..."; }
contributors = res.getContributors();
console.log(gh3Repo);
});

}, "asynchronous method : fetchContributors()");

waitsFor(function () {
return contributors.length > 0;
}, "...", 1000);

runs(function () {
var k33g_ = gh3Repo.getContributorByLogin('k33g');
expect(contributors.length).toBeGreaterThan(1);
expect(k33g_.login).toEqual('k33g');
expect(k33g_.contributions).toBeGreaterThan(20);
});

});

});


/*-----------------------------
Branch, File & Directory
Expand Down

0 comments on commit 8091cad

Please sign in to comment.