forked from ichard26/next-pr-number
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
462 lines (411 loc) · 13.4 KB
/
Copy pathmain.js
File metadata and controls
462 lines (411 loc) · 13.4 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
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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
const defaultTitle = "Git Author Info";
const usernameInput = document.querySelector(".username-input");
const getButton = document.querySelector(".get-button");
const workingStatusText = document.querySelector(".working-status-text");
const resultText = document.querySelector(".result-text");
// GitHub usernames: alphanumeric and hyphens, no leading hyphen, max 39 chars, allow trailing hyphens.
// https://github.com/shinnn/github-username-regex/blob/0794566cc10e8c5a0e562823f8f8e99fa044e5f4/index.js#L1C16-L1C58
// https://github.com/shinnn/github-username-regex/pull/5
const validUsernameRegex = /^[a-z\d](?:[a-z\d]|-(?!-)){0,38}$/i;
let working;
const GITHUB_API = "https://api.github.com";
const CACHE_TTL_SECONDS = 3600;
if (typeof window !== "undefined" && window.ls) {
window.ls.config.ttl = CACHE_TTL_SECONDS;
}
/**
* Get the cached data for a key.
* @param {string} key - Cache key
* @returns {*} - Cached value, or null if not found/expired
*/
function getCached(key) {
try {
if (!window.ls) return null;
return window.ls.get(key);
} catch (err) {
console.error("Cache read error:", err);
return null;
}
}
/**
* Set cached data with TTL.
* @param {string} key - Cache key
* @param {*} value - Value to cache
*/
function setCached(key, value) {
try {
if (!window.ls) return;
window.ls.set(key, value, { ttl: CACHE_TTL_SECONDS });
} catch (err) {
console.error("Cache write error:", err);
}
}
function sanitizeString(string) {
const map = {
"&": "&",
"<": "<",
">": ">",
'"': """,
"'": "'",
"/": "/",
};
const reg = /[&<>"'/]/gi;
return string.replace(reg, (match) => map[match]);
}
class HTTPError extends Error {
constructor(code, message) {
super(message);
this.name = "HTTPError";
this.status = code;
}
}
async function getUserInfo(username) {
const cacheKey = `github_user_${username}`;
const cached = getCached(cacheKey);
if (cached) {
console.log(`Using cached user info for ${username}`);
return cached;
}
const response = await fetch(`${GITHUB_API}/users/${username}`);
const data = await response.json();
if (!response.ok) {
console.error("HTTPError", response);
throw new HTTPError(response.status, data.message || "Unknown error");
}
setCached(cacheKey, data);
console.log(`Cached user info for ${username}`);
return data;
}
/**
* Get the email addresses associated with the user's commits. We employ two strategies:
* 1. Check the user's public events for commit data.
* 2. Check the user's repositories for commits authored by them.
* @param {string} username - The GitHub username.
* @returns {Promise<string[]>} - Array of unique email addresses (real ones, not noreply ones).
*/
async function getCommitEmail(username) {
const cacheKey = `github_emails_${username}`;
const cached = getCached(cacheKey);
if (cached) {
console.log(`Using cached commit emails for ${username}`);
return cached;
}
const emails = new Set();
try {
const response = await fetch(
`${GITHUB_API}/users/${username}/events/public`
);
if (response.ok) {
const events = await response.json();
for (const event of events) {
if (event.type === "PushEvent" && event.payload.commits) {
for (const commit of event.payload.commits) {
if (commit.author && commit.author.email) {
const email = commit.author.email;
if (!email.includes("noreply.github.com")) {
emails.add(email);
}
}
}
}
}
}
} catch (err) {
console.error("Failed to fetch commit email from events:", err);
}
// Try non-forked repos first as they're more likely to have the user's own commits.
// If no email is found, we will check the user's forked repos. We'll check up to ten
// repos in total to avoid excessive API calls.
try {
const reposResponse = await fetch(
`${GITHUB_API}/users/${username}/repos?sort=updated&per_page=30`
);
if (reposResponse.ok) {
const repos = await reposResponse.json();
const nonForkedRepos = repos.filter((repo) => !repo.fork);
const reposToCheck = nonForkedRepos.length > 0 ? nonForkedRepos : repos;
for (const repo of reposToCheck.slice(0, 10)) {
try {
const commitsResponse = await fetch(
`${GITHUB_API}/repos/${repo.owner.login}/${repo.name}/commits?author=${username}&per_page=10`
);
if (!commitsResponse.ok) continue;
const commits = await commitsResponse.json();
for (const commitData of commits) {
if (
commitData.commit &&
commitData.commit.author &&
commitData.commit.author.email
) {
const email = commitData.commit.author.email;
if (!email.includes("noreply.github.com")) {
emails.add(email);
}
}
}
} catch (err) {
console.error(`Failed to fetch commits from ${repo.full_name}:`, err);
continue;
}
}
}
} catch (err) {
console.error("Failed to fetch repos:", err);
}
const emailsArray = Array.from(emails);
setCached(cacheKey, emailsArray);
console.log(`Cached commit emails for ${username}`);
return emailsArray;
}
/**
* Extract the username from a GitHub URL, or return the input as-is, if it's already one.
* We can handle URLs like the following:
* - https://github.com/username
* - http://github.com/username
* - github.com/username
* - https://github.com/username/repo
* @param {string} input - The user input (URL or username).
* @returns {string} - The extracted username.
*/
function extractUsername(input) {
const trimmedInput = input.trim();
if (trimmedInput.includes("github.com")) {
try {
let url;
if (
trimmedInput.startsWith("http://") ||
trimmedInput.startsWith("https://")
) {
url = new URL(trimmedInput);
} else {
url = new URL("https://" + trimmedInput);
}
const pathSegments = url.pathname
.split("/")
.filter((segment) => segment.length > 0);
if (pathSegments.length > 0) {
return pathSegments[0];
}
} catch (err) {
console.error("Failed to parse URL:", err);
}
}
return trimmedInput;
}
function checkInputValidity() {
const username = extractUsername(usernameInput.value);
if (!validUsernameRegex.test(username)) {
let msg;
if (!usernameInput.value.length) {
msg = "Please enter a GitHub username or URL";
} else {
msg = "Invalid username or URL";
}
usernameInput.setCustomValidity(msg);
usernameInput.reportValidity();
return false;
}
return true;
}
function setWorkingStatus() {
working = true;
let workingStep = 1;
usernameInput.readOnly = true;
getButton.disabled = true;
function showWorkingDots() {
if (working) {
workingStatusText.textContent = `working${" .".repeat(workingStep)}`;
if (workingStep === 3) {
workingStep = 1;
} else {
workingStep++;
}
setTimeout(showWorkingDots, 150);
}
}
showWorkingDots();
}
function setFinishedStatus(errored, details) {
working = false;
workingStatusText.textContent = errored ? "ERROR!!!" : "done!";
if (errored) {
workingStatusText.classList.add("error");
resultText.classList.add("error");
}
resultText.innerHTML = details;
usernameInput.readOnly = false;
getButton.disabled = false;
}
function updateQueryParamsAndTitle(username) {
const url = new URL(window.location.href);
const searchParams = url.searchParams;
searchParams.set("username", username);
window.history.replaceState(null, null, url);
document.title = username.concat(" | ", defaultTitle);
}
function removeQueryParamsAndTitle() {
const url = new URL(window.location.href);
const searchParams = url.searchParams;
for (let key of Array.from(searchParams.keys())) {
searchParams.delete(key);
}
window.history.replaceState(null, null, url);
document.title = defaultTitle;
}
function resetOutputStatus() {
workingStatusText.innerHTML = "";
workingStatusText.classList.remove("error");
resultText.innerHTML = "";
resultText.classList.remove("error");
removeQueryParamsAndTitle();
}
async function copyToClipboard(text, button) {
try {
await navigator.clipboard.writeText(text);
const originalText = button.textContent;
button.textContent = "Copied!";
button.classList.add("copied");
setTimeout(() => {
button.textContent = originalText;
button.classList.remove("copied");
}, 1500);
} catch (err) {
console.error("Failed to copy to clipboard:", err);
}
}
function createResultHTML(userData, commitEmails) {
const name = userData.name || userData.login;
// Our priority is: commit email > API email > noreply GitHub standard email
const allEmails = new Set();
if (commitEmails && commitEmails.length > 0) {
commitEmails.forEach((email) => allEmails.add(email));
}
if (userData.email) {
allEmails.add(userData.email);
}
if (allEmails.size === 0) {
allEmails.add(`${userData.id}+${userData.login}@users.noreply.github.com`);
}
const emailsArray = Array.from(allEmails);
const isNoreply =
emailsArray.length === 1 && emailsArray[0].includes("noreply.github.com");
const hasMultiple = emailsArray.length > 1;
let html = `<div class="author-info">`;
html += `<div class="author-info-line" data-copy="${sanitizeString(
name
)}"><strong>Name:</strong> ${sanitizeString(name)}</div>`;
if (hasMultiple) {
html += `<div style="margin-top: 8px;"><strong>Email${
emailsArray.length > 1 ? "s" : ""
}:</strong></div>`;
emailsArray.forEach((email, index) => {
html += `<div class="author-info-line" data-copy="${sanitizeString(
email
)}" style="margin-left: 10px;">• ${sanitizeString(email)}</div>`;
});
} else {
html += `<div class="author-info-line" data-copy="${sanitizeString(
emailsArray[0]
)}"><strong>Email:</strong> ${sanitizeString(emailsArray[0])}</div>`;
}
html += `</div>`;
if (isNoreply) {
html += `<p class="noreply-note">No public email address was found from recent commits. Here's their GitHub noreply address.</p>`;
} else if (commitEmails && commitEmails.length > 0) {
html += `<p class="noreply-note">${
hasMultiple
? "These email addresses were found"
: "This email address was found"
} from their recent commits.</p>`;
}
html += `<div class="copy-buttons-container">`;
emailsArray.forEach((email, index) => {
const buttonLabel = hasMultiple
? `Copy #${index + 1} in "Name <email>"; format`
: `Copy as "Name <email>"`;
html += `<button class="copy-button" data-name="${sanitizeString(
name
)}" data-email="${sanitizeString(email)}">${buttonLabel}</button>`;
});
html += `</div>`;
return html;
}
async function onSubmit() {
if (!checkInputValidity()) {
return;
}
const username = extractUsername(usernameInput.value);
resetOutputStatus();
setWorkingStatus();
let resultString;
try {
const [userData, commitEmail] = await Promise.all([
getUserInfo(username),
getCommitEmail(username),
]);
resultString = createResultHTML(userData, commitEmail);
setFinishedStatus(false, resultString);
updateQueryParamsAndTitle(username);
// Add click handlers for all copy buttons
document.querySelectorAll(".copy-button").forEach((button) => {
button.addEventListener("click", () => {
const name = button.dataset.name;
const email = button.dataset.email;
copyToClipboard(`${name} <${email}>`, button);
});
});
document.querySelectorAll(".author-info-line").forEach((line) => {
line.addEventListener("click", () => {
const text = line.dataset.copy;
navigator.clipboard.writeText(text).then(() => {
const original = line.innerHTML;
line.innerHTML += " <em>(copied!)</em>";
setTimeout(() => {
line.innerHTML = original;
}, 1000);
});
});
});
} catch (err) {
if (err.name === "HTTPError") {
if (err.status === 404) {
resultString =
"That user does not exist. Please check the username and try again.";
} else if (
err.status === 403 &&
err.message.toLowerCase().includes("api rate limit exceeded")
) {
resultString =
"API rate limit exceeded. Please wait and try again later.";
} else {
resultString = `unexpected error: ${sanitizeString(err.toString())}`;
}
} else {
resultString = `unexpected error: ${sanitizeString(err.toString())}`;
}
setFinishedStatus(true, resultString);
}
}
usernameInput.addEventListener("keydown", (event) => {
usernameInput.setCustomValidity("");
if (event.key === "Enter") {
onSubmit();
}
});
getButton.addEventListener("click", onSubmit);
function maybeUseUsernameFromURL() {
const params = new URLSearchParams(document.location.search.substring(1));
let username = params.get("username");
if (username === null) {
return;
}
username = extractUsername(username);
usernameInput.blur();
const tipAdmonition = document.querySelector("#tip-admonition");
if (tipAdmonition) {
tipAdmonition.remove();
}
usernameInput.value = username;
getButton.click();
}
window.addEventListener("DOMContentLoaded", maybeUseUsernameFromURL);