How do you create a Jira issue with code snippet in the description using REST API?

James Jung November 22, 2019

I am trying to create a Jira issue with a code snippet in the description.

I am new to the Atlassian REST API, so help would be much appreciated.

Also, a link to what kinds of formatting the description field can take for creating a Jira issue would be much appreciated.

2 answers

2 accepted

1 vote
Answer accepted
James Jung November 25, 2019

After many trials and errors and trying to see what the Jira UI was sending to its backend, I tried something like this as the JSON body with a POST call to "/rest/api/3/issue/" to create an issue:

{
"fields": {
"summary": "Some Summary",
"description": {
"version": 1,
"type": "doc",
"content": [
{
"type": "codeBlock",
"attrs": {
"language": "javascript"
},
"content": [
{
"type": "text",
"text": "some code here with proper escapes"
}
]
}
]
},
"project": {
"key": "some_key"
},
"priority": {
"id": "some_priority"
},
"issuetype": {
"name": "some_issue_type"
}
}
}

 

I'm not sure what the best way to do this is, so I think there needs to be more documentation and examples. Maybe I am looking at the wrong places, but the documentation looks a little sparse.

https://developer.atlassian.com/cloud/jira/platform/rest/v3/#api-rest-api-3-issue-post

Screen Shot 2019-11-25 at 11.42.26 AM.pngI would like to know what attributes I can mess around with for "description", but the "fields" attribute is just an object without any explanation of how to utilize the "description" attribute inside of it.

felix.weber June 3, 2021

Hi, maybe to late but just in case see also https://developer.atlassian.com/cloud/jira/platform/apis/document/structure/

 

as the description is a "paragraph" top-node - see also https://developer.atlassian.com/cloud/jira/platform/apis/document/nodes/paragraph/

 

but yes overall it is a hard to understand in my mind

0 votes
Answer accepted
Nir Haimov
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
November 24, 2019

Hi,

You need to send a POST call to:
https://your_jira_url/rest/api/2/issue/{issueKey}/comment

add header:
"Content-Type", "application/json"

your body data should be like:
{
    "body": "{code:java}// code placeholder{code}"
}

James Jung November 25, 2019

I want to create an issue and insert a description. The code block needs to be in the description. I saw something here:

https://confluence.atlassian.com/doc/code-block-macro-139390.html

 

I have tried inserting the code macro inside of the "description" text and it worked.

{
"fields": {
"summary": "Some Summary",
"description": "{code:title=Detailed Results|theme=FadeToGrey|linenumbers=true|language=javascript|firstline=0001|collapse=true}some code with proper escapes{code}",
"project": {
"key": "some_key"
},
"priority": {
"id": "some_priority"
},
"issuetype": {
"name": "some_issue_type"
}
}
}

 

My underlying question is where I can find documentation for this. What attributes and fields can I place inside the "description" attribute?

Suggest an answer

Log in or Sign up to answer