Jira integration - REST POST not generating a response.

User667175
Giga Expert

Hi all, 

I've recently been implementing a version of John Andersen's JIRA REST api POC and I started to build a similar app myself, keeping most of it locked down to a scope, although some of it has had to be moved to Global. 

I'm having an issue where I've created a UI action on an incident that POSTs to JIRA, however I'm not receiving a valid response and am instead getting an error: 

  •  RESPONSE: {"errorMessages":["No content to map to Object due to end of input"]}

I can POST from the Outbound Rest Message test and get a valid response using exactly the same data. I've also use Postman's Mock server and received a valid response from there using the UI action. 

Below are my UI actions and Script include (pretty much taken straight from the Link above).

Script Include:  (mainly focus on the createIssue as I've not even changed a lot of the other parts due to not having a valid connection)

var JiraIntegration = Class.create();
JiraIntegration.prototype = {
	
	LOGGER_SOURCE: "Jira Integration",
    CORRELATION_DISPLAY: "Jira Integration",
	
    initialize: function() {
		this.enabled = gs.getProperty("x_cds_jira.enable_integration"); // is it enabled? 
		this.verbose = gs.getProperty("x_cds_jira.jira_debug");  // debug? 
		this.base = gs.getProperty("x_cds_jira.jira_base"); // base instance. 
		this.username = gs.getProperty("x_cds_jira.jira_username"); // username
		this.password = gs.getProperty("x_cds_jira.jira_password"); // password
		this.sys_id = gs.getProperty("x_cds_jira.sys_id_field"); 
	},
	
	debug : function(msg){
		if(this.verbose=="true") {
			gs.log(msg, this.LOGGER_SOURCE);
		}
	},
	
	// The create issue will run from the "Create Jira Record" UI action that is present on the incident form when the 3rd Party supplier is JIRA. 
	// We pass an object into this function using the createIssue(issue) syntax.
	
	 createIssue : function(issue){    
		 
		//this.debug('Enabled? ' + this.enabled + '\n' + 'Base: ' + this.base + "     " + this.username);
		
        var json = new JSON();
        var body = json.encode(issue);
        var r = new sn_ws.RESTMessageV2('Jira Issue Global', 'POST');
        r.setBasicAuth(this.username, this.password);
        this.debug("BODY to be Submitted: \n" + body);
        r.setXMLParameter("issuebody", body);
        r.setStringParameter("base_endpoint", this.base);
		var res = this._submitBodyTypeRequest(r, true);
        return res;
    },
	
	//modify a JIRA issue passing the project key into JIRA. 
	
	 modifyIssue : function( issue, key ){
        var json = new JSON();
        var body = json.encode(issue);
        var r = new RESTMessage('Jira Issue', 'PUT');
        r.setBasicAuth(this.username, this.password);
        this.debug("BODY to be Submitted: \n" + body);
        r.setXMLParameter('issuebody', body);
        r.setStringParameter('base_endpoint', this.base);
        r.setStringParameter("issueKey", key);
        var res = this._submitBodyTypeRequest(r, false);
        return res;
    },
	
	// add a comment to JIRA updating the JIRA card.  
	
	   addComment : function( comment, key ){
        var json = new JSON();
        var body = json.encode(comment);
        var r = new RESTMessage('Jira Issue Comment', 'POST');
        r.setBasicAuth(this.username, this.password);
        this.debug("Comment BODY to be Submitted: \n" + body);
        r.setXMLParameter('commentBody', body);
        r.setStringParameter('base_endpoint', this.base);
        r.setStringParameter("issueKey", key);
        resBody = this._submitBodyTypeRequest(r, true);
        
        this.debug("Handling the response data from Jira");
        if(resBody){
            if(this.verbose=="true"){
                JSUtil.logObject(resBody);
            }
        }
    },
	
    _submitBodyTypeRequest: function(r, waitForResponse){
        if(waitForResponse==null){
            waitForResponse = true;
        }
        if(waitForResponse){                              //if waitForResponse is true then do this. 
            var response = this._executeRest(r);
            if(!response){
                return 'No response';                          // curently always returning no response. 
            }
            this.debug("RESPONSE: \n"+ response.getBody());
            var parser = new JSONParser();
            var jiraIssue = parser.parse(response.getBody());
            
            return jiraIssue;
			
        } else {
            r.execute();
        }
    },
   
	_executeRest : function (r){
            this.debug("Executing request synchronously and directly");
            return r.execute();
    },
    
	
	getJiraPriority: function(p){
        //For Now the ID's match up well with OOB values, so keeping them the same ID's
        var jp = p;
        this.debug("Jira priority equivalent to SNC Priority "+p+" is: "+jp);
        return jp;
    },
	
	
    type: 'JiraIntegration'
};

UI Action

var j = new JiraIntegration();
j.debug("New Incident of category 'Jira Issue'...create a JIRA issue");

// here we create the object that we're going to push into JIRA. 

var issue = {};
issue.fields = {};
issue.fields.project ={};
issue.fields.project.key = gs.getProperty("x_cds_jira.jira_project"); // this will hold the board name where we push the card onto the backlog. 
issue.fields.summary = "" + current.short_description;
issue.fields.description = "" + current.description;
issue.fields.issuetype = {};
issue.fields.issuetype.name = "Bug";
issue.fields.customfield_10033=""+current.number;
issue.fields['customfield_' + j.sys_id] = ".service-now:" + current.sys_id;
issue.fields.priority = {};
issue.fields.priority.id = j.getJiraPriority(""+current.priority);

if (j.verbose == 'true') {
	JSUtil.logObject(issue);
}

j.debug("Jira fields set.  Ready to create issue in Jira");
var jiraIssue = j.createIssue(issue);
j.debug("Jira issue created: " + jiraIssue.self);
current.correlation_id = jiraIssue.key;

var wn = "A corresponding JIRA Issue has been created:\n";
wn += "ID: "+jiraIssue.id+"\n";
wn += "Issue Number: "+jiraIssue.key+"\n";
wn += "Issue URL: "+gs.getProperty('x_cds_jira.jira_base')+"/browse/"+jiraIssue.key+"\n";
current.work_notes = wn;
current.update();

gs.addInfoMessage('Jira Record Created' + jiraIssue.self);
action.setRedirectURL(current);

Any help will be massively appreciated,

Thanks

Mike.

3 REPLIES 3

Priyanka136
Mega Guru

Hi Mike,

Please follow the below steps might be it will solve your issue.

1) Go to link - https://www.atlassian.com/software/jira

    Try it Free (Trial for 7 days only) - Go to Jira Software, Jira Service Desk - Create Your Jira account

2) Refer this link also :-https://developer.atlassian.com/cloud/jira/platform/jsapi/request/

https://developer.atlassian.com/server/jira/platform/rest-apis/

https://developer.atlassian.com/cloud/jira/platform/rest/v3/#api-api-3-universal-avatar-type-type-ow...

4) In Snow Instance , Go to - System Web Services - OutBound - Rest Message

5) New - Endpoint = https://<your site name>.atlassian.net/rest/api/3/user

6) Authentication Type - Basic Auth Profile

7) HTTP Request - HTTP Header - Name = Content-Type, Value = application/json

😎 Save - Go to HTTP Methods - Default Get already created - Go to New

9) Http Methods = GET, POST, PUT, PATCH, DELETE

10) If you are creating the user in jira from snow so for that use POST method

11) In HTTP Query Parameters - Give the Content (json format) - save

12) click on auto-generate variables

13) Copy Preview script usage code - paste it in Business Rule ( user table, before , insert) - save

14) Go to - User table - Fill the fields

15) Check it in Jira , whether the user is created or not.

Please let me know if you have any question.

I hope it works, Please mark it Correct or Helpful based on impact...!!!!!!

Warm Regards,

Priyanka

 

User667175
Giga Expert

Further to this, the object that is being built formats as the below: 

{  
   "fields":{  
      "customfield_10031":"[company].service-now:8d2c88e3db1df78060319ee3db9619c0",
      "customfield_10033":"INC0001032",
      "description":"Description 2",
      "issuetype":{  
         "name":"Bug"
      },
      "priority":{  
         "id":"4"
      },
      "project":{  
         "key":"STES"
      },
      "summary":"Short description"
   }
}

Were you able to fix this issue?