Skip to content

Commit

Permalink
Fix site issues related to 403 errors.
Browse files Browse the repository at this point in the history
  • Loading branch information
Rishabh570 authored Jan 15, 2019
1 parent a4376e1 commit 75d7fee
Show file tree
Hide file tree
Showing 7 changed files with 5,295 additions and 6,822 deletions.
11,880 changes: 5,139 additions & 6,741 deletions dist/community-toolbox.js

Large diffs are not rendered by default.

4 changes: 0 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 15 additions & 17 deletions src/community-toolbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ CommunityToolbox = function CommunityToolbox(org, repo) {
// 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) {
if (lifespan!=null && ((timeNow-lifespan)/1000) >= 86400) {
localStorage.removeItem('repoContributors');
localStorage.removeItem('repoContributorsExpiry');
}
Expand All @@ -152,7 +152,7 @@ CommunityToolbox = function CommunityToolbox(org, repo) {

// If we don't have repoContributors in localStorage, we fetch them from Github
if (repoContributors == null || repoContributors.length == 0) {
repoContributorsUtility.fetchRepoContributors(org, repo)
repoContributorsUtility.fetchRepoContributorsUtil(org, repo)
.then(function gotRepoContributorsInStorage (contributors) {
// Map to contributors and store their usernames and avatar URLs to variables
let usernames = contributors.map(function getRepoContributorUsername(c, i) {
Expand Down Expand Up @@ -190,7 +190,6 @@ CommunityToolbox = function CommunityToolbox(org, repo) {
if(recencyLabel==='month') {
return getRecentCommitsUtility.getCommitsLastMonth(org)
.then(function gotCommits(commits) {
console.log("inside month then");
let totalCommits = commits.length;
let usernames = commits.map((commit, i) => {
return `<a href="${commit.author.html_url}">@${commit.author.login}</a>`;
Expand All @@ -203,20 +202,19 @@ CommunityToolbox = function CommunityToolbox(org, repo) {
return;
})
} else {
return getRecentCommitsUtility.getCommitsLastWeek(org)
.then(function gotCommits(commits) {
let totalCommits = commits.length;
let usernames = commits.map((commit, i) => {
return `<a href="${commit.author.html_url}">@${commit.author.login}</a>`;
})
let avatars = commits.map((commit, i) => {
return `<a href="${commit.author.html_url}" class="hvr-Glow"><img width="100px" src="${commit.author.avatar_url}"></a>`;
})
// Push data to UI
ui.insertRecentContributors(totalCommits,usernames, avatars);
return;
})
}
return getRecentCommitsUtility.getCommitsLastWeek(org).then((weekly_contribs) => {
let totalCommits = weekly_contribs.length;
let usernames = weekly_contribs.map((commit, i) => {
return `<a href="${commit.author.html_url}">@${commit.author.login}</a>`;
})
let avatars = weekly_contribs.map((commit, i) => {
return `<a href="${commit.author.html_url}" class="hvr-Glow"><img width="100px" src="${commit.author.avatar_url}"></a>`;
})
// Push data to UI
ui.insertRecentContributors(totalCommits,usernames, avatars);
return;
})
}
}

function displayIssuesForRepo(org, repo, label, selector) {
Expand Down
43 changes: 37 additions & 6 deletions src/getAllContribsUtility.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,44 @@


var SimpleApi = require("github-api-simple")
var api = new SimpleApi();
var ui = require('./ui');
var parse = require('parse-link-header');
var SimpleApi = require("github-api-simple")
var api = new SimpleApi();
var ui = require('./ui');
var parse = require('parse-link-header');

// Utility function which decides whether to make a single or multiple
// requests for fetching repo's contributors
function fetchRepoContributorsUtil(org, repo) {
return new Promise((resolve, reject) => {
if(repo==='plots2') {
resolve(fetchAllRepoContributors(org, repo));
}else {
resolve(fetchRepoContributors(org, repo));
}
})
}


// Fetches all the contributors for a particular repository
// Fetches the CONTRIBUTORS for a particular repository
function fetchRepoContributors(org, repo) {
// This array is used to hold all the contributors of a repo
let arr = [];

return api.Repositories
.getRepoContributors(org, repo, { method:"GET", qs: { sort: 'pushed', direction: 'desc', per_page: 100 } })
.then(function gotContributorsOnParticularPage(contributors) {
if (contributors!=undefined && (contributors != null || contributors.length > 0)) {
contributors.map((contributor, i) => arr.push(contributor));
}
})
.then(() => {
return arr;
});
}



// Fetches ALL THE CONTRIBUTORS for a particular repository
function fetchAllRepoContributors(org, repo) {
// This array is used to hold all the contributors of a repo
var arr = [];

Expand Down Expand Up @@ -80,7 +111,7 @@
// We take only first 20 repos to stay under API quota
var splicedRepos = repos.splice(0,20);
var promises = splicedRepos.map(function mapToEachRepo(repo, i) {
return fetchRepoContributors(org, repo)
return fetchRepoContributorsUtil(org, repo)
.then(function gotContribsForParticularRepo(repoContributors) {
if (repoContributors!=undefined && repoContributors.length>0) {
// Maps to each contributor and stores it in the array
Expand Down
112 changes: 62 additions & 50 deletions src/getRecentCommitsUtility.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
var getAllContribsUtility = require('./getAllContribsUtility');


function fetchRecentCommits(repos, queryTime, flag) {
function fetchRecentCommits(repos, queryTime) {
var commitersSet = new Set([]);
var results = [];
let timeToday = (new Date).getTime();
Expand Down Expand Up @@ -34,65 +34,78 @@ function fetchRecentCommits(repos, queryTime, flag) {
return Promise.all(promises)
.then(function promisesResolved() {
// Store recentCommits and recentCommitsExpiry in the localStorage
if(flag==='w') {
localStorage.setItem('recentCommits', JSON.stringify(results));
localStorage.setItem('recentCommitsExpiry', timeToday);
return results;
} else if(flag==='m') {
localStorage.setItem('recentMCommits', JSON.stringify(results));
localStorage.setItem('recentCommitsMExpiry', timeToday);
return results;
}
return true;
localStorage.setItem('recentMCommits', JSON.stringify(results));
localStorage.setItem('recentCommitsMExpiry', timeToday);
return results;
});
}


// Gets the list of active contributors last Week
// Fetches the list of active contributors last Week
function getCommitsLastWeek(org) {
let repos = JSON.parse(localStorage.getItem('repos'));
let recentCommitsExpiry = localStorage.getItem('recentCommitsExpiry');
let recentCommitsMExpiry = localStorage.getItem('recentCommitsExpiry');
let timeToday = (new Date).getTime();
// If recentCommits expiry time is 1 day behind the current time, flush them out.
if(recentCommitsExpiry!=null && ((timeToday-recentCommitsExpiry)/1000)>=86400) {
if(recentCommitsMExpiry!=null && ((timeToday-recentCommitsMExpiry)/1000)>=86400) {
localStorage.removeItem('recentCommitsExpiry');
localStorage.removeItem('recentCommits');
}

// We make queryTime 1 week behind the current time, to pass it as query in the request
let d = (new Date);
d.setDate(d.getDate() - 7);
let queryTime = d.toISOString();

var recentCommits = JSON.parse(localStorage.getItem('recentCommits'));
// This flag is used to distinguish the place to store the result in localStorage
let flag = 'w';

// There is no list of recentCommits in localStorage,
// we need to get it from Github
if(recentCommits==null || recentCommits.length==0) {
if(repos==null || repos.length == 0) {
return getAllContribsUtility.getAllRepos(org)
.then(function gotAllRepos(repos) {
return fetchRecentCommits(repos, queryTime, flag)
.then(function gotRecentCommitsInStorage(commits) {
return commits;
})
});
} else {
// Repos are in the localStorage, we saved a network call!
return fetchRecentCommits(repos, queryTime, flag)
.then(function gotRecentCommitsInStorage(commits) {
return commits;
let weekly_contribs = JSON.parse(localStorage.getItem('recentCommits'));
if(weekly_contribs!=null && weekly_contribs!=undefined && weekly_contribs.length>0) {
return new Promise((resolve, reject) => {resolve(weekly_contribs)});
}else {
let commits_last_month;
let contribs = [];
commits_last_month = JSON.parse(localStorage.getItem('recentMCommits'));
if(commits_last_month==null || commits_last_month==undefined || commits_last_month.length==0) {
return getCommitsLastMonth(org).then((commits) => {
commits_last_month = commits;
commits_last_month.map((commit_last_month, index) => {
let commit_date = commit_last_month['commit']['committer']['date'];
let check = within_this_week(commit_date);
if(check) {
contribs.push(commit_last_month);
}
});
}
} else {
// RecentCommits are in the localStorage, no need for any network call!!!
return new Promise(function returningPromise(resolve, reject) {resolve(recentCommits)});

let timeToday = (new Date).getTime();
localStorage.setItem('recentCommits', JSON.stringify(contribs));
localStorage.setItem('recentCommitsExpiry', timeToday);
return contribs;
});
}
else {
commits_last_month.map((commit_last_month, index) => {
let commit_date = commit_last_month['commit']['committer']['date'];
let check = within_this_week(commit_date);
if(check) {
contribs.push(commit_last_month);
}
});

let timeToday = (new Date).getTime();
localStorage.setItem('recentCommits', JSON.stringify(contribs));
localStorage.setItem('recentCommitsExpiry', timeToday);
return new Promise((resolve, reject) => {resolve(contribs)});
}
}
}

// Utility function that checks if a given date is behind the current date
// by 7 or less
function within_this_week(date) {
let current = (new Date).getTime();
let past_date = (new Date(`${date}`)).getTime();
let measure = Math.ceil(Math.abs(current - past_date) / (1000*3600*24));
if(measure<=7) {
return true;
}
return false;
}


// Fetches the list of active contributors last Week
function getCommitsLastMonth(org) {
let repos = JSON.parse(localStorage.getItem('repos'));
let recentCommitsMExpiry = localStorage.getItem('recentCommitsMExpiry');
Expand All @@ -109,23 +122,21 @@ function getCommitsLastMonth(org) {
let queryTime = d.toISOString();

var recentCommits = JSON.parse(localStorage.getItem('recentMCommits'));
// This flag is used to distinguish the place to store the result in localStorage
var flag = 'm';

// There is no list of recentCommits in localStorage,
// we need to get it from Github
if(recentCommits==null || recentCommits.length==0) {
if(repos==null || repos.length == 0) {
return getAllContribsUtility.getAllRepos(org)
.then(function gotAllRepos(repos) {
return fetchRecentCommits(repos, queryTime, flag)
return fetchRecentCommits(repos, queryTime)
.then(function gotRecentCommitsInStorage(commits) {
return commits;
})
});
} else {
// Repos are in the localStorage, we saved a network call!
return fetchRecentCommits(repos, queryTime, flag)
return fetchRecentCommits(repos, queryTime)
.then(function gotRecentCommitsInStorage(commits) {
return commits;
});
Expand All @@ -141,6 +152,7 @@ function getCommitsLastMonth(org) {
// EXPORTS
module.exports = {
fetchRecentCommits: fetchRecentCommits,
getCommitsLastMonth: getCommitsLastMonth,
getCommitsLastWeek: getCommitsLastWeek,
getCommitsLastMonth: getCommitsLastMonth
}
within_this_week: within_this_week
}
42 changes: 40 additions & 2 deletions src/repoContributorsUtility.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,21 @@ var SimpleApi = require("github-api-simple")
var api = new SimpleApi();
var parse = require('parse-link-header');

// This is a utility function which decides whether to a single request for fetching
// each repo's contributors or multiple ones.
function fetchRepoContributorsUtil(org, repo) {
return new Promise((resolve, reject) => {
if(repo === 'plots2') {
resolve(fetchAllRepoContributors(org, repo));
}else {
resolve(fetchRepoContributors(org, repo));
}
})
}

// This utility helps us in getting all the contributors for a particular repository
function fetchRepoContributors(org, repo) {

// This utility helps us in getting ALL THE CONTRIBUTORS for a particular repository
function fetchAllRepoContributors(org, repo) {
// This array is used to store the contributors from all of the repositories
let contributorsArray = [];

Expand Down Expand Up @@ -49,7 +61,33 @@ function fetchRepoContributors(org, repo) {




// This utility helps us in getting CONTRIBUTORS for a particular repository
function fetchRepoContributors(org, repo) {
// This array is used to store the contributors from all of the repositories
let contributorsArray = [];

return api.Repositories
.getRepoContributors(org, repo, { method:"GET", qs: { sort: 'pushed', direction: 'desc', per_page: 100 }})
.then(function gotRepoContributors(contributors) {
if (contributors!=undefined && (contributors != null || contributors.length > 0)) {
contributors.map((contributor, i) => contributorsArray.push(contributor));
}
})
.then(() => {
let now = (new Date).getTime();
localStorage.setItem('repoContributors', JSON.stringify(contributorsArray));
localStorage.setItem('repoContributorsExpiry', now);
return contributorsArray;
})

}



// EXPORTS
module.exports = {
fetchAllRepoContributors: fetchAllRepoContributors,
fetchRepoContributors: fetchRepoContributors,
fetchRepoContributorsUtil: fetchRepoContributorsUtil,
}
4 changes: 2 additions & 2 deletions src/ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,15 @@ var insertRecentContributorsExec = false;

function insertContributors(totalContributors, usernames, avatars){
if(insertContributorsExec) $('.contributors > .usernames').append(', ');
$('.contributors-head').html('Contributors ('+totalContributors+')');
$('.contributors-head').html('Contributors ('+totalContributors+'+)');
$('.contributors > .usernames').append(usernames.join(', '));
$('.contributors > .avatars').append(avatars.join(''));
insertContributorsExec=true;
}

function insertRecentContributors(totalContributors, usernames, avatars){
if(insertRecentContributorsExec) $('.recent-contributors > .usernames').append(', ');
$('.recent-contributors-head').html('Recent Contributors ('+totalContributors+')');
$('.recent-contributors-head').html('Recent Contributors ('+totalContributors+'+)');
$('.recent-contributors > .usernames').html(usernames.join(', '));
$('.recent-contributors > .avatars').html(avatars.join(''));
insertRecentContributorsExec=true;
Expand Down

0 comments on commit 75d7fee

Please sign in to comment.