Skip to content

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

Package listing

@kentcdodds/github

src/pr/workflows-probe.ts

51 lines · 1.5 KB · TypeScript
import { workflows } from 'kody:runtime'

/**
 * Prove that `workflows` from `kody:runtime` is reachable inside this package's
 * saved-package invocation runtime. Creates a trivial inline workflow that
 * returns a constant, then returns the create receipt for the caller to poll
 * with `workflow_run_list`.
 *
 * @param params.idempotencyKey - Optional stable key; defaults to a unique probe key.
 * @returns `{ ok, workflowId, status, idempotencyKey, workflowsAvailable }`.
 * @example
 * import probe from 'kody:@kentcdodds/github/pr/workflows-probe'
 * const result = await probe({})
 * // => { ok: true, workflowsAvailable: true, workflowId: '...', status: 'queued' }
 */
export default async function workflowsProbe(
	params: Record<string, unknown> = {},
) {
	if (!workflows) {
		return {
			ok: false,
			workflowsAvailable: false,
			workflowId: null,
			status: null,
			idempotencyKey: null,
			error: 'workflows is null in this package runtime',
		}
	}

	const idempotencyKey =
		typeof params.idempotencyKey === 'string' && params.idempotencyKey.trim()
			? params.idempotencyKey.trim()
			: `github:workflows-probe:${Date.now()}`

	const created = await workflows.create({
		workflowName: 'github-workflows-probe',
		idempotencyKey,
		code: `export default async function main() {
  return { ok: true, probe: 'github-workflows-probe', at: Date.now() }
}`,
	})

	return {
		ok: true,
		workflowsAvailable: true,
		workflowId: created.id,
		status: created.status ?? null,
		idempotencyKey,
		workflow_name: created.workflow_name,
	}
}