//native
async function get(auth: RT.Jira, path: string) {
const response = await fetch(`https://${auth.domain}.atlassian.net${path}`, {
headers: {
Authorization: `Basic ${btoa(`${auth.username}:${auth.password}`)}`,
Accept: "application/json",
},
})
if (!response.ok) {
throw new Error(`${response.status} ${await response.text()}`)
}
return await response.json()
}
export type DynSelect_project_id = string
// Resolver: the site's projects (first 100).
export async function project_id(auth: RT.Jira) {
const { values } = await get(
auth,
"/rest/api/3/project/search?maxResults=100"
)
return values.map((p: { id: string; key: string; name: string }) => ({
value: p.id,
label: `${p.name} (${p.key})`,
}))
}
export type DynSelect_issue_type_id = string
// Resolver: the issue types available in the chosen project.
export async function issue_type_id(auth: RT.Jira, project_id: string) {
if (!project_id) return []
const types = await get(
auth,
`/rest/api/3/issuetype/project?projectId=${project_id}`
)
return types.map((t: { id: string; name: string }) => ({
value: t.id,
label: t.name,
}))
}
// Wraps plain text in Atlassian Document Format, one paragraph per line.
function adf(text: string) {
return {
type: "doc",
version: 1,
content: text.split("\n").map((line) => ({
type: "paragraph",
content: line ? [{ type: "text", text: line }] : [],
})),
}
}
/**
* Create Issue
* Create an issue in a project. The description is plain text (converted to Atlassian Document Format, one paragraph per line); set any other field through `fields`, keyed by field ID, e.g. `{ "labels": ["bug"], "priority": { "name": "High" }, "assignee": { "accountId": "..." }, "parent": { "key": "PROJ-1" } }`.
*/
export async function main(
auth: RT.Jira,
project_id: DynSelect_project_id,
issue_type_id: DynSelect_issue_type_id,
summary: string,
description: string | undefined,
fields: { [key: string]: any } | undefined,
update: { [key: string]: any } | undefined
) {
const url = new URL(`https://${auth.domain}.atlassian.net/rest/api/3/issue`)
const body: { [key: string]: any } = {
fields: {
...fields,
project: { id: project_id },
issuetype: { id: issue_type_id },
summary,
},
}
if (description != null && description !== "") {
body.fields.description = adf(description)
}
if (update && Object.keys(update).length) {
body.update = update
}
const response = await fetch(url, {
method: "POST",
headers: {
Authorization: `Basic ${btoa(`${auth.username}:${auth.password}`)}`,
"Content-Type": "application/json",
Accept: "application/json",
},
body: JSON.stringify(body),
})
if (!response.ok) {
throw new Error(`${response.status} ${await response.text()}`)
}
return await response.json()
}
Submitted by hugo989 8 days ago