diff --git a/dist/community-toolbox.js b/dist/community-toolbox.js index 73ebba11..fe2ba4ac 100644 --- a/dist/community-toolbox.js +++ b/dist/community-toolbox.js @@ -81834,10 +81834,11 @@ CommunityToolbox = function CommunityToolbox(org, repo) { } function getRepoContributors(org, repo) { + let contributorsArray = []; - var getPages = api.Repositories + return api.Repositories .getRepoContributors(org, repo, {method: "HEAD", qs: { sort: 'pushed', direction: 'desc', per_page: 100 } }) - .then(function(contribData) { + .then((contribData) => { var headers = contribData; if (headers.hasOwnProperty("link")) { var parsed = parse(headers['link']); @@ -81846,33 +81847,67 @@ CommunityToolbox = function CommunityToolbox(org, repo) { totalPages = 1; } return totalPages; + }) + .then((totalPages) => { + let promises = []; + for(let i = 1; i <= totalPages; i++) { + var currentPromise = api.Repositories + .getRepoContributors(org, repo, { method:"GET", qs: { sort: 'pushed', direction: 'desc', per_page: 100, page:i } }) + .then((contributors) => { + if (contributors!=undefined && (contributors != null || contributors.length > 0)) { + contributors.map((contributor, i) => contributorsArray.push(contributor)); + } + }); + promises.push(currentPromise); + } + return Promise.all(promises) + .then(()=> { + let now = (new Date).getTime(); + localStorage.setItem('repoContributors', JSON.stringify(contributorsArray)); + localStorage.setItem('repoContributorsExpiry', now); + return contributorsArray; + }); }); + } - //define totalContributors - var totalContributors = 0; - - // get data given the page number - function getData(curPage){ - api.Repositories - .getRepoContributors(org, repo, { method:"GET", qs: { sort: 'pushed', direction: 'desc', per_page: 100, page:curPage } }) - .then(function(contributors) { - var usernames = contributors.map(function(c) { - return '@' + c.login + ''; - }); - var avatars = contributors.map(function(c) { - return ''; - }); - totalContributors += contributors.length; - //push data to UI - ui.insertContributors(totalContributors, usernames, avatars); - }); - } + function showRepoContributors(org, repo) { + let totalContributors = 0; - getPages.then(function(totalPages){ - for(var i = 1; i <= totalPages; i++) { - getData(i); - } - }); + // Flushes repoContributors from localStorage after every single day + let timeNow = (new Date).getTime(); + let lifespan = localStorage.getItem('repoContributorsExpiry'); + if (lifespan!=null && ((timeNow-lifespan)/1000) >= 43200) { + localStorage.removeItem('repoContributors'); + localStorage.removeItem('repoContributorsExpiry'); + } + let repoContributors = JSON.parse(localStorage.getItem('repoContributors')); + + // If we don't have repoContributors in localStorage, we fetch them from Github + if (repoContributors == null || repoContributors.length == 0) { + getRepoContributors(org, repo).then((contributors) => { + let usernames = contributors.map((c, i) => { + return '@' + c.login + ''; + }); + let avatars = contributors.map((c, i) => { + return ''; + }); + totalContributors += contributors.length; + //push data to UI + ui.insertContributors(totalContributors, usernames, avatars); + }) + } + // If we have repoContributors in localStorage, we save a network call :) + else { + let usernames = repoContributors.map((c, i) => { + return '@' + c.login + ''; + }); + let avatars = repoContributors.map((c, i) => { + return ''; + }); + totalContributors += repoContributors.length; + //push data to UI + ui.insertContributors(totalContributors, usernames, avatars); + } } @@ -81898,8 +81933,8 @@ CommunityToolbox = function CommunityToolbox(org, repo) { getIssuesForRepo: getIssuesForRepo, getIssuesForOrg: getIssuesForOrg, getCommitsForRepo: getCommitsForRepo, - getRepoContributors: getRepoContributors, getAllContributors: getAllContributors, + showRepoContributors: showRepoContributors, displayIssuesForRepo: displayIssuesForRepo } diff --git a/index.html b/index.html index 942d8858..ca48ea82 100644 --- a/index.html +++ b/index.html @@ -186,7 +186,7 @@

Contributors

.then(displayIssues('.candidates')); // compile and display all contributors for given org/repo: - toolbox.getRepoContributors(org, repo); + toolbox.showRepoContributors(org, repo); } diff --git a/src/community-toolbox.js b/src/community-toolbox.js index e6f9d0ff..e02e6a2c 100644 --- a/src/community-toolbox.js +++ b/src/community-toolbox.js @@ -192,10 +192,11 @@ CommunityToolbox = function CommunityToolbox(org, repo) { } function getRepoContributors(org, repo) { + let contributorsArray = []; - var getPages = api.Repositories + return api.Repositories .getRepoContributors(org, repo, {method: "HEAD", qs: { sort: 'pushed', direction: 'desc', per_page: 100 } }) - .then(function(contribData) { + .then((contribData) => { var headers = contribData; if (headers.hasOwnProperty("link")) { var parsed = parse(headers['link']); @@ -204,34 +205,68 @@ CommunityToolbox = function CommunityToolbox(org, repo) { totalPages = 1; } return totalPages; + }) + .then((totalPages) => { + let promises = []; + for(let i = 1; i <= totalPages; i++) { + var currentPromise = api.Repositories + .getRepoContributors(org, repo, { method:"GET", qs: { sort: 'pushed', direction: 'desc', per_page: 100, page:i } }) + .then((contributors) => { + if (contributors!=undefined && (contributors != null || contributors.length > 0)) { + contributors.map((contributor, i) => contributorsArray.push(contributor)); + } + }); + promises.push(currentPromise); + } + return Promise.all(promises) + .then(()=> { + let now = (new Date).getTime(); + localStorage.setItem('repoContributors', JSON.stringify(contributorsArray)); + localStorage.setItem('repoContributorsExpiry', now); + return contributorsArray; + }); }); + } - //define totalContributors - var totalContributors = 0; - - // get data given the page number - function getData(curPage){ - api.Repositories - .getRepoContributors(org, repo, { method:"GET", qs: { sort: 'pushed', direction: 'desc', per_page: 100, page:curPage } }) - .then(function(contributors) { - var usernames = contributors.map(function(c) { - return '@' + c.login + ''; - }); - var avatars = contributors.map(function(c) { - return ''; - }); - totalContributors += contributors.length; - //push data to UI - ui.insertContributors(totalContributors, usernames, avatars); - }); + function showRepoContributors(org, repo) { + let totalContributors = 0; + + // Flushes repoContributors from localStorage after every single day + let timeNow = (new Date).getTime(); + let lifespan = localStorage.getItem('repoContributorsExpiry'); + if (lifespan!=null && ((timeNow-lifespan)/1000) >= 43200) { + localStorage.removeItem('repoContributors'); + localStorage.removeItem('repoContributorsExpiry'); + } + let repoContributors = JSON.parse(localStorage.getItem('repoContributors')); + + // If we don't have repoContributors in localStorage, we fetch them from Github + if (repoContributors == null || repoContributors.length == 0) { + getRepoContributors(org, repo).then((contributors) => { + let usernames = contributors.map((c, i) => { + return '@' + c.login + ''; + }); + let avatars = contributors.map((c, i) => { + return ''; + }); + totalContributors += contributors.length; + //push data to UI + ui.insertContributors(totalContributors, usernames, avatars); + }) + } + // If we have repoContributors in localStorage, we save a network call :) + else { + let usernames = repoContributors.map((c, i) => { + return '@' + c.login + ''; + }); + let avatars = repoContributors.map((c, i) => { + return ''; + }); + totalContributors += repoContributors.length; + //push data to UI + ui.insertContributors(totalContributors, usernames, avatars); } - getPages.then(function(totalPages){ - for(var i = 1; i <= totalPages; i++) { - getData(i); - } - }); - } function displayIssuesForRepo(org, repo, label, selector) { @@ -256,8 +291,8 @@ CommunityToolbox = function CommunityToolbox(org, repo) { getIssuesForRepo: getIssuesForRepo, getIssuesForOrg: getIssuesForOrg, getCommitsForRepo: getCommitsForRepo, - getRepoContributors: getRepoContributors, getAllContributors: getAllContributors, + showRepoContributors: showRepoContributors, displayIssuesForRepo: displayIssuesForRepo }