Skip to content
← Public packages

@kody/kody-factory

Forkable multi-project software factory: signals, priority queue, project UI, sweep job. Agent runners and Sentry reach it by events.

FollowFork

Files

github-port.ts

348 lines · 13.6 KB · TypeScript
import githubGraphql from 'kody:@kody/github/graphql'
import githubRequest from 'kody:@kody/github/request'
import type { PrLocator } from './input.ts'
import { humanReplyBody } from './human.ts'
import { makeMentionSafe } from './mention-safe.ts'
import {
	actorLogin,
	dedupeChecks,
	normalizeContexts,
	RUNNER_NOT_ACQUIRED,
	type PrSnapshot,
	type ProductionInfo,
	type ThreadComment,
	type WorkflowRun,
} from './snapshot.ts'

const PR_QUERY = `query ShipPr($owner: String!, $repo: String!, $number: Int!) {
  repository(owner: $owner, name: $repo) {
    pullRequest(number: $number) {
      title url state isDraft createdAt merged mergedAt mergeable mergeStateStatus reviewDecision
      additions deletions baseRefName headRefName headRefOid
      mergeCommit { oid }
      baseRef { refUpdateRule { requiredStatusCheckContexts } }
      timelineItems(last: 1, itemTypes: [READY_FOR_REVIEW_EVENT]) { nodes { ... on ReadyForReviewEvent { createdAt } } }
      author { login __typename }
      commits(last: 1) { nodes { commit { oid committedDate statusCheckRollup { state contexts(first: 100) { nodes {
        __typename
        ... on CheckRun { name status conclusion detailsUrl startedAt completedAt checkSuite { app { slug } } }
        ... on StatusContext { context state targetUrl description createdAt }
      } } } } } }
      reviewThreads(first: 100) {
        pageInfo { hasNextPage }
        nodes { id isResolved isOutdated path line
          root: comments(first: 1) { nodes { ...ThreadComment } }
          comments(last: 49) { totalCount nodes { ...ThreadComment } }
        }
      }
      reviews(last: 50) { totalCount nodes { databaseId state body submittedAt url authorAssociation commit { oid } author { login __typename } } }
      comments(last: 100) { totalCount nodes { databaseId body createdAt url authorAssociation author { login __typename } } }
    }
  }
}
fragment ThreadComment on PullRequestReviewComment {
  databaseId body url createdAt authorAssociation author { login __typename } replyTo { databaseId }
}`

type Gql = Record<string, any>

/** Outbound fetches this module made since the last `takeFetchCount()`. */
let fetchCount = 0

/** Return and reset the outbound-fetch count (tick reports it as `cost`). */
export function takeFetchCount(): number {
	const n = fetchCount
	fetchCount = 0
	return n
}

export async function fetchSnapshot(locator: PrLocator): Promise<PrSnapshot> {
	fetchCount++
	const result = (await githubGraphql({
		query: PR_QUERY,
		variables: { owner: locator.owner, repo: locator.repo, number: locator.prNumber },
	})) as Gql
	const pr = result?.data?.repository?.pullRequest as Gql | undefined
	if (!pr) {
		const errors = Array.isArray(result?.errors)
			? result.errors.map((e: Gql) => e?.message).join('; ')
			: ''
		throw new Error(
			`ship-pr: could not load ${locator.prUrl} as kody-bot${errors ? `: ${errors}` : ''}.`,
		)
	}
	const commit = pr.commits?.nodes?.[0]?.commit as Gql | undefined
	const contexts = (commit?.statusCheckRollup?.contexts?.nodes ?? []) as Array<Gql>
	const deduped = dedupeChecks(normalizeContexts(contexts))
	const mergeCommitSha = pr.mergeCommit?.oid ? String(pr.mergeCommit.oid) : null
	const headSha = String(pr.headRefOid ?? commit?.oid ?? '')

	let deployRuns: Array<WorkflowRun> | null = null
	if (pr.merged && mergeCommitSha) {
		deployRuns = await fetchWorkflowRuns(locator, mergeCommitSha)
	}
	const production = pr.merged && mergeCommitSha ? await fetchProduction(locator, mergeCommitSha) : null
	// Workflow runs exist (queued) before their jobs report check runs, so CI
	// gates on them too, not only on the status-check rollup.
	const headRuns = pr.state === 'OPEN' && headSha ? await fetchWorkflowRuns(locator, headSha) : []

	return {
		locator,
		title: String(pr.title ?? ''),
		state: pr.state,
		isDraft: Boolean(pr.isDraft),
		merged: Boolean(pr.merged),
		mergedAt: pr.mergedAt ?? null,
		mergeCommitSha,
		mergeable: String(pr.mergeable ?? 'UNKNOWN'),
		mergeStateStatus: String(pr.mergeStateStatus ?? 'UNKNOWN'),
		reviewDecision: pr.reviewDecision ?? null,
		author: actorLogin(pr.author),
		baseRef: String(pr.baseRefName ?? ''),
		headRef: String(pr.headRefName ?? ''),
		headSha,
		headCommittedDate: commit?.committedDate ?? null,
		createdAt: pr.createdAt ?? null,
		readyForReviewAt: pr.timelineItems?.nodes?.[0]?.createdAt ?? null,
		additions: Number(pr.additions ?? 0),
		deletions: Number(pr.deletions ?? 0),
		checks: deduped.checks,
		requiredChecks: ((pr.baseRef?.refUpdateRule?.requiredStatusCheckContexts ?? []) as Array<unknown>)
			.filter((c): c is string => typeof c === 'string' && c.length > 0),
		duplicateChecksHidden: deduped.hidden,
		headRuns,
		threads: ((pr.reviewThreads?.nodes ?? []) as Array<Gql>).map((t) => {
			const comments = mergeThreadComments(t)
			return {
				id: String(t.id),
				isResolved: Boolean(t.isResolved),
				isOutdated: Boolean(t.isOutdated),
				path: t.path ?? null,
				line: typeof t.line === 'number' ? t.line : null,
				comments,
				commentsTruncated: Number(t.comments?.totalCount ?? 0) > comments.length,
			}
		}),
		threadsTruncated: Boolean(pr.reviewThreads?.pageInfo?.hasNextPage),
		reviews: ((pr.reviews?.nodes ?? []) as Array<Gql>).map((r) => ({
			id: Number(r.databaseId),
			author: actorLogin(r.author),
			state: String(r.state ?? ''),
			body: String(r.body ?? ''),
			submittedAt: r.submittedAt ?? null,
			commitSha: r.commit?.oid ?? null,
			url: r.url ?? null,
			authorAssociation: r.authorAssociation ?? null,
		})),
		reviewsTruncated: Number(pr.reviews?.totalCount ?? 0) > (pr.reviews?.nodes?.length ?? 0),
		issueComments: ((pr.comments?.nodes ?? []) as Array<Gql>).map((c) => ({
			id: Number(c.databaseId),
			author: actorLogin(c.author),
			body: String(c.body ?? ''),
			createdAt: String(c.createdAt ?? ''),
			url: c.url ?? null,
			authorAssociation: c.authorAssociation ?? null,
		})),
		issueCommentsTruncated: Number(pr.comments?.totalCount ?? 0) > (pr.comments?.nodes?.length ?? 0),
		deployRuns,
		production,
		fetchedAt: new Date().toISOString(),
	}
}

function threadComment(c: Gql): ThreadComment {
	return {
		id: Number(c.databaseId),
		author: actorLogin(c.author),
		body: String(c.body ?? ''),
		url: c.url ?? null,
		createdAt: String(c.createdAt ?? ''),
		replyToId: typeof c.replyTo?.databaseId === 'number' ? c.replyTo.databaseId : null,
		authorAssociation: c.authorAssociation ?? null,
	}
}

/** Root comment plus the newest comments, deduped, oldest first (the last word is never cut off). */
export function mergeThreadComments(t: Gql): Array<ThreadComment> {
	const root = ((t.root?.nodes ?? []) as Array<Gql>).map(threadComment)
	const latest = ((t.comments?.nodes ?? []) as Array<Gql>).map(threadComment)
	const seen = new Set<number>()
	const out: Array<ThreadComment> = []
	for (const c of [...root, ...latest]) {
		if (seen.has(c.id)) continue
		seen.add(c.id)
		out.push(c)
	}
	return out
}

export async function fetchWorkflowRuns(locator: PrLocator, sha: string): Promise<Array<WorkflowRun>> {
	fetchCount++
	const response = (await githubRequest({
		path: `/repos/${locator.owner}/${locator.repo}/actions/runs?head_sha=${sha}&per_page=100`,
	})) as Gql
	const runs = (response?.data?.workflow_runs ?? []) as Array<Gql>
	return runs.map((w) => ({
		id: Number(w.id),
		name: String(w.name ?? ''),
		event: String(w.event ?? ''),
		status: String(w.status ?? ''),
		conclusion: w.conclusion ?? null,
		url: String(w.html_url ?? ''),
		createdAt: String(w.created_at ?? ''),
	}))
}

// FORKERS: replace with your own repo-to-health-URL mapping.
const HEALTH_URLS: Record<string, string> = {
	'kody-hq/kody': 'https://kody.codes/health',
}

/**
 * For repos with a known production health endpoint, read the served
 * `commitSha` and check (compare API) whether it is the merge SHA or a
 * descendant. A newer push can cancel Validate/Deploy on the merge SHA via a
 * concurrency group; production then ships the merge inside a later SHA.
 */
export async function fetchProduction(locator: PrLocator, mergeSha: string): Promise<ProductionInfo | null> {
	const url = HEALTH_URLS[`${locator.owner}/${locator.repo}`.toLowerCase()]
	if (!url) return null
	const info: ProductionInfo = { url, commitSha: null, containsMerge: null, compareStatus: null, error: null }
	try {
		fetchCount++
		const res = await fetch(url, { headers: { accept: 'application/json' } })
		if (!res.ok) {
			info.error = `health returned ${res.status}`
			return info
		}
		const body = (await res.json()) as Gql
		const sha = typeof body?.commitSha === 'string' ? body.commitSha : typeof body?.commit?.sha === 'string' ? body.commit.sha : null
		info.commitSha = sha
		if (!sha) {
			info.error = 'health response has no commitSha'
			return info
		}
		if (sha === mergeSha) {
			info.containsMerge = true
			info.compareStatus = 'identical'
			return info
		}
		fetchCount++
		const compare = (await githubRequest({
			path: `/repos/${locator.owner}/${locator.repo}/compare/${mergeSha}...${sha}`,
		})) as Gql
		const status = typeof compare?.data?.status === 'string' ? compare.data.status : null
		info.compareStatus = status
		if (status) info.containsMerge = status === 'identical' || status === 'ahead'
		else info.error = `compare failed (${compare?.status ?? 'unknown'})`
	} catch (error) {
		info.error = error instanceof Error ? error.message : String(error)
	}
	return info
}

/**
 * Comment bodies are prose that may quote the `{{secret:<name>}}` placeholder syntax
 * (common when replying to reviews of credential code). The gateway refuses a
 * body placeholder unless the request opts into headers-only resolution; the
 * auth header placeholder still resolves. Set this on every GitHub request that
 * posts user-visible text.
 */
export const PROSE_BODY_HEADERS = { 'x-kody-secret-resolution': 'headers-only' } as const

/** True when the job's annotations say no runner picked it up (not a test failure or concurrency cancel). */
export async function jobRunnerNotAcquired(locator: PrLocator, jobId: number): Promise<boolean> {
	fetchCount++
	const result = (await githubRequest({
		path: `/repos/${locator.owner}/${locator.repo}/check-runs/${jobId}/annotations`,
	})) as Gql
	const annotations = (Array.isArray(result?.data) ? result.data : []) as Array<Gql>
	return annotations.some((a) => RUNNER_NOT_ACQUIRED.test(String(a?.message ?? '')))
}

/** Re-run one Actions job (its dependents re-run with it). */
export async function rerunJob(locator: PrLocator, jobId: number): Promise<void> {
	const path = `/repos/${locator.owner}/${locator.repo}/actions/jobs/${jobId}/rerun`
	fetchCount++
	const result = (await githubRequest({ path, method: 'POST', confirm: true })) as Gql
	if (result?.ok === false || (typeof result?.status === 'number' && result.status >= 400)) {
		throw new Error(
			`ship-pr: ${path} failed (${result?.status}): ${String(result?.text ?? '').slice(0, 200)}. The bot account needs Actions write on this repo; otherwise re-run the job by hand.`,
		)
	}
}

/**
 * Reply to a PR review comment thread as kody-bot.
 *
 * The body goes through a deterministic mention-safety pass first (package
 * names and `kody:@scope/name` specifiers in inline code, "by @someone"
 * credits dropped). `@kody/github` refuses any remaining @-mention
 * unless `options.allowedMentions` lists it. Pass only a handle that is
 * already in the thread, such as the reviewer on their own thread.
 */
export async function replyToReviewComment(
	locator: PrLocator,
	commentId: number,
	body: string,
	options: { allowedMentions?: Array<string> } = {},
): Promise<{ id: number | null; url: string | null }> {
	const path = `/repos/${locator.owner}/${locator.repo}/pulls/${locator.prNumber}/comments/${commentId}/replies`
	fetchCount++
	const result = (await githubRequest({
		path,
		method: 'POST',
		headers: { ...PROSE_BODY_HEADERS },
		body: { body: makeMentionSafe(body) },
		confirm: true,
		...(options.allowedMentions?.length ? { allowedMentions: options.allowedMentions } : {}),
	})) as Gql
	if (result?.ok === false || (typeof result?.status === 'number' && result.status >= 400)) {
		throw new Error(
			`ship-pr: reply ${path} failed (${result?.status}): ${String(result?.text ?? '').slice(0, 200)}`,
		)
	}
	return { id: result?.data?.id ?? null, url: result?.data?.html_url ?? null }
}

/** Resolve a review thread as kody-bot (GraphQL node id `PRRT_…`). */
export async function resolveReviewThread(threadId: string): Promise<boolean> {
	fetchCount++
	const result = (await githubGraphql({
		query: `mutation($id: ID!) { resolveReviewThread(input: { threadId: $id }) { thread { id isResolved } } }`,
		variables: { id: threadId },
		confirm: true,
	})) as Gql
	return Boolean(result?.data?.resolveReviewThread?.thread?.isResolved)
}

/**
 * Post a top-level PR comment as kody-bot that answers one human review body
 * or PR comment (`target` is `comment:<id>` or `review:<id>`). The comment
 * links the item it answers and carries a hidden marker so tick counts the
 * item as replied. The body goes through the mention-safety pass and
 * `@kody/github` still refuses every @-mention: no allowedMentions are
 * ever passed here.
 */
export async function replyToHumanItem(
	locator: PrLocator,
	target: string,
	itemUrl: string | null,
	body: string,
): Promise<{ id: number | null; url: string | null }> {
	const path = `/repos/${locator.owner}/${locator.repo}/issues/${locator.prNumber}/comments`
	fetchCount++
	const result = (await githubRequest({
		path,
		method: 'POST',
		headers: { ...PROSE_BODY_HEADERS },
		body: { body: humanReplyBody(target, itemUrl, body) },
		confirm: true,
	})) as Gql
	if (result?.ok === false || (typeof result?.status === 'number' && result.status >= 400)) {
		throw new Error(
			`ship-pr: comment ${path} failed (${result?.status}): ${String(result?.text ?? '').slice(0, 200)}`,
		)
	}
	return { id: result?.data?.id ?? null, url: result?.data?.html_url ?? null }
}