From 8091cad37c4cdce5ac491181785af93b144d7721 Mon Sep 17 00:00:00 2001 From: twidi Date: Wed, 13 Feb 2013 22:16:50 +0100 Subject: [PATCH] Add fetchContributors to Repository --- gh3.js | 50 +++++++++++++++++++++++++++++++++++++++++++ tests/spec/gh3Spec.js | 33 ++++++++++++++++++++++++++++ 2 files changed, 83 insertions(+) diff --git a/gh3.js b/gh3.js index 970aab3..14927ec 100644 --- a/gh3.js +++ b/gh3.js @@ -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); @@ -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(); + } } },{}); diff --git a/tests/spec/gh3Spec.js b/tests/spec/gh3Spec.js index 5c619d0..8f94e47 100644 --- a/tests/spec/gh3Spec.js +++ b/tests/spec/gh3Spec.js @@ -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