import { isConfirmed, isDryRun, normalizePipelineLocator, pickAuthInput, request } from '../core.ts'
import { asRecord, slimPipeline } from '../shape.ts'
/**
* Cancel a GitLab pipeline.
*
* Pass `dryRun: true` to preview the request. Live cancels require `confirm: true`.
*
* @example
* import cancelGitlabPipeline from 'kody:@kody/gitlab/pipelines/cancel'
* const preview = await cancelGitlabPipeline({
* project: 'example-group/example-project',
* pipelineId: 88,
* dryRun: true,
* })
*/
export default async function cancelGitlabPipeline(params: Record<string, unknown> = {}) {
const auth = pickAuthInput(params)
const { projectId, projectPath, pipelineId } = normalizePipelineLocator(params)
if (isDryRun(params)) {
return {
dryRun: true,
wouldCancel: true,
projectPath,
pipelineId,
auth,
}
}
if (!isConfirmed(params)) {
throw new Error(
`Canceling 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}/cancel`,
throwOnError: true,
confirm: true,
})
return {
...slimPipeline(asRecord(response.data)),
projectPath,
auth: response.auth,
}
}