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
134 lines (113 loc) · 3.98 KB
/
script.js
File metadata and controls
134 lines (113 loc) · 3.98 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
126
127
128
129
130
131
132
133
134
// Token
const options = {
method: 'GET',
headers: {
Authorization: `TOKEN ${API_TOKEN}`
}
}
// DOM selectors
const projectInfo = document.getElementById('projectInfo')
const userInfo = document.getElementById('userInfo')
const aboutUser = document.getElementById('aboutUser')
const profilePicture = document.getElementById('profilePicture')
const mainContent = document.getElementById('mainContent')
// Github API
const username = 'emmajosefina'
const API_URL = `https://api.github.com/users/${username}/repos`
const API_USER = `https://api.github.com/users/${username}`
//------------------ FIRST FETCH - USER PROFILE -----------------------//
const getProfile = () => {
fetch(API_USER, options)
.then((res) => res.json())
.then((data) => {
mainContent.innerHTML +=
`
<section class="profile-box">
<img src="${data.avatar_url}" id="profilePicture" class="profile-picture" />
<p class="full-name">${data.name}</p>
<p class="username">${data.login}</p>
</section>
<div class="status-box">
<p class="status-text">${data.bio} 👩💻</p>
</div>
`;
});
};
getProfile();
//------------------ SECOND FETCH - ALL REPOS -----------------------//
const findingAllRepos = (repos) => {
fetch(API_URL, options)
.then((res) => res.json())
.then((data) => {
// Fetches only repositories from Technigo //
const forkedRepos = data.filter((repo) => repo.fork && repo.name.startsWith('project-'))
forkedRepos.forEach((repo) =>
projectInfo.innerHTML +=
`
<div class="projectContainer">
<p class="smallerContainer">
<span class="header-project">
${repo.name.replace('project-', '').replace('-', ' ')}
</span>
</p>
<p class="smallerContainer">
<span> Updated:</span>
<span> ${new Date(repo.pushed_at).toLocaleDateString('en-SE', {year: 'numeric', month: 'short', day: 'numeric'})}</span>
</p>
<p class="smallerContainer">
<span>
Default branch: </span>
<span>${repo.default_branch}</span>
</p>
<p class="smallerContainer" id="commit-${repo.name}">
<span>
Number of commits:
</span>
</p>
<p class="smallerContainer" id="pull-request-${repo.name}">
<span>Pull requests: </span>
</p>
<p class="smallerContainer">
<img src="images/github-logo-extra-big.png" alt="github-logo" width="15px" />
<span><a class="project-url" href="${repo.svn_url}">
${repo.name}</span>
</a>
</p>
</div>
`
)
getPullRequest(forkedRepos)
renderChart(forkedRepos.length)
})
}
findingAllRepos()
const getCommits = (myCommitsUrl, myRepoName) => {
fetch(myCommitsUrl, options)
.then((res) => {
return res.json()
})
.then((data) => {
document.getElementById(`commit-${myRepoName}`).innerHTML += data.length
})
}
//------------------ THIRD FETCH - PULL REQUESTS -----------------------//
const getPullRequest = (repos) => {
repos.forEach((repo) => {
fetch(`https://api.github.com/repos/Technigo/${repo.name}/pulls?per_page=100`, options)
.then((res) => res.json())
.then((data) => {
//Filter out pullrequests
const pulls = data.find((pull) => pull.user.login === repo.owner.login)
const myPullRequests = data.filter((pullRequest) => {
return pullRequest.user.login === username
})
document.getElementById(`pull-request-${repo.name}`).innerHTML = `Pull Request: ${myPullRequests.length}`
if (pulls) {
getCommits(pulls.commits_url, repo.name)
} else {
document.getElementById(
`commit-${repo.name}`).innerHTML += `no commits visible, pull request unavailable.`;
}
});
});
};