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
154 lines (135 loc) · 4.77 KB
/
script.js
File metadata and controls
154 lines (135 loc) · 4.77 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
const user = 'Asivol93'
const REPOS_URL = `https://api.github.com/users/${user}/repos`
const USER_URL = `https://api.github.com/users/${user}`
const githubImg = './images/big-github_icon.png'
const githubProfile = `https://github.com/${user}`
const userContainer = document.getElementById('userProfile')
const projectsContainer = document.getElementById('projectsContainer')
const userDetailContainer = document.getElementById('userDetails')
const modal = document.getElementById('myModal') // Get the modal
const span = document.getElementsByClassName('close')[0] // Get the <span> element that closes the modal
//Fetches general user info
const userProfile = () => {
fetch(USER_URL)
.then((res) => res.json())
.then((data) => {
userContainer.innerHTML += `
<div class="user-section">
<h2>Username: ${data.login}</h2>
<p>Full name: ${data.name}</p>
<p>Location: ${data.location}</p>
<div class=swap-on-hover>
<a href="${githubProfile}" target="blank">
<img class="front-image" src="${data.avatar_url}"/>
<img class="back-image" src="${githubImg}">
</div>
</a>
</div>
`
})
}
//Fetches all repositories that are forked and starts with "project-" to get the ones from Technigo
const fetchAll = () => {
fetch(REPOS_URL)
.then((res) => res.json())
.then((data) => {
const forkedRepos = data.filter(
(repo) => repo.fork && repo.name.startsWith('project-')
)
//Sorts and displays the projects by the newest to the latest projects
forkedRepos.sort((a, b) => {
return new Date(b.pushed_at) - new Date(a.pushed_at)
})
//Formates the date nicely
forkedRepos.forEach((repo) => {
const pushedDate = new Date(repo.pushed_at).toLocaleDateString(
'en-se',
{
hour: '2-digit',
minute: '2-digit',
weekday: 'short',
year: 'numeric',
month: 'short',
day: 'numeric',
}
)
projectsContainer.innerHTML += `
<div class="repo-item">
<div class="icon-container">
<a href="${repo.html_url}" target="blank">
<img src="./images/github-icon_black.png" class="github-icon">
</a>
</div>
<h3><a href="${repo.html_url}" target="blank">${repo.name}</a></h3>
<p>Branch: ${repo.default_branch}<p>
<p>Latest push: ${pushedDate}</p>
<p id="commit-${repo.name}"></p>
</div>
`
})
drawChart(forkedRepos.length)
pullRequests(forkedRepos)
})
.catch(() => {
userContainer.innerHTML = `
<h1>Sorry we could not find any data!</h1>
`
})
}
//Gets the PR from the forked repos.
const pullRequests = (repos) => {
repos.forEach((repo) => {
fetch(
`https://api.github.com/repos/technigo/${repo.name}/pulls?per_page=100`
)
.then((res) => res.json())
.then((data) => {
const myPulls = data.find(
(pull) => pull.user.login === repo.owner.login
)
if (myPulls) {
showCommits(myPulls.commits_url, repo.name)
} else {
//Displays message if not commits are made
document.getElementById(`commit-${repo.name}`).innerHTML = `
<h3>No pull request done</h3>
<p>(either ongoing project or a group/pair project)</p>`
}
})
})
}
//Displayes the commit messages for the repos that have a PR
const showCommits = (url, myRepoName) => {
fetch(url)
.then((res) => res.json())
.then((data) => {
let commitMessage = data[data.length - 1].commit.message
document.getElementById(`commit-${myRepoName}`).innerHTML += `
<p>Number of commits: ${data.length}</p>
<button id="myBtn-${myRepoName}" class="modal-button">Read commit</button>
`
document.getElementById('myModal').innerHTML += `
<div id="modalContent" class="modal-content">
<button class="close">×</button>
<p class="modal-message">${myRepoName} latest commit: ${commitMessage}</p>
</div>
`
const btn = document.getElementById(`myBtn-${myRepoName}`)
//Opens the commit msg in a modal by clicking at the button
btn.onclick = function () {
document.getElementById('myModal').style.display = 'block'
}
//Closes the modal when clicking at X
document.getElementsByClassName('close')[0].onclick = function () {
document.getElementById('myModal').style.display = 'none'
}
//Have not made this function work yet, closes the modal when clicking outside of it
window.onclick = function (event) {
if (event.target == modal) {
document.getElementById('myModal').style.display = 'none'
}
}
})
}
userProfile()
fetchAll()