MCPcopy Create free account
hub / github.com/ChinaGodMan/UserScripts / getCoAuthors

Function getCoAuthors

github-co-authors/github-co-authors.user.js:456–488  ·  view source on GitHub ↗

Get participants for the current PR and generate `Co-authored-by` messages for them.

()

Source from the content-addressed store, hash-verified

454
455/** Get participants for the current PR and generate `Co-authored-by` messages for them. */
456async function getCoAuthors() {
457 const [, owner, repo, _pull, id] = window.location.pathname.split('/')
458 const pullNumber = parseInt(id || '', 10)
459
460 const [prData, comments, reviewComments, reviews] = await Promise.all([
461 fetchGitHubAPI(`/repos/${owner}/${repo}/pulls/${pullNumber}`),
462 fetchGitHubAPI(`/repos/${owner}/${repo}/issues/${pullNumber}/comments`),
463 fetchGitHubAPI(`/repos/${owner}/${repo}/pulls/${pullNumber}/comments`),
464 fetchGitHubAPI(`/repos/${owner}/${repo}/pulls/${pullNumber}/reviews`)
465 ])
466
467 const participants = /** @type {Map<string, { name: string; email: string }>} */ (new Map())
468
469 // Add commenters
470 for (const { user } of [...comments, ...reviewComments, ...reviews]) {
471 // Skip bot comments
472 if (user.type === 'Bot') continue
473 // Skip PR author
474 if (user.login === prData.user.login) continue
475 // Add commenters
476 if (!participants.has(user.login)) {
477 participants.set(user.login, {
478 name: user.name || user.login,
479 email: `${user.id}+${user.login}@users.noreply.github.com`
480 })
481 }
482 }
483
484 const lines = Array.from(participants.values()).map(
485 (p) => `Co-authored-by: ${p.name} <${p.email}>`
486 )
487 return { message: lines.join('\n'), count: lines.length }
488}
489
490/**
491 * Simple wrapper around `fetch()` for making GitHub API requests, e.g. `fetchGitHubAPI('/repos/withastro')`.

Callers 1

createCoAuthorsUIFunction · 0.85

Calls 1

fetchGitHubAPIFunction · 0.85

Tested by

no test coverage detected