Create an Issue in Jira Cloud

This code snippet creates an issue in Jira Cloud based on a pre-defined project key and issue type.

This code should work for Jira Server and Data Center, too, since the APIs are similar.

js
import JiraCloud from './api/jira/cloud'; const JIRA_PROJECT_KEY = 'ISSUE'; const ISSUE_TYPE_NAME = 'Task'; export default async function (event: any, context: Context): Promise<void> { // Find the project to use based on pre-defined project key const project = await JiraCloud.Project.getProject({ projectIdOrKey: JIRA_PROJECT_KEY, }); // Find all the issue types for the given project const issueTypes = await JiraCloud.Issue.Type.getTypesForProject({ projectId: +(project.id ?? 0) // + sign converts the string to number }); // // Find the issue type to use based on pre-defined issue type name const issueType = issueTypes.find(it => it.name === ISSUE_TYPE_NAME); // Check if the issue type was found if (!issueType) { // If not, then throw an error throw Error('Issue type not found'); } // Create the issue const issue = await JiraCloud.Issue.createIssue({ body: { fields: { project: { id: project.id ?? '0' }, issuetype: { id: issueType.id ?? '0' }, summary: 'Hello World' } } }); // Log out created issue key console.log(`Issue created: ${issue.key}`); }