Create Jira Issues of different types
Asked by Jack Webster:

Hi all, I'm new to Jira/Confluence and trying to come up with a good way to quickly create Jira issues from a confluence document.

I want to have a table in confluence like the one shown below where I can generate EPICs, STORYs and SUBTASKS based on the number from the Table much like how the Create Multiple Issues works.

Has anyone had success with anything similar?

| **#** | **Requirement** |    **Description**     |
|-------|-----------------|------------------------|
| 1     | EPIC            | Description of EPIC    |
| 1.1   | STORY           | Description of STORY   |
| 1.2   | STORY           | Description of STORY   |
| 1.2.1 | SUBTASK         | Description of SUBTASK |
| 2     | EPIC            | Description of EPIC    |


Answers:
author: Benno Baumgartner
body: Hi Jack, you might be able to do something like that with our Addon [JavaScript Button for Confluence](https://marketplace.atlassian.com/apps/1231339/javascript-button-for-confluence):

![Screen Shot 2023-11-05 at 13.15.24.png](https://community.atlassian.com/t5/image/serverpage/image-id/290128i7F80A8D1E6C78C2D/image-size/large?v=v2&px=999 "Screen Shot 2023-11-05 at 13.15.24.png")

Using the following Code, which is not trivial but it works:

```
async function createIssues(page, projectKey) {

  let pageBody = JSON.parse(page.body.atlas_doc_format.value);

  let table = pageBody.content[0];

  let rows = table.content;

?

  var result = "Created issue ids: ";

  //stack of parents

  var parents = [];

  parents.push(null);

  for (let i = 1; i < rows.length; i++) {

    let number = rows[i].content[0].content[0].content[0].text;

    let requirement = rows[i].content[1].content[0].content[0].text;

    let description = rows[i].content[2].content[0].content[0].text;

    

    let numberOfDots = number.split(".").length - 1;

    while (parents.length > numberOfDots +?1) {

      parents.pop();

    }

    var parentKey = parents[parents.length -1];

    

    let newKey = await createIssue(projectKey, requirement, number, description, parentKey);

    parents.push(newKey);

    

    result += `${number} -> ${newKey} `;

  }

  

  return result;

}

?

//see https://domasys.io/javascript-button/tutorial.html

async function createIssue(projectKey, type, summary, description, parentKey) {

  const projectId = await getProjectId(projectKey);

  const issueTypeId = await getTypeId(projectId, type);

?

  var bodyData = {

    fields: {

      description: {

        content: [

          {

            content: [

              {

                text: description,

                type: "text"

              }

            ],

            type: "paragraph"

          }

        ],

        type: "doc",

        version: 1

      },

      issuetype: {

        id: issueTypeId

      },

      parent: {

        key: parentKey

      },

      project: {

        id: projectId

      },

      summary: summary

    }

  };

  

  const response = await api.asUser().requestJira(route`/rest/api/3/issue`, {

    method: 'POST',

    headers: {

      'Accept': 'application/json',

      'Content-Type': 'application/json'

    },

    body: JSON.stringify(bodyData)

  });

  

  console.log(`Response: ${response.status} ${response.statusText}`);

  const result = await response.json();

?

  return result.key;

}

?

//this is very inefficient for large tables because we do that for every row

//you could hard code your issue type ids

async function getTypeId(projectKey, typeName) {

  const response = await api.asUser().requestJira(route`/rest/api/3/issuetype/project?projectId=10000`, {

    headers: {

      'Accept': 'application/json'

    }

  });

  console.log(`Response: ${response.status} ${response.statusText}`);

  

  const issueTypes = await response.json();

  console.log(issueTypes);

  return issueTypes.find(element => element['name'] == typeName)['id'];

}

?

?

//this is very inefficient for large tables because we do that for every row

//you could hard code your project id

async function getProjectId(projectKey) {

  const response = await api.asUser().requestJira(route`/rest/api/3/project`, {

    headers: {

      'Accept': 'application/json'

    }

  });

  console.log(`Response: ${response.status} ${response.statusText}`);

?

  const projects = await response.json();

  console.log(projects);

?

  return projects.find(element => element['key'] == projectKey)['id'];

}

```

Best regards Benno.


author: Jack Brickey
body: Hi Jack, welcome to the community.

there isn't a way out of the box to achieve this automatically based on some number scheme. If you are not aware you can manually create Jira issues in Confluence simply by highlighting text and clicking the icon that pops up.


Tags: cloud

confluence-cloud