Skip to content

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

Package listing

@kody/gitlab

src/pipelines/retry.ts

50 lines · 1.3 KB · TypeScript
import { isConfirmed, isDryRun, normalizePipelineLocator, pickAuthInput, request } from '../core.ts'
import { asRecord, slimPipeline } from '../shape.ts'

/**
 * Retry a GitLab pipeline.
 *
 * Pass `dryRun: true` to preview the request. Live retries require `confirm: true`.
 *
 * @example
 * import retryGitlabPipeline from 'kody:@kody/gitlab/pipelines/retry'
 * const preview = await retryGitlabPipeline({
 *   project: 'example-group/example-project',
 *   pipelineId: 88,
 *   dryRun: true,
 * })
 */
export default async function retryGitlabPipeline(params: Record<string, unknown> = {}) {
	const auth = pickAuthInput(params)
	const { projectId, projectPath, pipelineId } = normalizePipelineLocator(params)

	if (isDryRun(params)) {
		return {
			dryRun: true,
			wouldRetry: true,
			projectPath,
			pipelineId,
			auth,
		}
	}

	if (!isConfirmed(params)) {
		throw new Error(
			`Retrying pipeline ${pipelineId} in ${projectPath} requires dryRun: true (preview) or confirm: true (live write).`,
		)
	}

	const response = await request<Record<string, unknown>>({
		...auth,
		method: 'POST',
		path: `/projects/${projectId}/pipelines/${pipelineId}/retry`,
		throwOnError: true,
		confirm: true,
	})

	return {
		...slimPipeline(asRecord(response.data)),
		projectPath,
		auth: response.auth,
	}
}