Skip to content

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

Package listing

@kentcdodds/github

src/pr/get-info.ts

39 lines · 1.4 KB · TypeScript
import { request } from '../core.ts'
import type { GitHubAccount } from '../types.ts'
import { normalizePrLocator } from './pr-input.ts'

/**
 * Fetch pull request details.
 *
 * @param params - PR locator (`prUrl` or `owner`/`repo`/`prNumber`) plus optional `account`.
 * @returns PR number, title, state, draft flag, and URLs.
 * @example
 * import getPrInfo from 'kody:@kentcdodds/github/pr/get-info'
 * const pr = await getPrInfo({ owner: 'kentcdodds', repo: 'kody', prNumber: 1, account: 'bot' })
 * // => { number: 1, title: '...', state: 'open', draft: false }
 */
export default async function getPrInfo(params: Record<string, unknown> = {}) {
	const account = (params.account as GitHubAccount | undefined) ?? 'bot'
	const { owner, repo, prNumber } = normalizePrLocator(params)

	const response = await request<Record<string, unknown>>({
		account,
		path: `/repos/${owner}/${repo}/pulls/${prNumber}`,
		throwOnError: true,
	})
	const pr = response.data!

	return {
		nodeId: pr.node_id as string,
		number: pr.number as number,
		title: pr.title as string,
		state: pr.state as 'open' | 'closed',
		draft: pr.draft as boolean,
		url: pr.html_url as string,
		repository: `${owner}/${repo}`,
		createdAt: pr.created_at as string,
		updatedAt: pr.updated_at as string,
		mergedAt: pr.merged_at as string | null,
		user: (pr.user as { login?: string } | undefined)?.login as string,
	}
}