forked from hnasr/javascript_playground
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
190 lines (144 loc) · 5.77 KB
/
index.html
File metadata and controls
190 lines (144 loc) · 5.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
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>github</title>
</head>
<body>
<h1>Github tutorial</h1>
<button id = 'btnRepos'>Repos</button>
<button id = 'btnIssues'>Issues</button>
<button id = 'btnCommits'>Commits</button>
<button id = 'btnIssuesPrivate'>Issues Private</button>
<button id = 'btnCreateIssue'>Create Issue</button>
<div id = 'divResult'>
</div>
<script>
const btnRepos = document.getElementById("btnRepos")
const btnIssues = document.getElementById("btnIssues")
const btnIssuesPrivate = document.getElementById("btnIssuesPrivate")
const btnCreateIssue = document.getElementById("btnCreateIssue")
const btnCommits = document.getElementById("btnCommits")
const divResult = document.getElementById("divResult")
btnRepos.addEventListener("click", getRepos)
btnIssues.addEventListener("click", getIssues)
btnCommits.addEventListener("click", e=> getCommits())
btnIssuesPrivate.addEventListener("click", e=> getIssuesPrivate())
btnCreateIssue.addEventListener("click", e=> createIssues())
async function getRepos() {
clear();
const url = "https://api.github.com/search/repositories?q=stars:150000..300000"
const response = await fetch(url)
const result = await response.json()
result.items.forEach(i=>{
const anchor = document.createElement("a")
anchor.href = i.html_url;
anchor.textContent = i.full_name;
divResult.appendChild(anchor)
divResult.appendChild(document.createElement("br"))
})
}
async function createIssues() {
clear();
const url = "https://api.github.com/repos/husseintest/sandboxpublic/issues"
const token = "YOUR_TOKEN_HERE";
const headers = {
"Authorization" : `Token ${token}`
}
const payLoad = {
title: "Hey my brand new issue!!!"
}
const response = await fetch(url, {
method: "POST",
headers: headers,
body: JSON.stringify(payLoad)
})
const result = await response.json()
const i = result;
const anchor = document.createElement("a")
anchor.href = i.html_url;
anchor.textContent = i.title;
divResult.appendChild(anchor)
divResult.appendChild(document.createElement("br"))
}
async function getIssues() {
clear();
const url = "https://api.github.com/search/issues?q=author:raisedadead repo:freecodecamp/freecodecamp type:issue"
const response = await fetch(url)
const result = await response.json()
result.items.forEach(i=>{
const anchor = document.createElement("a")
anchor.href = i.html_url;
anchor.textContent = i.title;
divResult.appendChild(anchor)
divResult.appendChild(document.createElement("br"))
})
}
async function getIssuesPrivate() {
clear();
const username = "YOUR_USER_NAME"
const password = "YOUR_PASSWORD"
const headers = {
"Authorization" : `Basic ${btoa(`${username}:${password}`)}`
}
const url = "https://api.github.com/search/issues?q=repo:husseintest/sandboxprivate type:issue"
const response = await fetch(url, {
"method": "GET",
"headers": headers
})
const result = await response.json()
result.items.forEach(i=>{
const anchor = document.createElement("a")
anchor.href = i.html_url;
anchor.textContent = i.title;
divResult.appendChild(anchor)
divResult.appendChild(document.createElement("br"))
})
}
async function getCommits(url="https://api.github.com/search/commits?q=repo:freecodecamp/freecodecamp author-date:2019-03-01..2019-03-31") {
clear();
const headers = {
"Accept" : "application/vnd.github.cloak-preview"
}
const response = await fetch(url, {
"method" : "GET",
"headers" : headers
})
//"<https://api.github.com/search/commits?q=repo%3Afreecodecamp%2Ffreecodecamp+author-date%3A2019-03-01..2019-03-31&page=2>; rel="next", <https://api.github.com/search/commits?q=repo%3Afreecodecamp%2Ffreecodecamp+author-date%3A2019-03-01..2019-03-31&page=27>; rel="last""
const link = response.headers.get("link")
const links = link.split(",")
const urls = links.map(a=> {
return {
url: a.split(";")[0].replace(">","").replace("<",""),
title:a.split(";")[1]
}
})
const result = await response.json()
result.items.forEach(i=>{
const img = document.createElement("img")
img.src = i.author.avatar_url;
img.style.width="32px"
img.style.height="32px"
const anchor = document.createElement("a")
anchor.href = i.html_url;
anchor.textContent = i.commit.message.substr(0,120) + "...";
divResult.appendChild(img)
divResult.appendChild(anchor)
divResult.appendChild(document.createElement("br"))
})
urls.forEach(u => {
const btn = document.createElement("button")
btn.textContent = u.title;
btn.addEventListener("click", e=> getCommits(u.url))
divResult.appendChild(btn);
})
}
function clear(){
while(divResult.firstChild)
divResult.removeChild(divResult.firstChild)
}
</script>
</body>
</html>