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
135 lines (111 loc) · 4.07 KB
/
script.js
File metadata and controls
135 lines (111 loc) · 4.07 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
//DOM SELECTORS
const userSection = document.getElementById("user-section")
//GLOBAL VARIABLES
/* const options = {
method: 'GET',
headers: {
Authorization: `token xxx`
},
}; */
const REPO_API = "https://api.github.com/users/nehrwein/repos";
const totalProjects = 19;
//FUNCTIONS
const getRepos = () => {
fetch(REPO_API, /* options */)
.then((res) => res.json())
.then((data) => {
//forkedRepos shows a list of all repos that are forked ones from Technigo
const forkedRepos = data.filter(repo => repo.fork && repo.name.startsWith('project-'))
//show the newest repos first (default)
forkedRepos.sort((a, b) => {
return new Date(b.pushed_at) - new Date(a.pushed_at);
});
//My name, username and profile picture
const userName = data[0].owner.login
const profilePic = data[0].owner.avatar_url
userSection.innerHTML += /* html */`
<div class="userImage_Text">
<div class="userImageDiv">
<img class="userImage" id="userImage" src="${profilePic}" alt="Github Avatar">
</div>
<div class="userTextDiv">
<p class="myName">Birgit</p>
<p class="userName">${userName}</p>
</div>
</div>
<label for="sort"></label>
<select class="sort" name="sort" id="sort">
<option disabled="" value="">Sorted by: </option>
<option value="updated">Last updated</option>
<option value="name">Name</option>
</select>
`
const sortBtn = document.getElementById('sort')
sortBtn.addEventListener('change', () => sort(sortBtn.value, forkedRepos))
drawProjects(forkedRepos);
drawChart(forkedRepos.length)
})
}
//sorting the projects by name, last updated
const sort = (value, repos) => {
if (value === "name") {
repos.sort((a, b) => {
return (a.name) > (b.name) ? 1 : -1;
});
drawProjects(repos)
} else if (value === "updated") {
repos.sort((a, b) => {
return new Date(b.pushed_at) - new Date(a.pushed_at);
});
drawProjects(repos)
}
}
const drawProjects = (forkedRepositories) => {
document.getElementById('projects-section').innerHTML = ` `
forkedRepositories.forEach((repo) => {
document.getElementById('projects-section').innerHTML += `
<div class="projects-div" id="projects">
<a href="${repo.html_url}">${repo.name}</a>
<p>default branch: ${repo.default_branch}</p>
<p>Last push: ${new Date(repo.pushed_at).toLocaleDateString('en-GB')}</p>
<p class="noOfCommits" id="commit-${repo.name}">Commits: 0</p>
<ul id="commitMessages-${repo.name}"></ul>
</div>
`;
getCommits(forkedRepositories, repo.name);
});
forkedRepositories.forEach((repo) => {
document
.getElementById(`commit-${repo.name}`)
.addEventListener('click', () => {
document
.getElementById(`commitMessages-${repo.name}`)
.classList.toggle('active');
});
});
};
const getCommits = (filteredArray, myRepoName) => {
//First make a new array with all the needed APIs (found under commit_urls in forkedRepos)
const allCommitUrls = filteredArray.map(repo => repo.commits_url)
allCommitUrls.forEach(commitAPI => {
//the URLs end with {/sha}, therefore the last 6 chars need to be sliced
commitAPI = commitAPI.slice(0, -6)
//now the URLs can be passed on to get data about number and content of commits
fetch(commitAPI, /* options */)
.then((res) => res.json())
.then((data) => {
const authorCommits = data.filter(commits => commits.author.login === 'nehrwein' && commits.url.includes(myRepoName))
if (authorCommits.length > 0) {
document.getElementById(`commit-${myRepoName}`).innerHTML = `
Commits: ${authorCommits.length}<i class="fas fa-bars"></i>
`
authorCommits.forEach(element => {
document.getElementById(`commitMessages-${myRepoName}`).innerHTML += `
<li>${element.commit.message}</li>
`
})
}
})
});
}
getRepos();