-
Notifications
You must be signed in to change notification settings - Fork 131
Expand file tree
/
Copy pathscript.js
More file actions
183 lines (138 loc) · 6.44 KB
/
script.js
File metadata and controls
183 lines (138 loc) · 6.44 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
// DOM selectors
const userInfo = document.getElementById('user-info')
const projectBoard = document.getElementById('projects')
// Global variables
const username = 'tiiliu'
// Api for fetching github repos
const API_URL = `https://api.github.com/users/${username}/repos`
// Github token
const TOKEN = API_TOKEN
// Authorization
const options = {
method: 'GET',
headers: {
Authorization: `token ${TOKEN}`,
}
}
// Function for fetching all github projects
const getGithubProjects = () => {
//-------------------------------FETCH 1------------------------------------------------
fetch(API_URL, options)
.then(res => res.json())
.then(data => {
// Invoking the createUser and getTechnigoRepos functions
createUser(data)
getTechnigoRepos(data)
})
.catch(err => console.error(err))
}
// Function to display info from user
const createUser = (data) => {
userInfo.innerHTML = `
<p><img class='user-image' src='${data[0].owner.avatar_url}' alt='profile picture from Github'/></p>
<h1 class='full-name'>Tiina Liukkonen</h1>
<h3 class='github-user-name'><a class='nav-item' href='https://github.com/tiiliu' target="-blank"><i class="fab fa-github"></i> ${data[0].owner.login}</a></h3>
<h1 class='course-name'>Technigo Web Development Class of Jan '22 student</h1>
`
}
// Function to filter out all repos forked from Technigo
const getTechnigoRepos = (data) => {
// variable to store the filtered repos
const forkedProjects = data.filter(repo => repo.name.includes('project-'))
console.log('forked projects:', forkedProjects)
// Invoking the drawChart function
drawChart(forkedProjects.length)
// Looping through the filtered repos
forkedProjects.forEach(repo => {
// storing reponame for each repo
let reponame = repo.name
reponame = reponame[0].toUpperCase() + reponame.substring(1)
//storing the latest update for each repo
let recentUpdate = new Date(repo.pushed_at).toLocaleDateString('en', {day: 'numeric', month: 'short', year: 'numeric'})
// storing the default branch for each repo
let defaultBranch = repo.default_branch
// storing the link for each repo
let linkToRepo = repo.html_url
// Printing the info about projects
projectBoard.innerHTML += `
<div class='repo-container'>
<h2 class='repo-title'>${reponame}</h2>
<p>Default branch: ${defaultBranch}</p>
<p id='commit-${repo.name}'></p>
<p>Most recent push: ${recentUpdate}</p>
<button class='repo-link-button'><a class='nav-item' href='${repo.html_url}' target="-blank">Click here to view repo</a></button>
`
//-------------------------------FETCH 2------------------------------------------------
fetch(`https://api.github.com/repos/Technigo/${reponame}/pulls?per_page=100`, options)
.then(res => res.json())
.then(data => {
// filtering own pull requests
const ownPullRequests = data.filter(repo => repo.user.login === username)
// filtering pull requests done by a team member
const otherPullRequests = data.filter(item => item.user.login === 'kolkri' && item.base.repo.name === 'project-weather-app')
// combining the created arrays
const combinedPullRequests = ownPullRequests.concat(otherPullRequests)
// looping through own pull requests
combinedPullRequests.forEach(repo => {
// storing the reponame of each pull request
reponame = repo.base.repo.name
// storing the url for commits for each repo
const commitsForRepos = repo.commits_url
// Invoking the fetchCommits function
fetchCommits(commitsForRepos, reponame)
// storing the url for comments for each repo -> commented out because not used yet in code
//const commentsForRepos = repo.comments_url
// storing the pull request number for each repo
const pullRequestNumber = repo.number
// Invoking the fetchComments function
fetchComments(pullRequestNumber, reponame)
// storing the url for languages
const usedLanguages = repo.base.repo.languages_url
// Invoking the getLanguages function
getLanguages(usedLanguages, reponame)
})
})
})
}
// Function for fetching the commits for all repos
const fetchCommits = (commitsForRepos, reponame) => {
//-------------------------------FETCH 3------------------------------------------------
fetch(commitsForRepos, options)
.then(res => res.json())
.then(data => {
// storing the number of commits
const numberOfCommits = data.length
// printing info to the projectBoard with dynamic id
document.getElementById(`commit-${reponame}`).innerHTML += `
<p>Amount of commits: ${numberOfCommits}</p>
`
// looping through data for commit messages
data.forEach(item => {
// storing the commit message from each
const commitMessages = item.commit.message
})
})
.catch(err => console.error(err))
}
// Function for fetching the comments for each pull request
const fetchComments = (pullRequestNumber, reponame) => {
//-------------------------------FETCH 4------------------------------------------------
fetch(`https://api.github.com/repos/Technigo/${reponame}/issues/${pullRequestNumber}/comments`, options)
.then(res => res.json())
.then(data => {
// storing the review message -> commented out because not used yet in the code
//const reviewMessage = data[0].body
})
.catch(err => console.error(err))
}
// Function for fetching the languages used in projects
const getLanguages = (usedLanguages, reponame) => {
fetch(usedLanguages, options)
.then(res => res.json())
.then(data => {
// storing the info about used languages
const languages = Object.keys(data)
})
}
// Invoking the getGithubProjects function
getGithubProjects()