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

scope.ts

158 lines · 6.1 KB · TypeScript
import githubRequest from 'kody:@kentcdodds/github/request'
import { normalizeRepoSlug } from './shared.ts'
import { branchPrefix, type ChangedFile } from './scope-core.ts'

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

async function gh(path: string) {
	const result = (await githubRequest({ account: 'bot', path })) as GithubResult
	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
}

/** GitHub caps the compare file list at 300 files. */
export const compareFileCap = 300

export type LastFix = { sha: string; number: number; branch: string; mergedAt: string } | null

/**
 * Most recently merged PR into `base` whose head branch starts with
 * `cleanup/<automation>-`. Its merge_commit_sha is the "last fix" marker.
 * Scans the newest 300 closed PRs (3 pages).
 */
export async function findLastFix(input: { repo: string; base: string; automation: string }): Promise<LastFix> {
	const slug = normalizeRepoSlug(input.repo)
	const prefix = branchPrefix(input.automation)
	let best: LastFix = null
	for (let page = 1; page <= 3; page += 1) {
		const data = await gh(
			`/repos/${slug}/pulls?state=closed&base=${encodeURIComponent(input.base)}&sort=updated&direction=desc&per_page=100&page=${page}`,
		)
		const pulls = Array.isArray(data) ? (data as Array<Record<string, any>>) : []
		for (const p of pulls) {
			const ref = String(p?.head?.ref ?? '')
			if (!ref.startsWith(prefix) || !p.merged_at || !p.merge_commit_sha) continue
			if (!best || String(p.merged_at) > best.mergedAt) {
				best = { sha: String(p.merge_commit_sha), number: Number(p.number), branch: ref, mergedAt: String(p.merged_at) }
			}
		}
		if (pulls.length < 100) break
	}
	return best
}

export async function getHeadSha(repo: string, branch: string) {
	const data = (await gh(`/repos/${normalizeRepoSlug(repo)}/branches/${encodeURIComponent(branch)}`)) as any
	const sha = data?.commit?.sha
	if (!sha) throw new Error('branch head has no commit')
	return String(sha)
}

/**
 * The later of two commits on the branch. `ahead` means b is newer than a.
 * Diverged or unknown keeps `b` (the caller passes the stored floor as b).
 */
export async function laterOf(repo: string, a: string | null, b: string | null) {
	if (!a) return b
	if (!b || a === b) return a
	try {
		const data = (await gh(`/repos/${normalizeRepoSlug(repo)}/compare/${a}...${b}?per_page=1`)) as any
		return data?.status === 'behind' ? a : b
	} catch {
		return b
	}
}

/** Parent of the oldest commit on `branch` in the last 24h (initial base), or null. */
export async function baseFromLookback(repo: string, branch: string, now = new Date()) {
	const since = new Date(now.getTime() - 24 * 60 * 60 * 1000).toISOString()
	const slug = normalizeRepoSlug(repo)
	let oldest: any = null
	for (let page = 1; page <= 5; page += 1) {
		const data = await gh(`/repos/${slug}/commits?sha=${encodeURIComponent(branch)}&since=${encodeURIComponent(since)}&per_page=100&page=${page}`)
		const list = Array.isArray(data) ? data : []
		if (list.length > 0) oldest = list[list.length - 1]
		if (list.length < 100) break
	}
	if (!oldest) return null
	return oldest.parents?.[0]?.sha ? String(oldest.parents[0].sha) : null
}

function mapFiles(raw: unknown): ChangedFile[] {
	return (Array.isArray(raw) ? raw : []).map((f: any) => ({
		path: String(f.filename),
		status: String(f.status ?? 'modified'),
		additions: Number(f.additions ?? 0),
		deletions: Number(f.deletions ?? 0),
		...(typeof f.patch === 'string' ? { patch: f.patch } : {}),
	}))
}

async function listRangeCommits(slug: string, base: string, head: string) {
	const shas: string[] = []
	for (let page = 1; page <= 20; page += 1) {
		const data = (await gh(`/repos/${slug}/compare/${base}...${head}?per_page=100&page=${page}`)) as any
		const commits = Array.isArray(data?.commits) ? data.commits : []
		for (const c of commits) shas.push(String(c.sha))
		if (commits.length < 100) break
	}
	return shas
}

function mergeFiles(target: Map<string, ChangedFile>, files: ChangedFile[]) {
	for (const f of files) {
		const prev = target.get(f.path)
		if (!prev) {
			target.set(f.path, { ...f })
			continue
		}
		target.set(f.path, {
			path: f.path,
			status: f.status,
			additions: prev.additions + f.additions,
			deletions: prev.deletions + f.deletions,
			patch: [prev.patch, f.patch].filter(Boolean).join('\n'),
		})
	}
}

/**
 * Changed files in base...head. When the comparison hits GitHub's 300 file
 * cap, walk the range's commits (paginated) and compare consecutive windows,
 * halving a window that still hits the cap, then merge per path.
 */
export async function compareFiles(input: { repo: string; base: string; head: string }) {
	const slug = normalizeRepoSlug(input.repo)
	const first = (await gh(`/repos/${slug}/compare/${input.base}...${input.head}`)) as any
	const files = mapFiles(first?.files)
	const totalCommits = Number(first?.total_commits ?? 0)
	if (files.length < compareFileCap) return { files, totalCommits, windows: 1, capped: false }
	const commits = await listRangeCommits(slug, input.base, input.head)
	const merged = new Map<string, ChangedFile>()
	let windows = 0
	const walk = async (from: string, list: string[]) => {
		if (list.length === 0) return
		const to = list[list.length - 1]
		const data = (await gh(`/repos/${slug}/compare/${from}...${to}`)) as any
		const part = mapFiles(data?.files)
		if (part.length >= compareFileCap && list.length > 1) {
			const mid = Math.floor(list.length / 2)
			await walk(from, list.slice(0, mid))
			await walk(list[mid - 1], list.slice(mid))
			return
		}
		windows += 1
		mergeFiles(merged, part)
	}
	await walk(input.base, commits)
	return { files: [...merged.values()], totalCommits, windows, capped: true }
}

/** All blob paths at a commit (for cheap test-coverage detection). */
export async function repoPathsAt(repo: string, sha: string) {
	const data = (await gh(`/repos/${normalizeRepoSlug(repo)}/git/trees/${sha}?recursive=1`)) as any
	const tree = Array.isArray(data?.tree) ? data.tree : []
	return new Set<string>(tree.filter((t: any) => t.type === 'blob').map((t: any) => String(t.path)))
}