← Public packages
@ifyoumakeit/pr-health
Tells you which of your open GitHub PRs are actually stuck: red CI, conflicts, or unanswered bots.
Files
import { credential } from 'kody:runtime'
const DAY = 86_400_000
const ALLOWED_KEYS = ['abandonedDays', 'botWindowDays', 'ignoreBots']
const DEFAULT_IGNORED_BOTS = [
'netlify',
'vercel',
'cloudflare-workers-and-pages',
'github-actions',
'dependabot',
'renovate',
]
const QUERY = `query($q:String!){ search(query:$q, type:ISSUE, first:100){ nodes{ ... on PullRequest {
number title url isDraft updatedAt mergeable reviewDecision repository{ nameWithOwner }
commits(last:1){ nodes{ commit{ statusCheckRollup{ state } } } }
comments(last:1){ nodes{ createdAt author{ __typename login } } }
reviews(last:1){ nodes{ submittedAt author{ __typename login } } }
} } } }`
export type StuckReason = 'ci-failing' | 'conflicts' | 'bot-waiting' | 'changes-requested'
export type StuckPr = {
repo: string
number: number
title: string
url: string
updatedAt: string
reasons: StuckReason[]
}
function normalizeBot(login: string): string {
return login.toLowerCase().replace(/\[bot\]$/, '')
}
/**
* List your open, non-draft GitHub PRs that have a concrete, fixable problem:
* failing CI, merge conflicts, an unanswered review bot, or changes requested.
* Call when a person asks which of their PRs need attention. Read-only.
*
* Drafts are skipped, and PRs idle longer than `abandonedDays` count as parked.
* Idleness alone is never reported as stuck. Uses the `github` credential slot.
*
* @param input - Optional settings. Unknown keys are rejected.
* @param input.abandonedDays - PRs idle longer than this are parked, not stuck (default 120).
* @param input.botWindowDays - Bot comments only count on PRs active within this many days (default 14).
* @param input.ignoreBots - Extra bot logins to ignore (deploy-preview bots are ignored by default).
* @returns `{ stuck, checked, skipped: { drafts, parked }, summary }`
*
* @example
* import check from 'kody:@ifyoumakeit/pr-health/check'
*
* const { stuck, summary } = await check({})
*/
export default async function check(raw: unknown = {}) {
const input = (raw ?? {}) as Record<string, unknown>
if (typeof input !== 'object' || Array.isArray(input)) {
throw new Error('check() expects an object: { abandonedDays?, botWindowDays?, ignoreBots? }')
}
const unknownKeys = Object.keys(input).filter((k) => !ALLOWED_KEYS.includes(k))
if (unknownKeys.length) {
throw new Error(`Unknown input key(s): ${unknownKeys.join(', ')}. Accepted: ${ALLOWED_KEYS.join(', ')}`)
}
const abandonedDays = typeof input.abandonedDays === 'number' ? input.abandonedDays : 120
const botWindowDays = typeof input.botWindowDays === 'number' ? input.botWindowDays : 14
const extraBots = Array.isArray(input.ignoreBots) ? input.ignoreBots.map(String) : []
const ignored = new Set([...DEFAULT_IGNORED_BOTS, ...extraBots].map(normalizeBot))
const github = await credential('github')
const res = await github.fetch('https://api.github.com/graphql', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ query: QUERY, variables: { q: 'is:pr is:open author:@me archived:false' } }),
})
if (!res.ok) throw new Error(`GitHub GraphQL ${res.status}: ${await res.text()}`)
const json = (await res.json()) as any
if (json.errors?.length) throw new Error(`GitHub GraphQL error: ${json.errors[0].message}`)
const now = Date.now()
const prs = (json.data?.search?.nodes ?? []).filter((p: any) => p?.number)
const stuck: StuckPr[] = []
let drafts = 0
let parked = 0
for (const p of prs) {
if (p.isDraft) { drafts++; continue }
const idleDays = (now - Date.parse(p.updatedAt)) / DAY
if (idleDays > abandonedDays) { parked++; continue }
const reasons: StuckReason[] = []
const ci = p.commits?.nodes?.[0]?.commit?.statusCheckRollup?.state
if (ci === 'FAILURE' || ci === 'ERROR') reasons.push('ci-failing')
if (p.mergeable === 'CONFLICTING') reasons.push('conflicts')
if (p.reviewDecision === 'CHANGES_REQUESTED') reasons.push('changes-requested')
const c = p.comments?.nodes?.[0]
const r = p.reviews?.nodes?.[0]
const last = [
c && { at: c.createdAt, author: c.author },
r && { at: r.submittedAt, author: r.author },
].filter(Boolean).sort((a: any, b: any) => Date.parse(b.at) - Date.parse(a.at))[0] as any
if (
last?.author?.__typename === 'Bot' &&
!ignored.has(normalizeBot(last.author.login)) &&
idleDays <= botWindowDays
) reasons.push('bot-waiting')
if (reasons.length) {
stuck.push({ repo: p.repository.nameWithOwner, number: p.number, title: p.title, url: p.url, updatedAt: p.updatedAt, reasons })
}
}
const summary = stuck.length
? stuck.map((s) => `${s.repo}#${s.number} ${s.title} — ${s.reasons.join(', ')}`).join('\n')
: 'Nothing stuck. 🎉'
return { stuck, checked: prs.length, skipped: { drafts, parked }, summary }
}