How to create a jira issue from postfunction using groovy through rest API

Vineela Durbha April 19, 2019

I am trying to create an issue of a project from another project post function. I am trying to create it through rest api as below

import groovy.json.StreamingJsonBuilder
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import java.text.SimpleDateFormat
import java.text.DateFormat
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.comments.CommentManager

def baseURL = "http://localhost:8081/rest/api/2/issue/";

// localhost:8081 it`s the target instance
def authString = "admin:admin".getBytes().encodeBase64().toString();

def body_req = [
"""
{ "fields":
{
"project":
{
"key": "XXXX"
},
"summary": "My Issue.",
"description": "Creating of an issue using project keys and issue type names using the REST API",
"PType": "CCCC",
"TSW": "XXXX",
"reporter":
{
"name": "sdurbha"
}
"issuetype":
{
"name": "Bug"
}
}

}
"""
]

URL url = new URL(baseURL);
HttpURLConnection connection=(HttpURLConnection)url.openConnection();
connection.setRequestProperty( "Authorization", "Basic ${authString}" );
connection.requestMethod = "POST";
connection.doOutput = true;
connection.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
connection.outputStream.withWriter("UTF-8") { new StreamingJsonBuilder(it, body_req) }
connection.connect();

 

I am neither getting an error nor the issue is being created. Can someone help me on this

3 answers

0 votes
zaharovvv_suek_ru
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
May 27, 2020

1. I have the same problem, so I decided to create new project in Jira. And issue started to be created. The reason was that my json object to create issue did not have fields which are marked as **required**. Read more about required fields here. So try to create a new project without required fieds.

2. Then your "body_req" can be changed to:  

def body_req = '''{
"fields":
{
"project" : { "key" : "E1" },
"issuetype" : { "name" : "Task" },
"summary" : "FooSummary",
"description" : "FooDescription"
"reporter": {"name": "yourJiraLogin"}
}
}'''

 3. In addition, try to see actual format of json for your Jira version. Sometimes, some fields have different formats. For example, component field should be put into square brackets:

"components": [
            {
                "id": "10000"
            }
        ]

In addition, you can read desired info about fields from the docs.

For example, you can get a list of all the projects from the following endpoint:

    http://localhost:8080/rest/api/2/project

After finding the project you want, you will need its ID or key. You will use this information to retrieve the issue types in this project:

http://localhost:8080/rest/api/2/issue/createmeta/{projectIdOrKey}/issuetypes

Once you find the appropriate issue type to use, you need to get the fields under this issue type. This endpoint will list the usable fields under the issue type.

http://localhost:8080/rest/api/2/issue/createmeta/{projectIdOrKey}/issuetypes/{issueTypeId}

 

My full code looks like this:

import groovy.json.StreamingJsonBuilder
import groovy.json.JsonBuilder
import org.apache.log4j.Logger
import org.apache.log4j.Level

def log = Logger.getLogger("com.acme.CreateSubtask")
log.setLevel(Level.DEBUG)


def authString = "adminUser:adminPassword".getBytes().encodeBase64().toString()
def body_req = '''{
"fields":
{
"project" : { "key" : "S" },
"issuetype" : { "name" : "Task" },
"summary" : "FooSummary",
"description" : "FooDescription"
}
}'''

def connection = new URL("https://yourjiraaddress/rest/api/2/issue/").openConnection() as HttpURLConnection
connection.setRequestMethod( "POST" )
connection.setRequestProperty( "Authorization", "Basic ${authString}" )
log.debug("Hello, authString is ${authString}")
connection.doOutput = true
connection.setRequestProperty("Content-Type", "application/json;charset=UTF-8")
connection.getOutputStream().write(body_req.getBytes("UTF-8"))
connection.connect()
def postRC = connection.getResponseCode();
println(postRC);
if(postRC.equals(200)) {
println(connection.getInputStream().getText());
}
log.debug("The response is ${connection.getInputStream().getText()}")
0 votes
Antoine Berry
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
April 19, 2019

Hi @Vineela Durbha ,

Why use Rest-API over standard groovy script ?

0 votes
Tarun Sapra
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
April 19, 2019

Hello @Vineela Durbha 

Why are you using REST APIs if the target project in which you want to create an issue exists in the same instance?

You can very easily use the Jira Java API to easily create an issue in another project in the same Jira instance. Generally , REST APIs is used to create issues in remote instances. 

Using Jira Java API, it's very simple as described here 

https://developer.atlassian.com/server/jira/platform/performing-issue-operations/#creating-a-new-issue

Suggest an answer

Log in or Sign up to answer