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
215 lines (184 loc) · 7.58 KB
/
script.js
File metadata and controls
215 lines (184 loc) · 7.58 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
//Global DOM Selectors
const header = document.querySelector('header')
const projects = document.getElementById('projects')
// Github API
const username = 'michaelchangdk'
const GITHUB_API = `https://api.github.com/users/${username}/repos`
// GITHUB Authentication
const options = {
method: 'GET',
headers: {
Authorization: `token ${API_KEY}`
}
}
// Function to Sort Projects By Created Date
compareCreateDate = (a, b) => {
if (a.created_at < b.created_at) {
return -1;
} if (a.created_at > b.created_at) {
return 1;
}
return 0;
};
// Function for opening and closing the projects items
const openSesame = (projectID) => {
let projectHeader = document.getElementById(projectID);
let projectBody = projectHeader.nextElementSibling;
if (getComputedStyle(projectBody).display === "none") {
projectBody.style.display = "block";
projectHeader.classList.add("project-header--active")
} else {
projectBody.style.display = "none";
projectHeader.classList.remove("project-header--active")
}
}
const getProjects = async () => {
const projectsWait = await fetch(GITHUB_API, options);
const data = await projectsWait.json();
// Filter Technigo Projects Only & Sort by Date Created
const technigoProjects = data.filter(item => item.archive_url.includes('project') === true).sort(compareCreateDate);
// Length of completed projects and sending the number to chart.js
const completedNumber = data.filter(item => item.archive_url.includes('project') === true).length
completedProjects(completedNumber)
// Create Header Section
header.innerHTML = `
<img src="${technigoProjects[0].owner.avatar_url}" class="avatar" alt="profile picture" />
<h1>${technigoProjects[0].owner.login}</h1>
`
// For Loop to Create Main Project Elements || & Fetch Number of Commits as well as Pull Requests
for (let i = 0; i < technigoProjects.length; i++) {
// Creating Project classname & project ID
let projectName = technigoProjects[i].name.replaceAll("-","");
let projectID = technigoProjects[i].id
// Create DIV in the correct sorted order
projects.innerHTML += `
<div id="${technigoProjects[i].id}" class="project-wrapper">
</div>
`
// Creating the Project Elements
document.getElementById(projectID).innerHTML += `
<button class="project-header ${projectName}" id="${projectName}" onclick="openSesame('${projectName}')">
Week ${i+2}: ${technigoProjects[i].name}
</button>
<div class="project-info" id="${projectID}2">
<p>Repo can be found <a href="${technigoProjects[i].html_url}" target="_blank">here</a>.</p>
</div>
`
// Fetching Commit Data
commitFetch(technigoProjects[i], projectID);
// Fetching Pull Requests
pullFetch(technigoProjects[i].name, projectID)
}
}
// Function for Fetching Commits and Live Link
const commitFetch = (projects, projectsID) => {
const GIT_COMMIT_API = projects.commits_url.replace("{/sha}", "") + "?per_page=100"
fetch(GIT_COMMIT_API, options)
.then(res => res.json())
.then(data => {
const filteredCommits = []
for (let i = 0; i < data.length; i++) {
let author = data[i].commit.author.name
if (author.includes("Chang") === true) {
filteredCommits.push('1')
}
};
// Defining number of commits
let numberOfCommits = filteredCommits.length;
// Last Commit Message - Committed Netlify Link to add to live link.
let lastCommitLink = data[0].commit.message
// Formatting the DATES
// Last Commit Date which is NOT the Netlify commit (Index 1 instead of 0)
// Formatting DAYS
let lastCommitDateRaw = data[1].commit.committer.date.substring(0,10)
// Removing the 0 from dates before the 10th for DAYS
let lastCommitDayRaw = lastCommitDateRaw.substring(8,10)
let lastCommitDay
if (lastCommitDayRaw < 10) {
lastCommitDay = lastCommitDayRaw.replace("0", "")
} else {
lastCommitDay = lastCommitDayRaw;
}
// Formatting MONTHS
const months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
let lastCommitMonthNumber
if (lastCommitDateRaw.substring(5,7) !== 10) {
lastCommitMonthNumber = lastCommitDateRaw.substring(5,7).replace("0","")
} else {
lastCommitMonthNumber = lastCommitDateRaw.substring(5,7)
}
let lastCommitMonth = months[lastCommitMonthNumber - 1]
// Extracting YEAR
let lastCommitYear = lastCommitDateRaw.substring(0,4)
let commitDate = `${lastCommitMonth} ${lastCommitDay}, ${lastCommitYear}`
// Creating the Project Elements
document.getElementById(`${projectsID}2`).innerHTML += `
<p>Number of commits: ${numberOfCommits}</p>
<p>Last commit date: ${commitDate}</p>
`
// If statement to check if last commit includes netlify link
if (lastCommitLink.includes('https://') === true) {
document.getElementById(`${projectsID}2`).innerHTML += `
<p>View it live <a href="${lastCommitLink}" target="_blank">here</a>.
`
} else {
document.getElementById(`${projectsID}2`).innerHTML += `
<p>No live link available.</p>
`
}
})
}
// Fetching the pull requests
const pullFetch = (projectName, projectsID) => {
const GIT_FETCH_API = `https://api.github.com/repos/technigo/${projectName}/pulls?per_page=100`
fetch(GIT_FETCH_API, options)
.then(res => res.json())
.then(data => {
// Filter Pull Requests by my username
const filteredPR = data.filter(function (pr) {
return pr.user.login === username
})
// Check if there is a pull request with my username
let exist = false
for (let i = 0; i < data.length; i++) {
if (data[i].user.login === username) {
exist = true
}
}
// Conditional for pull request innerHTML
if (exist === true) {
document.getElementById(`${projectsID}2`).innerHTML += `
<p>Pull request with comments <a href="${filteredPR[0].html_url}" target="_blank">here</a>.</p>
`
} if (exist === false) {
document.getElementById(`${projectsID}2`).innerHTML += `
<p>Pull request unavailable.</p>
`
}
// Fetch Comments - Commented out because I didn't want them, but left it here to show how to do it
// pullComments(filteredPR, projectsID);
})
}
// Fetch Comments - Commented out because I didn't want them, but left it here to show how to do it
// const pullComments = (pullRequests, projectsID) => {
// for (let i = 0; i < pullRequests.length; i++) {
// fetch(pullRequests[0].review_comments_url, options)
// .then(res => res.json())
// .then(data => {
// document.getElementById(`${projectsID}2`).innerHTML += `
// <br>
// <p>Review comments:</p>
// `
// for (let i = 0; i < data.length; i++) {
// if (data[i].user.login !== username) {
// document.getElementById(`${projectsID}2`).innerHTML += `
// <ul>
// <li>${data[i].body}</li>
// </ul>
// `
// }
// }
// })
// }
// }
getProjects();