Skip to content

Built for people who want to own their automations. Join the waitlist for an invite.

Package listing

@kentcdodds/lineage

src/ingest-missing.ts

57 lines · 1.4 KB · TypeScript
import { collectBranchHistory, ingestCommitSha, ingestMissingFromList } from './ingest.ts'
import { getRepoSettings, hasCommit, ingestSummary } from './storage.ts'

/**
 * Fill any commits that are on the tracked branch but missing from storage.
 * Use after a webhook, or to catch commits the backfill skipped.
 *
 * @param input - Optional explicit SHAs; otherwise scans recent branch history
 * @returns Newly ingested SHAs and current storage summary
 *
 * @example
 * import ingestMissing from 'kody:@kentcdodds/lineage/ingest-missing'
 *
 * const result = await ingestMissing({ limit: 20 })
 */
export default async function ingestMissing(
	input: {
		shas?: string[]
		limit?: number
		maxPages?: number
	} = {},
) {
	const repo = await getRepoSettings()
	if (input.shas && input.shas.length > 0) {
		const ingested: string[] = []
		const skipped: string[] = []
		for (const sha of input.shas) {
			if (await hasCommit(sha)) {
				skipped.push(sha)
				continue
			}
			await ingestCommitSha(sha)
			ingested.push(sha)
		}
		return {
			ok: true,
			repo,
			ingested: ingested.length,
			alreadyStored: skipped.length,
			shas: ingested,
			ingest: await ingestSummary(),
		}
	}

	const history = await collectBranchHistory(input.maxPages ?? 5)
	const result = await ingestMissingFromList(history, {
		limit: input.limit ?? 40,
		deadlineMs: 50_000,
	})
	return {
		ok: true,
		repo,
		listed: history.length,
		...result,
		ingest: await ingestSummary(),
	}
}