-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.html
382 lines (305 loc) · 11.7 KB
/
index.html
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<!-- Open Graph meta tags -->
<meta property="og:title" content="Random Issue Picker" />
<meta property="og:description" content="Pick a random (skill) GitHub issue XD" />
<meta property="og:url" content="https://rip.tobycm.dev/" />
<meta property="og:site_name" content="Random Issue Picker" />
<link rel="stylesheet" href="index.css" />
<link rel="stylesheet" id="theme" href="light.css" />
<link rel="preload" href="dark.css" as="style" />
<title>Random Issue Picker</title>
</head>
<body>
<script>
/**
* Sets the theme of the page. If no theme is provided, it will toggle between light and dark.
*
* @param {string | undefined} theme The theme to set.
*
* @returns {void}
*/
function setTheme(theme) {
/** @type {HTMLLinkElement} */
const css = document.getElementById("theme");
if (!theme) theme = css.href.endsWith("light.css") ? "dark.css" : "light.css";
css.href = theme;
localStorage.setItem("theme", theme);
document.getElementById("themeButton").textContent = theme === "light.css" ? "🔆" : "🌙";
}
</script>
<div style="float: right">
<button onclick="setTheme()" id="themeButton">🔆</button>
</div>
<script>
let theme = localStorage.getItem("theme");
const prefersDarkScheme = window.matchMedia("(prefers-color-scheme: dark)");
if (!theme && prefersDarkScheme.matches) theme = "dark.css";
setTheme(theme);
</script>
<h1>Random Issue Picker</h1>
<hr />
<p>Current open issues: <a id="issues">...</a></p>
<script>
/**
* @typedef {{
* name: string | null,
* issues: number
* pull_requests: number
* }} RepoInfo
*/
/** @type {RepoInfo} */
var repoInfo = {
name: null,
issues: -1,
pull_requests: 0,
};
/**
* Parses the URL of a GitHub repository.
*
* @param {string} url The URL of the GitHub repository.
* @returns {string | null} The name of the repository.
*/
function parseUrl(url) {
url = url.trim();
// Remove trailing '/issues' if present
// url = url.replace(/\/issues\/?$/, "");
// new regex that matches repo even with .git, /issues, /branch/main etc. at the end
let name = url.match(/(?:git@|https:\/\/)github.com[:\/]([^\/\s]+\/[^\/\s]+?)(?:\.git)?(?=\/|$)/);
if (!name) return null;
name = name[1].replace(/\.git$/, "");
if (name.endsWith("/")) name = name.slice(0, -1);
return name;
}
/**
* Parses the number of pages from the 'Link' header of a GitHub API response.
*
* @param {string | null} link The 'Link' header of a GitHub API response.
* @returns {number} The number of pages in the response.
*/
function parsePageCount(link) {
if (!link) return 1;
const match = link.match(/page=(\d+)>; rel="last"/);
return match ? parseInt(match[1], 10) : 1;
}
/**
* Fetches the number of open issues in a GitHub repository.
*
* @param {RepoInfo} repoInfo The information about the repository.
*
* @returns {Promise<number>} The number of open issues in the repository.
*/
async function fetchIssuesCount(repoInfo) {
if (repoInfo.name === null) return -1;
try {
const response = await fetch(`https://api.github.com/repos/${repoInfo.name}/issues?per_page=1`);
if (!response.ok) return -1;
if ((await response.json()).length === 0) return 0;
return parsePageCount(response.headers.get("Link"));
} catch {
return -1;
}
}
/**
* Fetches the number of open pull requests in a GitHub repository.
*
* @param {RepoInfo} repoInfo The information about the repository.
*
* @returns {Promise<number>} The number of open pull requests in the repository.
*/
async function fetchPullRequestsCount(repoInfo) {
if (repoInfo.name === null) return -1;
try {
const response = await fetch(`https://api.github.com/repos/${repoInfo.name}/pulls?per_page=1`);
if (!response.ok) return -1;
if ((await response.json()).length === 0) return 0;
return parsePageCount(response.headers.get("Link"));
} catch {
return -1;
}
}
/**
* Github API response for an issue.
*
* @typedef {{
* number: number,
* title: string,
* user: {
* login: string,
* avatar_url: string,
* html_url: string
* },
* html_url: string
* pull_request?: Record<string, unknown>
* }} Issue
*/
/**
* Fetches a random issue from a GitHub repository.
*
* @returns {Promise<Issue | null>} The random issue url.
*/
async function randomIssue() {
if (repoInfo.issues === -1) return null;
let issues = localStorage.getItem("issues");
if (issues) issues = JSON.parse(issues).map((issue) => issue.number);
else issues = [];
try {
const randomPage = Math.floor(Math.random() * repoInfo.issues) + 1;
let response = await fetch(`https://api.github.com/repos/${repoInfo.name}/issues?per_page=1&page=${randomPage}`);
if (!response.ok) return null;
/** @type {Issue} */
let issue = (await response.json())[0];
while (issues.includes(issue.number) || issue.pull_request) {
const retryPage = Math.floor(Math.random() * repoInfo.issues) + 1;
response = await fetch(`https://api.github.com/repos/${repoInfo.name}/issues?per_page=1&page=${retryPage}`);
if (!response.ok) return null;
issue = (await response.json())[0];
}
return issue;
} catch {
return null;
}
}
</script>
<script>
async function onUrlInput() {
const name = parseUrl(document.getElementById("url").value);
if (repoInfo.name === name) return;
repoInfo.name = name;
repoInfo.issues = await fetchIssuesCount(repoInfo);
repoInfo.pull_requests = await fetchPullRequestsCount(repoInfo);
document.getElementById("issues").textContent = repoInfo.issues === -1 ? "..." : repoInfo.issues - repoInfo.pull_requests;
}
</script>
<label for="url">Repo URL:</label>
<input
type="text"
id="url"
name="url"
style="width: 100%; max-width: 40rem"
onkeypress="this.onchange();"
onpaste="this.onchange();"
oninput="this.onchange();"
onchange="onUrlInput()" />
<script>
const url = document.getElementById("url").value;
if (url) onUrlInput();
</script>
<script>
/**
* Delete an issue from the history list.
*
* @param {number} issueNumber The number of the issue to delete.
*
* @returns {void}
*/
function deleteIssue(issueNumber) {
const issues = JSON.parse(localStorage.getItem("issues")) || [];
const newIssues = issues.filter((issue) => issue.number !== issueNumber);
localStorage.setItem("issues", JSON.stringify(newIssues));
// refresh
const history = document.getElementById("history");
history.innerHTML = "";
newIssues.forEach((issue) => makeIssueEntry(issue));
}
/**
* Creates an entry in the history list for an issue.
*
* @param {Issue} issue The issue to create an entry for.
*
* @returns {void}
*/
function makeIssueEntry(issue) {
const history = document.getElementById("history");
const issueEntry = document.createElement("li");
issueEntry.innerHTML =
`Issue <a href="${issue.html_url}" target="_blank">#${issue.number}</a>: ${issue.title}` +
"<br />" +
'<div class="bottom" style="margin-top: 1vh">' +
`Opened by <a class="bottom" href="${issue.user.html_url}" target="_blank">` +
`<img class="avatar" src="${issue.user.avatar_url}" alt="${issue.user.login}'s avatar" />` +
` ${issue.user.login}</a>` +
"</div>" +
`<button style="margin-top: 1vh" onClick="deleteIssue(${issue.number})">Delete</button>`;
history.insertBefore(issueEntry, history.firstChild);
}
async function openRandomIssue() {
const issue = await randomIssue();
if (!issue) return;
const issues = JSON.parse(localStorage.getItem("issues")) || [];
issues.push(issue);
localStorage.setItem("issues", JSON.stringify(issues));
if (localStorage.getItem("openInNewTab") === "true") window.open(issue.html_url, "_blank");
makeIssueEntry(issue);
}
</script>
<br />
<div style="display: flex; align-items: center; margin-top: 2vh">
<button onclick="openRandomIssue()">Pick random issue</button>
<input style="margin-left: 2rem" type="checkbox" id="openInNewTab" onchange="localStorage.setItem('openInNewTab', this.checked)" />
<label style="margin-left: 1rem" for="openInNewTab">Open in new tab</label>
</div>
<script>
if (localStorage.getItem("openInNewTab") === "true") document.getElementById("openInNewTab").checked = true;
</script>
<br />
<div id="exampleUrls">
<p style="margin-bottom: 1vh">Example URLs:</p>
<ul style="margin-top: 0">
<li>https://github.com/oven-sh/bun</li>
<li>https://github.com/oven-sh/bun/issues/</li>
<li>https://github.com/tobycm/akatsuki-du-ca-bot-remastered.git</li>
<li>[email protected]:pdt1806/discord-status-as-image.git</li>
<li>https://github.com/elysiajs/elysia/tree/main</li>
<li>https://github.com/esaruoho/org.lackluster.Paketti.xrnx/actually/you_know-what/</li>
</ul>
</div>
<hr />
<p style="margin-bottom: 1vh">History:</p>
<ul style="margin-top: 0" id="history"></ul>
<script>
const issues = JSON.parse(localStorage.getItem("issues"));
if (issues) issues.forEach((issue) => makeIssueEntry(issue));
</script>
<hr />
<footer>
<div class="bottom">
Made by
<a class="bottom" href="https://github.com/tobycm"
><img class="avatar" src="https://avatars.githubusercontent.com/u/62174797" alt="tobycm's avatar" /> tobycm</a
>
with ❤
</div>
| Copyright © <span id="year">2024</span>
<br />
| GitHub: <a href="https://github.com/tobycm/random-issue-picker">tobycm/random-issue-picker</a>
</footer>
<script>
document.getElementById("year").textContent = new Date().getFullYear();
</script>
<!-- New Scripts for URL Parameter Handling -->
<script>
/**
* Jobs:
* 1. Extracts the repository URL if `url` param is set.
* 2. Automatically fetches a random issue `fetch` param is present.
* 3. Opens the issue in a new tab if the 'open' param is set.
*/
async function handleUrlQuery() {
const urlParams = new URLSearchParams(window.location.search);
const repo = urlParams.get("url");
if (!repo) return;
document.getElementById("url").value = repo;
await onUrlInput();
if (!urlParams.has("fetch")) return;
if (urlParams.has("open")) localStorage.setItem("openInNewTab", "true");
openRandomIssue();
}
// Call the function on page load
handleUrlQuery();
</script>
</body>
</html>