Skip to content
← Public packages

@kentcdodds/codebase-cleanup

Cursor cloud agents for legacy cleanup, docs freshness, tight tests, and prod CI fixes, plus a weekly Devin general audit.

FollowFork

Files

github.ts

116 lines · 3.7 KB · TypeScript
import githubRequest from 'kody:@kentcdodds/github/request'
import { lookbackMs, normalizeRepoSlug } from './shared.ts'

export {
	parseCiFailurePayload,
	parseCiCompletedPayload,
	type CiFailureEvent,
	type CiCompletedEvent,
} from './ci-parse.ts'

type GithubRequestResult = {
	ok?: boolean
	status?: number
	data?: unknown
	text?: string
}

function requireOk(result: GithubRequestResult, path: string) {
	if (result?.ok === false || (result.status && result.status >= 400)) {
		throw new Error(
			`GitHub ${path} failed (${result.status}): ${String(result.text || '').slice(0, 200)}`,
		)
	}
	return result.data
}

/**
 * True when the branch has at least one commit in the last 24 hours.
 */
export async function hasCommitsInLast24h(input: {
	repo: string
	branch: string
	sinceMs?: number
	now?: Date
}) {
	const slug = normalizeRepoSlug(input.repo)
	const sinceMs = input.sinceMs ?? lookbackMs
	const since = new Date((input.now ?? new Date()).getTime() - sinceMs).toISOString()
	const path = `/repos/${slug}/commits?sha=${encodeURIComponent(input.branch)}&since=${encodeURIComponent(since)}&per_page=1`
	const data = requireOk(await githubRequest({ account: 'bot', path }), path)
	return Array.isArray(data) && data.length > 0
}

/** Current head commit SHA of a branch. */
export async function getBranchHeadSha(input: { repo: string; branch: string }) {
	const slug = normalizeRepoSlug(input.repo)
	const path = `/repos/${slug}/branches/${encodeURIComponent(input.branch)}`
	const data = requireOk(await githubRequest({ account: 'bot', path }), path) as {
		commit?: { sha?: string }
	}
	const sha = data?.commit?.sha
	if (!sha) throw new Error(`GitHub ${path} returned no head commit`)
	return sha
}

export type WorkflowRunSummary = {
	id: number
	workflow_id: number
	name: string
	path: string
	event: string
	head_branch: string
	head_sha: string
	status: string
	conclusion: string | null
	run_attempt: number
	created_at: string
	html_url: string
}

/** GitHub Actions workflow runs for one commit SHA (first 100). */
export async function listWorkflowRunsForSha(input: { repo: string; sha: string }) {
	const slug = normalizeRepoSlug(input.repo)
	const path = `/repos/${slug}/actions/runs?head_sha=${encodeURIComponent(input.sha)}&per_page=100`
	const data = requireOk(await githubRequest({ account: 'bot', path }), path) as {
		workflow_runs?: Array<Record<string, unknown>>
	}
	return (data?.workflow_runs ?? []).map((r) => ({
		id: Number(r.id),
		workflow_id: Number(r.workflow_id),
		name: String(r.name ?? ''),
		path: String(r.path ?? ''),
		event: String(r.event ?? ''),
		head_branch: String(r.head_branch ?? ''),
		head_sha: String(r.head_sha ?? ''),
		status: String(r.status ?? ''),
		conclusion: r.conclusion == null ? null : String(r.conclusion),
		run_attempt: Number(r.run_attempt ?? 1),
		created_at: String(r.created_at ?? ''),
		html_url: String(r.html_url ?? ''),
	})) as WorkflowRunSummary[]
}

export type OpenPullSummary = {
	number: number
	title: string
	body: string
	html_url: string
	head_ref: string
	created_at: string
}

/** Open PRs targeting a base branch (newest 50). */
export async function listOpenPullsForBase(input: { repo: string; base: string }) {
	const slug = normalizeRepoSlug(input.repo)
	const path = `/repos/${slug}/pulls?state=open&base=${encodeURIComponent(input.base)}&sort=created&direction=desc&per_page=50`
	const data = requireOk(await githubRequest({ account: 'bot', path }), path)
	return (Array.isArray(data) ? data : []).map((p: Record<string, unknown>) => ({
		number: Number(p.number),
		title: String(p.title ?? ''),
		body: String(p.body ?? ''),
		html_url: String(p.html_url ?? ''),
		head_ref: String(((p.head || {}) as Record<string, unknown>).ref ?? ''),
		created_at: String(p.created_at ?? ''),
	})) as OpenPullSummary[]
}