Skip to content
← Public packages

@kentcdodds/devin

Start, monitor, and manage Devin sessions, knowledge, playbooks, and schedules via the Devin v3 API

src/reviews.ts

59 lines · 1.4 KB · TypeScript
import {
	compact,
	DevinApiError,
	devinOrgApi,
	devinRequestRaw,
	resolveOrgId,
	trimString,
} from './lib/client.ts'

export type PrReview = {
	status: string
	repo_path: string
	pr_number: number
	commit_sha: string
	created_at: number
	[key: string]: unknown
}

/**
 * Review status for a PR (optionally a specific commit). Returns `null` when
 * Devin has never reviewed that PR (404) rather than throwing.
 */
export async function getPrReview(params: {
	prUrl: string
	orgId?: string
	commitSha?: string
}): Promise<PrReview | null> {
	const prUrl = trimString(params.prUrl)
	if (!prUrl) throw new Error('getPrReview requires a pr_url')

	const org = await resolveOrgId(params.orgId)
	const result = await devinRequestRaw({
		path: `/v3/organizations/${org}/pr-reviews`,
		query: compact({ pr_url: prUrl, commit_sha: params.commitSha }),
	})
	if (result.status === 404) return null
	if (result.status < 200 || result.status >= 300) {
		throw new DevinApiError(result.status, result.body)
	}
	return result.body as PrReview
}

/** Kick off a Devin PR review. Consumes ACUs. */
export async function createPrReview(params: {
	prUrl: string
	orgId?: string
}) {
	const prUrl = trimString(params.prUrl)
	if (!prUrl) throw new Error('createPrReview requires a pr_url')

	return devinOrgApi<PrReview>({
		orgId: params.orgId,
		path: '/pr-reviews',
		method: 'POST',
		body: { pr_url: prUrl },
	})
}

export default getPrReview