import { isConfirmed, isDryRun, normalizeProjectLocator, pickAuthInput, request } from '../core.ts' import { asRecord, readOptionalBoolean, readOptionalString, slimMergeRequest } from '../shape.ts' /** * Create a GitLab merge request. * * Pass `dryRun: true` to preview the request. Live creates require `confirm: true`. * * @example * import createGitlabMergeRequest from 'kody:@kody/gitlab/merge-requests/create' * const preview = await createGitlabMergeRequest({ * project: 'example-group/example-project', * source_branch: 'feature/example', * target_branch: 'main', * title: 'Example merge request', * dryRun: true, * }) */ export default async function createGitlabMergeRequest(params: Record = {}) { const auth = pickAuthInput(params) const { projectId, projectPath } = normalizeProjectLocator(params) const sourceBranch = readOptionalString(params.source_branch ?? params.sourceBranch) const targetBranch = readOptionalString(params.target_branch ?? params.targetBranch) const title = readOptionalString(params.title) if (!sourceBranch || !targetBranch || !title) { throw new Error('source_branch, target_branch, and title are required') } const body: Record = { source_branch: sourceBranch, target_branch: targetBranch, title, } const description = readOptionalString(params.description) if (description) body.description = description const labels = readOptionalString(params.labels) if (labels) body.labels = labels const removeSourceBranch = readOptionalBoolean( params.remove_source_branch ?? params.removeSourceBranch, ) if (removeSourceBranch !== undefined) body.remove_source_branch = removeSourceBranch const squash = readOptionalBoolean(params.squash) if (squash !== undefined) body.squash = squash if (isDryRun(params)) { return { dryRun: true, wouldCreate: true, projectPath, body, auth, } } if (!isConfirmed(params)) { throw new Error( `Creating a merge request in ${projectPath} requires dryRun: true (preview) or confirm: true (live write).`, ) } const response = await request>({ ...auth, method: 'POST', path: `/projects/${projectId}/merge_requests`, body, throwOnError: true, confirm: true, }) return { ...slimMergeRequest(asRecord(response.data)), projectPath, auth: response.auth, } }