forked from Technigo/project-github-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
125 lines (115 loc) · 4.18 KB
/
script.js
File metadata and controls
125 lines (115 loc) · 4.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
// //GLOBAL VARIABLES
// const options = {
// method: 'GET',
// headers: {
// Authorization: `token ****`,
// },
// };
const USER = 'jenquach'
const USER_URL = `https://api.github.com/users/${USER}`
const REPOS_URL = `https://api.github.com/users/${USER}/repos`
const headerContainer = document.getElementById('topnav')
const profileContainer = document.getElementById('user-info')
const projectsContainer = document.getElementById('projects')
const pullRequests = document.getElementById('pullrequests')
// function to fetch the user data from GitHub API
const userProfile = () => {
fetch(USER_URL)
.then(res => res.json())
.then(data => {
profileContainer.innerHTML = `
<div class="picture">
<img class="user-picture" src="${data.avatar_url}"
</div>
<div class="user-information">
<h2>${data.name}</h2>
<a href="https://github.com/jenquach"><i class="fab fa-github"></i> ${data.login}</a>
<p><i class="fas fa-map-marker-alt"></i> ${data.location}</p>
</div>
`
})
}
// function to fetch the technigo repositories
const getRepositories = () => {
fetch(REPOS_URL)
.then(res => res.json())
.then(data => {
// filtering the technigo repositories
const technigoRepositories = data.filter(
repo => repo.fork && repo.name.includes('project-')
)
technigoRepositories.forEach(repo => {
projectsContainer.innerHTML += `
<div class="project">
<div class="repo-info">
<a href="${repo.html_url}">${repo.name}</a>
<p class="push">Recent push ${new Date(repo.pushed_at).toLocaleString("sv-SE", {dateStyle: 'short'})}</p>
<div id="language-${repo.name}" class="progress"></div>
</div>
<div class="repo-details">
<p id="branch">${repo.default_branch}</p>
<p class="commit" id="commit-${repo.name}"><i class="fas fa-code-branch"></i> </p>
</div>
</div>
`
getLanguages(repo)
})
// Draw chart with technigoRepositories data
drawChart(technigoRepositories.length)
getPullRequests(technigoRepositories)
})
}
// function to fetch all pull requests
const getPullRequests = (allRepositories) => {
allRepositories.forEach(repo => {
const myRepoName = repo.name
const PULL_URL = `https://api.github.com/repos/Technigo/${repo.name}/pulls?per_page=100`
fetch(PULL_URL)
.then(res => res.json())
.then(data => { //data is the array of all pull requests
// Detect if we have pull request or not.
const myPullRequest = data.find(
pull => pull.user.login === repo.owner.login
)
// If yes - call fetchCommits function
if (myPullRequest) {
getCommits(myPullRequest.commits_url, repo.name);
// If no - commits 0 is shown.
} else {
document.getElementById(`commit-${repo.name}`).innerHTML =
'<i class="fas fa-code-branch"></i> 0';
}
})
})
}
// function to fetch number of commits
const getCommits = (myCommitsUrl, myRepoName) => {
fetch(myCommitsUrl, )
.then(res => res.json())
.then(data => {
document.getElementById(`commit-${myRepoName}`).innerHTML += data.length
})
}
// function to fetch all the languages used in the repository
const getLanguages = (repo) => {
const languages_URL = `https://api.github.com/repos/${USER}/${repo.name}/languages`
fetch(languages_URL, )
.then(res => res.json())
.then(data => {
const html = data.HTML || 0;
const css = data.CSS || 0;
const js = data.JavaScript || 0;
const sum = html + css + js;
// calculates the percentage used of each language
const htmlPercentage = ((html / sum) * 100).toFixed(1);
const cssPercentage = ((css / sum) * 100).toFixed(1);
const jsPercentage = ((js / sum) * 100).toFixed(1);
document.getElementById(`language-${repo.name}`).innerHTML = `
<div class="language-html" title="html" style="width:${htmlPercentage}%;"></div>
<div class="language-css" title="css" style="width:${cssPercentage}%;"></div>
<div class="language-js" title="javascript" style="width:${jsPercentage}%;"></div>
`
})
}
userProfile()
getRepositories()