-
Notifications
You must be signed in to change notification settings - Fork 131
Expand file tree
/
Copy pathscript.js
More file actions
172 lines (145 loc) · 5.63 KB
/
script.js
File metadata and controls
172 lines (145 loc) · 5.63 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
const userSection = document.getElementById('user');
const projectSection = document.getElementById('projects');
const username = 'jessand77';
const API_USER_INFO = `https://api.github.com/users/${username}`;
const API_REPOS = `${API_USER_INFO}/repos`;
const options = {
method: 'GET',
headers: {
Authorization: 'token ' + API_TOKEN,
},
};
const fetchUserInfo = () => {
fetch(API_USER_INFO)
.then((res) => res.json())
.then((user) => {
userSection.innerHTML = `
<h3>${user.name}</h3>
<p>Username: ${user.login}</p>
<a href="${user.html_url}" target="_blank"><img class="avatar" src="${user.avatar_url}" alt="profile picture" /></a>
`;
});
};
const getCommits = (pullRequestCommitsUrl, reponame) => {
fetch(pullRequestCommitsUrl)
.then((res) => res.json())
.then((commits) => {
let repoDiv = document.getElementById(`${reponame}-div`);
let repoDivContent = document.getElementById(`${reponame}-div-content`);
let commitParagraph = document.createElement('p');
commitParagraph.innerHTML = `Number of commits: <span>${commits.length}</span>`;
repoDivContent.appendChild(commitParagraph);
// Create a new div to put the commit messages and the hide commits button in
let commitDiv = document.createElement('div');
commitDiv.className = 'commit-div';
commitDiv.style.display = 'none';
repoDiv.appendChild(commitDiv);
let commitMessageList = document.createElement('ol');
commitMessageList.className = 'message-list';
commitDiv.appendChild(commitMessageList);
commits.forEach((commit) => {
let commitMessage = document.createElement('li');
commitMessage.innerHTML = commit.commit.message;
commitMessageList.appendChild(commitMessage);
});
// Buttons to show or hide commit comments
let showCommitsButton = document.createElement('button');
showCommitsButton.className = 'commits-button';
showCommitsButton.innerHTML = 'Show commits';
repoDivContent.appendChild(showCommitsButton);
let hideCommitsButton = document.createElement('button');
hideCommitsButton.className = 'commits-button';
hideCommitsButton.innerHTML = 'Hide commits';
commitDiv.appendChild(hideCommitsButton);
const showCommitMessageList = () => {
repoDivContent.style.display = 'none';
commitDiv.style.display = 'block';
};
const hideCommitMessageList = () => {
commitDiv.style.display = 'none';
repoDivContent.style.display = 'flex';
};
showCommitsButton.addEventListener('click', showCommitMessageList);
hideCommitsButton.addEventListener('click', hideCommitMessageList);
});
};
const getPullRequests = (repos) => {
repos.forEach((repo) => {
fetch(
`https://api.github.com/repos/Technigo/${repo.name}/pulls?per_page=100`
)
.then((res) => res.json())
.then((pullRequests) => {
// How can I get the pull requests for the repos that I am not the owner of without hardcoding like this?
const firstTitleToSearchFor =
'Week 4 project - Sofie Pellegrini & Jessica Sandler';
const secondTitleToSearchFor =
'Week 6 project : Jessica - Maurii - Nadia - Rijad - Terese';
const thirdTitleToSearchFor =
'Week 9: Jessica Sandler, Laura Sjölander and Nadia Lefebvre';
const fourthTitleToSearchFor =
'Project Elephant Quiz made by Sofie Pellegrini, Lisa Bergström, Jessica Sandler, Terese Claesson and Emma Berg';
const fifthTitleToSearchFor = 'project-auth Lisa & Jessica';
const myPullRequests = pullRequests.filter(
(pullRequest) =>
pullRequest.user.login === username ||
pullRequest.title === firstTitleToSearchFor ||
pullRequest.title === secondTitleToSearchFor ||
pullRequest.title === thirdTitleToSearchFor ||
pullRequest.title === fourthTitleToSearchFor ||
pullRequest.title === fifthTitleToSearchFor
);
//If the length is 0 no pull request has been made
if (myPullRequests.length === 0) {
let repoDivContent = document.getElementById(
`${repo.name}-div-content`
);
let commitParagraph = document.createElement('p');
commitParagraph.innerHTML = `<span>No pull request found</span>`;
commitParagraph.className = 'no-pullrequest';
repoDivContent.appendChild(commitParagraph);
}
//Fetches the commits for all the projects where a pull request has been made
myPullRequests.forEach((myPullRequest) => {
getCommits(myPullRequest.commits_url, repo.name);
});
});
});
};
// This function filters out repos that are forked and start with "project"
const getTechnigoRepos = (repos) => {
return repos.filter(
(repo) => repo.fork === true && repo.name.startsWith('project')
);
};
const fetchRepos = () => {
fetch(API_REPOS)
.then((res) => res.json())
.then((repos) => {
const technigoRepos = getTechnigoRepos(repos);
getPullRequests(technigoRepos);
projectSection.innerHTML = `
<h2>My Technigo projects:</h2>
<div class="repo-container" id="repoContainer"></div>
`;
// Get the number of forked Technigo projects and draw the chart
const numberOfTechnigoRepos = technigoRepos.length;
drawChart(numberOfTechnigoRepos);
technigoRepos.forEach((repo) => {
const mostRecentPush = new Date(repo.pushed_at).toLocaleDateString(
'en-GB'
);
repoContainer.innerHTML += `
<div id=${repo.name}-div class="repo-div">
<div id=${repo.name}-div-content class="repo-div-content">
<h4><a href="${repo.html_url}" target="_blank">${repo.name}</a></h4>
<p>Most recent push: <span>${mostRecentPush}</span></p>
<p>Default branch: <span>${repo.default_branch}</span></p>
</div>
</div>
`;
});
});
};
fetchUserInfo();
fetchRepos();