Skip to content

Commit

Permalink
refactor github-stats
Browse files Browse the repository at this point in the history
  • Loading branch information
menghif committed Nov 16, 2021
1 parent cdc2dd0 commit c2148cd
Showing 1 changed file with 19 additions and 31 deletions.
50 changes: 19 additions & 31 deletions src/api/status/public/assets/js/github-stats.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
const fetchGitHubData = (repo) => {
const fetchGitHubApi = (owner, repo, path) => {
return fetch(`https://api.github.com/repos/${owner}/${repo}/${path}`, {
headers: { Accept: 'application/vnd.github.v3+json', }
});
}

const getGitHubData = (owner, repo) => {
let weeklyCommits = 0;

// get weekly commits for last year: https://docs.github.com/en/rest/reference/repos#get-the-weekly-commit-count
fetch(`https://api.github.com/repos/Seneca-CDOT/${repo}/stats/participation`, {
headers: {
Accept: 'application/vnd.github.v3+json',
},
})
.then((response) => response.json())
fetchGitHubApi(owner, repo, 'stats/participation')
.then((res) => res.json())
.then((data) => {
weeklyCommits = data.all[data.all.length - 1];
document.getElementById(`weekly-commits-${repo}`).innerHTML = weeklyCommits;
Expand All @@ -19,12 +21,8 @@ const fetchGitHubData = (repo) => {
.catch((error) => console.log(error));

// get weekly commits activity: https://docs.github.com/en/rest/reference/repos#get-the-weekly-commit-activity
fetch(`https://api.github.com/repos/Seneca-CDOT/${repo}/stats/code_frequency`, {
headers: {
Accept: 'application/vnd.github.v3+json',
},
})
.then((response) => response.json())
fetchGitHubApi(owner, repo, 'stats/code_frequency')
.then((res) => res.json())
.then((data) => {
if (weeklyCommits) {
document.getElementById(`lines-added-${repo}`).innerHTML = data[data.length - 1][1];
Expand All @@ -36,13 +34,9 @@ const fetchGitHubData = (repo) => {
})
.catch((error) => console.log(error));

// Get latest author from commits list: https://docs.github.com/en/rest/reference/repos#list-commits
fetch(`https://api.github.com/repos/Seneca-CDOT/${repo}/commits`, {
headers: {
Accept: 'application/vnd.github.v3+json',
},
})
.then((response) => response.json())
// get latest author from commits list: https://docs.github.com/en/rest/reference/repos#list-commits
fetchGitHubApi(owner, repo, 'commits')
.then((res) => res.json())
.then((data) => {
const avatar = data[0].author.avatar_url;
const username = data[0].author.login;
Expand All @@ -58,13 +52,9 @@ const fetchGitHubData = (repo) => {
})
.catch((error) => console.log(error));

// get total contributors
fetch(`https://api.github.com/repos/Seneca-CDOT/${repo}/contributors?per_page=1`, {
headers: {
Accept: 'application/vnd.github.v3+json',
},
})
.then((response) => response.headers.get('link'))
// get total contributors: https://docs.github.com/en/rest/reference/repos#list-repository-contributors
fetchGitHubApi(owner, repo, 'contributors?per_page=1')
.then((res) => res.headers.get('link'))
.then((data) => {
const contributors = data.match(/.*"next".*&page=([0-9]*).*"last".*/)[1];
document.getElementById(`total-contributors-${repo}`).innerHTML = contributors;
Expand All @@ -73,8 +63,6 @@ const fetchGitHubData = (repo) => {
};

document.addEventListener('DOMContentLoaded', () => {
fetchGitHubData('telescope');
fetchGitHubData('satellite');
getGitHubData('Seneca-CDOT','telescope');
getGitHubData('Seneca-CDOT', 'satellite');
});

response.headers.get('link');

0 comments on commit c2148cd

Please sign in to comment.