import { isConfirmed, isDryRun, normalizeProjectLocator, pickAuthInput, request } from '../core.ts'
import { asRecord, readOptionalBoolean, readOptionalString, slimIssue } from '../shape.ts'
/**
* Create a GitLab issue.
*
* Pass `dryRun: true` to preview the request. Live creates require `confirm: true`.
*
* @example
* import createGitlabIssue from 'kody:@kody/gitlab/issues/create'
* const preview = await createGitlabIssue({
* project: 'example-group/example-project',
* title: 'Example issue',
* dryRun: true,
* })
*/
export default async function createGitlabIssue(params: Record<string, unknown> = {}) {
const auth = pickAuthInput(params)
const { projectId, projectPath } = normalizeProjectLocator(params)
const title = readOptionalString(params.title)
if (!title) {
throw new Error('title is required')
}
const body: Record<string, unknown> = { title }
const description = readOptionalString(params.description)
if (description) body.description = description
const labels = readOptionalString(params.labels)
if (labels) body.labels = labels
const confidential = readOptionalBoolean(params.confidential)
if (confidential !== undefined) body.confidential = confidential
if (params.assignee_ids !== undefined) body.assignee_ids = params.assignee_ids
if (params.milestone_id !== undefined) body.milestone_id = params.milestone_id
if (isDryRun(params)) {
return {
dryRun: true,
wouldCreate: true,
projectPath,
body,
auth,
}
}
if (!isConfirmed(params)) {
throw new Error(
`Creating an issue in ${projectPath} requires dryRun: true (preview) or confirm: true (live write).`,
)
}
const response = await request<Record<string, unknown>>({
...auth,
method: 'POST',
path: `/projects/${projectId}/issues`,
body,
throwOnError: true,
confirm: true,
})
return {
...slimIssue(asRecord(response.data)),
projectPath,
auth: response.auth,
}
}