Skip to content
This repository has been archived by the owner on Jan 24, 2023. It is now read-only.

Commit

Permalink
Get user's repos and allow user to load more (#490)
Browse files Browse the repository at this point in the history
  • Loading branch information
ongk authored and irfanhabib committed Jul 28, 2016
1 parent 41c5f9b commit e36a870
Show file tree
Hide file tree
Showing 9 changed files with 268 additions and 114 deletions.
8 changes: 6 additions & 2 deletions src/index.module.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
'ui.bootstrap',
'ui.router',
'smart-table',
'http-etag'
'http-etag',
'ig.linkHeaderParser'
];

var pluginModules = _.chain(env.plugins).map('moduleName').value();
Expand All @@ -28,10 +29,11 @@

config.$inject = [
'$compileProvider',
'$logProvider',
'httpEtagProvider'
];

function config($compileProvider, httpEtagProvider) {
function config($compileProvider, $logProvider, httpEtagProvider) {

/**
* Disabling Debug Data
Expand All @@ -49,6 +51,8 @@

// Use Etags to resolve caching issues on application upgrade
httpEtagProvider.cache('default');

$logProvider.debugEnabled(false);
}

})();
48 changes: 24 additions & 24 deletions src/plugins/cloud-foundry/api/github/github.api.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,13 @@
}

angular.extend(GithubApi.prototype, {
/**
* @function repos
* @memberof cloud-foundry.api.github.GithubApi
* @description Get repos user owns or has admin rights to
* @param {object} params - additional params to send
* @returns {promise}
* @public
/**
* @function repos
* @memberof cloud-foundry.api.github.GithubApi
* @description Get repos user owns or has admin rights to
* @param {object} params - additional params to send
* @returns {promise} A promise object
* @public
*/
repos: function (params) {
var url = this.githubApiUrl + 'user/repos';
Expand All @@ -67,14 +67,14 @@
},

/**
* @function branches
* @memberof cloud-foundry.api.github.GithubApi
* @description Get branches a repo
* @param {string} repo - the repo full name
* @param {object} params - additional params to send
* @returns {promise}
* @public
*/
* @function branches
* @memberof cloud-foundry.api.github.GithubApi
* @description Get branches a repo
* @param {string} repo - the repo full name
* @param {object} params - additional params to send
* @returns {promise} A promise object
* @public
*/
branches: function (repo, params) {
var url = this.githubApiUrl + 'repos/' + repo + '/branches';
var config = {
Expand All @@ -94,7 +94,7 @@
* @param {string} repo - the repo full name
* @param {string} branch - the branch name
* @param {object} params - additional params to send
* @returns {promise}
* @returns {promise} A promise object
* @public
*/
getBranch: function (repo, branch, params) {
Expand All @@ -110,14 +110,14 @@
},

/**
* @function commits
* @memberof cloud-foundry.api.github.GithubApi
* @description Get commits for a repo
* @param {string} repo - the repo full name
* @param {object} params - additional params to send
* @returns {promise}
* @public
*/
* @function commits
* @memberof cloud-foundry.api.github.GithubApi
* @description Get commits for a repo
* @param {string} repo - the repo full name
* @param {object} params - additional params to send
* @returns {promise} A promise object
* @public
*/
commits: function (repo, params) {
var url = this.githubApiUrl + 'repos/' + repo + '/commits';
var config = {
Expand Down
149 changes: 124 additions & 25 deletions src/plugins/cloud-foundry/model/github/github.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,45 @@

registerGithubModel.$inject = [
'app.model.modelManager',
'app.api.apiManager'
'app.api.apiManager',
'$q',
'$filter',
'linkHeaderParser'
];

function registerGithubModel(modelManager, apiManager) {
modelManager.register('cloud-foundry.model.github', new GithubModel(apiManager));
function registerGithubModel(modelManager, apiManager, $q, $filter, linkHeaderParser) {
modelManager.register('cloud-foundry.model.github', new GithubModel(apiManager, $q, $filter, linkHeaderParser));
}

/**
* @memberof cloud-foundry.model.github
* @name GithubModel
* @param {app.api.apiManager} apiManager - the application API manager
* @param {object} $q - the Angular $q service
* @param {object} $filter - the Angular $filter service
* @param {linkHeaderParser} linkHeaderParser - the linkHeaderParser service
* @property {app.api.apiManager} apiManager - the application API manager
* @property {object} $q - the Angular $q service
* @property {object} $filter - the Angular $filter service
* @property {linkHeaderParser} linkHeaderParser - the linkHeaderParser service
* @property {object} data - the Github data
* @class
*/
function GithubModel(apiManager) {
function GithubModel(apiManager, $q, $filter, linkHeaderParser) {
this.apiManager = apiManager;
this.$q = $q;
this.$filter = $filter;
this.linkHeaderParser = linkHeaderParser;
this.repo = {
filterTerm: null,
page: 0,
pageSize: 50,
links: {}
};
this.data = {
repoLinks: {},
repos: [],
filteredRepos: [],
branches: [],
commits: []
};
Expand All @@ -42,7 +62,7 @@
* @function isAuthenticated
* @memberof cloud-foundry.model.github.GithubModel
* @description Whether the user has authenticated against Github
* @returns {boolean} True if user has authenticated against Github *
* @returns {boolean} True if user has authenticated against Github
*/
isAuthenticated: function () {
return _.get(this.apiManager.retrieve('cloud-foundry.api.github'), 'authenticated');
Expand All @@ -52,22 +72,113 @@
* @function repos
* @memberof cloud-foundry.model.github.GithubModel
* @description Get repos user owns or has admin rights to
* @param {boolean} ignoreReset - flag to ignore reset of repos array
* @returns {promise} A promise object
* @public
*/
repos: function () {
repos: function (ignoreReset) {
var that = this;
var githubApi = this.apiManager.retrieve('cloud-foundry.api.github');
return githubApi.repos({per_page: 100})
if (!ignoreReset) {
this._resetRepos();
}

return this.nextRepos()
.then(function (response) {
that.onRepos(response);
})
.catch(function (err) {
return response;
}, function (err) {
that.onReposError();
throw err;
return err;
});
},

filterRepos: function (filterTerm) {
this.repo.filterTerm = filterTerm || null;
if (this.repo.filterTerm !== null) {
this.data.filteredRepos = this.$filter('filter')(this.data.repos, this.repo.filterTerm);
var filteredCnt = this.data.filteredRepos.length;
var maxCnt = this.repo.page * this.repo.pageSize;
if (filteredCnt < maxCnt) {
return this.repos(true);
}
} else {
this.data.filteredRepos.length = 0;
}
},

/**
* @function nextRepos
* @memberof cloud-foundry.model.github.GithubModel
* @description Get next set of repos
* @returns {promise} A promise object
* @public
*/
nextRepos: function () {
var that = this;
var deferred = this.$q.defer();
var config = {per_page: 50};
var numFetched = 0;
var fetchedRepos = [];

function next() {
if (that.repo.links.next) {
config.page = _.toInteger(that.repo.links.next.page);
}

that.apiManager.retrieve('cloud-foundry.api.github')
.repos(config)
.then(function (response) {
// Parse out the Link header
var linkHeaderText = response.headers('Link');
var links = linkHeaderText ? that.linkHeaderParser.parse(linkHeaderText) : {};
that.repo.links = links;

var repos = response.data || [];
var adminRepos = _.filter(repos, function (o) { return o.permissions.admin; });

if (that.repo.filterTerm) {
var filteredAdminRepos = that.$filter('filter')(adminRepos, that.repo.filterTerm);
numFetched += filteredAdminRepos.length;
[].push.apply(that.data.filteredRepos, filteredAdminRepos);
} else {
numFetched += adminRepos.length;
}

[].push.apply(fetchedRepos, adminRepos);
[].push.apply(that.data.repos, adminRepos);

if (numFetched < that.repo.pageSize && links.next) {
next(); // eslint-disable-line callback-return
} else {
that.repo.page++;
deferred.resolve({newRepos: fetchedRepos, repos: that.data.repos, links: that.repo.links, page: that.repo.page});
}
}, function (err) {
deferred.reject(err);
});
}

if (this.repo.page === 0 || this.repo.links.next) {
next(); // eslint-disable-line callback-return
} else {
deferred.resolve({newRepos: fetchedRepos, repos: this.data.repos, links: this.repo.links, page: this.repo.page});
}

return deferred.promise;
},

/**
* @function _resetRepos
* @memberof cloud-foundry.model.github.GithubModel
* @description Reset repos
* @returns {void}
* @private
*/
_resetRepos: function () {
this.data.repos.length = 0;
this.repo.page = 0;
this.repo.links = {};
},

/**
* @function branches
* @memberof cloud-foundry.model.github.GithubModel
Expand Down Expand Up @@ -133,18 +244,6 @@
});
},

/**
* @function onRepos
* @memberof cloud-foundry.model.github.GithubModel
* @description onRepos handler
* @param {string} response - the JSON response from API call
* @private
*/
onRepos: function (response) {
this.data.repos.length = 0;
[].push.apply(this.data.repos, response.data || []);
},

/**
* @function onBranches
* @memberof cloud-foundry.model.github.GithubModel
Expand Down Expand Up @@ -176,7 +275,7 @@
* @private
*/
onReposError: function () {
this.data.repos.length = 0;
this._resetRepos();
},

/**
Expand Down
Loading

0 comments on commit e36a870

Please sign in to comment.