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
231 lines (185 loc) · 8.02 KB
/
script.js
File metadata and controls
231 lines (185 loc) · 8.02 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
//DOM Selectors
const toggleSwitch = document.getElementById('switch')
const modalWrapper = document.getElementById('modal-wrapper')
const modalBtn = document.getElementById("modal-btn")
const myProjects = document.getElementById('section-projects')
//Global variables
const username = 'emmahogberg88'
const URL_REPO = `https://api.github.com/users/${username}/repos`
//const API_TOKEN = TOKEN || process.env.API_KEY
//option for authorization
const options = {
method: 'GET',
headers: {
Authorization: `token ${API_TOKEN}` // you need to paste your token over here.
}
}
///////////////////////////////////////////////////////////////////////////
////////////////////// REPO DATA ////////////////////////
///////////////////////////////////////////////////////////////////////////
const gitHubData = () => {
fetch(URL_REPO, options)
.then(res => res.json())
.then(data => {
const technigoProjects = data.filter(repo => repo.name.startsWith('project-') && repo.fork)
document.getElementById('profile-picture').innerHTML = `
<img class="box-shadow" src='${data[0].owner.avatar_url}' alt='image of emmahogberg88 at GitHub'>
`
document.getElementById('username').innerHTML = `
<h4><a class="header nav-link" href='${data[0].html_url}'><i class="fab fa-github"></i> ${username}</a></h4>
`
//Toggle switch to toggle between sorting projects by latest updated project or by name
let toggleStatus = true
toggleSwitch.addEventListener('click', () => {
if (toggleStatus){
technigoProjects.sort((a, b) => new Date(a.pushed_at) < new Date(b.pushed_at) ? 1 : -1)
myProjects.innerHTML = ''
repoData(technigoProjects)
toggleStatus = false
} else if (!toggleStatus){
technigoProjects.sort((a, b) => a.name > b.name ? 1 : -1)
myProjects.innerHTML = ''
repoData(technigoProjects)
toggleStatus = true
}
})
repoData(technigoProjects)
//Pass amount of finished technigo projects to progressChart in chart.js file
progressChart(technigoProjects.length)
})
.catch(err => console.error(err))
}
//--display repo information--//
const repoData = (technigoProjects) => {
technigoProjects.forEach(repo => {
const reponame = repo.name
//Display HTML for all GitHub repos on website, setting dynamic ID to be able to add on more HTML in another function
myProjects.innerHTML += `
<div class="section-projects__card box-shadow">
<div>
<h3 class="repo-title bold-text"><a class="nav-link" href='${repo.html_url}'><i class="fab fa-github"></i> ${repo.name}</a></h3>
<div class="repo-languages">
<p id="languages-${repo.name}"></p>
</div>
<p><span class="bold-text">Default branch:</span> ${repo.default_branch}</p>
<p><span class="bold-text">Latest update:</span> ${new Date(repo.updated_at).toLocaleDateString('en-GB', {year: 'numeric', month: 'long', day: 'numeric'})}</p>
<p id="commits-${repo.name}"></p>
<p id="comments-${repo.name}"></p>
</div>
<div class="card-btn flex">
<p id="website-${repo.name}"></p>
<p id="comments-btn-${repo.name}"></p>
</div>
</div>
`
languagesData(username, reponame)
/////////////////////////////////////////////////////////////////////////////
//////////////////////// PULL REQUEST DATA ///////////////////////
///////////////////////////////////////////////////////////////////////////
fetch(`https://api.github.com/repos/Technigo/${reponame}/pulls?per_page=100`, options)
.then(res => res.json())
.then(data => {
data.forEach( repo => {
if(repo.user.login === username) {
let reponame = repo.base.repo.name
//get url for commits, comments and netlify to use in new fetch
displayCommits(repo.commits_url, reponame)
displayComments(repo.review_comments_url, reponame)
displayLink(repo.body, reponame)
} else if (repo.user.login === 'tiiliu' && reponame === 'project-chatbot') {
displayCommits(repo.commits_url, reponame)
displayComments(repo.review_comments_url, reponame)
displayLink('https://movie-bot.netlify.app/', reponame)
} else {
document.getElementById(`commits-${reponame}`).innerHTML = 'No commits for this repo'
document.getElementById(`comments-${reponame}`).innerHTML = ''
}
})
})
.catch(err => console.error(err))
})
}
/////////////////////////////////////////////////////////////////////////////
//////////////////////// LANGUAGES DATA ///////////////////////
///////////////////////////////////////////////////////////////////////////
const languagesData = (username, reponame) => {
fetch(`https://api.github.com/repos/${username}/${reponame}/languages`, options)
.then(res => res.json())
.then(data => {
// create an array with languages used by using the object keys.
const arrayLanguages = (Object.keys(data))
arrayLanguages.forEach(item => {
document.getElementById(`languages-${reponame}`).innerHTML += `<span class="repo-languages--item">${item}</span>`
console.log('item', arrayLanguages)
})
})
}
////////////////////////////////////////////////////////////////////////////
/////////////////////////// COMMITS DATA /////////////////////////
//////////////////////////////////////////////////////////////////////////
const displayCommits = (commitsUrl, reponame) => {
fetch(commitsUrl, options)
.then(res => res.json())
.then(data => {
document.getElementById(`commits-${reponame}`).innerHTML = `
<p>
<span class="bold-text">Amount of commits:</span> ${data.length}
</p>`
})
.catch(err => console.error(err))
}
////////////////////////////////////////////////////////////////////////////
////////////////////////// COMMENTS DATA /////////////////////////
//////////////////////////////////////////////////////////////////////////
const displayComments = (commentsUrl, reponame) => {
fetch(commentsUrl, options)
.then(res => res.json())
.then(data => {
if (data.length > 0) {
let commentsArray = []
data.forEach(comment => {
if (comment.user.login !== username) {
document.getElementById(`comments-${reponame}`).innerHTML = `
<p>
<span class="bold-text">Reviewed by:</span>
<a class="nav-link" href="${data[0].user.html_url}"> ${data[0].user.login}</a>
</p>
`
document.getElementById(`comments-btn-${reponame}`).innerHTML = `
<button class="filled-button" id="click-${reponame}">comments 💬</button>
`
commentsArray.push(comment.body)
document.getElementById(`click-${reponame}`).addEventListener('click', () => {
commentsArray.forEach(item => {
modalWrapper.style.display = 'block'
document.getElementById('modal').innerHTML += `
<p><span class="bold-text modal-header">Comment from ${data[0].user.login}</span></p>
<p class="modal-text">${item}</p>
<br>
`
})
})
}
})
}
})
.catch(err => console.error(err))
}
////////////////////////////////////////////////////////////////////////////
/////////////////////////// NETLIFY LINK /////////////////////////
//////////////////////////////////////////////////////////////////////////
const displayLink = (netlifyUrl, reponame) => {
document.getElementById(`website-${reponame}`).innerHTML = `
<a href="${netlifyUrl}" target="_blank">
<button class="outlined-button">visit website</button>
</a>
`
}
gitHubData()
////////////////////////////////////////////////////////////////////////////
//////////////////////// EVENTLISTENER //////////////////////
//////////////////////////////////////////////////////////////////////////
modalBtn.addEventListener('click', () => {
modalWrapper.style.display = 'none'
document.getElementById('modal').innerHTML = ''
})