Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

List of contributors fetched and stored to localStorage #61

Merged
merged 1 commit into from
Oct 10, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 62 additions & 27 deletions dist/community-toolbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
Expand All @@ -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 '<a href="https://github.com/' + org + '/'+ repo +'/commits?author='+ c.login + '">@' + c.login + '</a>';
});
var avatars = contributors.map(function(c) {
return '<a href="https://github.com/' + org + '/'+ repo +'/commits?author='+ c.login + '"><img width="100px" src="' + c.avatar_url + '"></a>';
});
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 '<a href="https://github.com/' + org + '/'+ repo +'/commits?author='+ c.login + '">@' + c.login + '</a>';
});
let avatars = contributors.map((c, i) => {
return '<a href="https://github.com/' + org + '/'+ repo +'/commits?author='+ c.login + '"><img width="100px" src="' + c.avatar_url + '"></a>';
});
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 '<a href="https://github.com/' + org + '/'+ repo +'/commits?author='+ c.login + '">@' + c.login + '</a>';
});
let avatars = repoContributors.map((c, i) => {
return '<a href="https://github.com/' + org + '/'+ repo +'/commits?author='+ c.login + '"><img width="100px" src="' + c.avatar_url + '"></a>';
});
totalContributors += repoContributors.length;
//push data to UI
ui.insertContributors(totalContributors, usernames, avatars);
}

}

Expand All @@ -81898,8 +81933,8 @@ CommunityToolbox = function CommunityToolbox(org, repo) {
getIssuesForRepo: getIssuesForRepo,
getIssuesForOrg: getIssuesForOrg,
getCommitsForRepo: getCommitsForRepo,
getRepoContributors: getRepoContributors,
getAllContributors: getAllContributors,
showRepoContributors: showRepoContributors,
displayIssuesForRepo: displayIssuesForRepo
}

Expand Down
2 changes: 1 addition & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ <h2 class="contributors-head">Contributors</h2>
.then(displayIssues('.candidates'));

// compile and display all contributors for given org/repo:
toolbox.getRepoContributors(org, repo);
toolbox.showRepoContributors(org, repo);

}

Expand Down
89 changes: 62 additions & 27 deletions src/community-toolbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
Expand All @@ -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 '<a href="https://github.com/' + org + '/'+ repo +'/commits?author='+ c.login + '">@' + c.login + '</a>';
});
var avatars = contributors.map(function(c) {
return '<a href="https://github.com/' + org + '/'+ repo +'/commits?author='+ c.login + '"><img width="100px" src="' + c.avatar_url + '"></a>';
});
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 '<a href="https://github.com/' + org + '/'+ repo +'/commits?author='+ c.login + '">@' + c.login + '</a>';
});
let avatars = contributors.map((c, i) => {
return '<a href="https://github.com/' + org + '/'+ repo +'/commits?author='+ c.login + '"><img width="100px" src="' + c.avatar_url + '"></a>';
});
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 '<a href="https://github.com/' + org + '/'+ repo +'/commits?author='+ c.login + '">@' + c.login + '</a>';
});
let avatars = repoContributors.map((c, i) => {
return '<a href="https://github.com/' + org + '/'+ repo +'/commits?author='+ c.login + '"><img width="100px" src="' + c.avatar_url + '"></a>';
});
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) {
Expand All @@ -256,8 +291,8 @@ CommunityToolbox = function CommunityToolbox(org, repo) {
getIssuesForRepo: getIssuesForRepo,
getIssuesForOrg: getIssuesForOrg,
getCommitsForRepo: getCommitsForRepo,
getRepoContributors: getRepoContributors,
getAllContributors: getAllContributors,
showRepoContributors: showRepoContributors,
displayIssuesForRepo: displayIssuesForRepo
}

Expand Down