Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

getting error messages in script runner for some methods and classes

Sanil Panicker April 23, 2019

Hi, 

we just installed evaluation version of script runner for our Jira Data Center version 7.12.1 and installed version 5.5.3 version of scriptrunner.

I am developing a rest API call with POST function to create a record in a different application based on the issue type we have in JIRA.

I tried different Options but always get static type checking errors in Scriptrunner, could you please let me know what do i need to do to resolve this?

the code is as below:

 

import groovy.json.JsonSlurper;
import groovy.json.StreamingJsonBuilder;
import com.atlassian.jira.ComponentManager;
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.issue.IssueManager;
import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.MutableIssue
import org.apache.commons.codec.binary.Base64;

def user_id = "*******";
def issueID = "";

IssueManager issueManager = ComponentAccessor.getIssueManager();
MutableIssue Issue = issueManager.getIssueObject("DEPA-88");
//issueID = Issue.getKey();
issueID = Issue.getId();

def issueType = Issue.getIssueType().name;
def textToTranslate = "";
def targetLanguage = "";
def originalLanguage ="";
def AcceptanceCriteria = "";
def Summary = "";
def requestMethod = "POST";
def user = "*********";
def passwd = "*******";


AcceptanceCriteria = "Testing Acceptance Criteria from JIRA 4-23-2019"

Summary = "Summary from JIRA with ID as " + issueID;

def body_req = [
"user": user_id,
"jiraTicket":issueID,
"route":"/resources/text",
"description":AcceptanceCriteria,
"state":1,
"short_description":Summary,
"sys_updated_by":user_id
]

def authString = user_id + ":" + passwd;

byte[] authEncBytes = Base64.encodeBase64(authString.getBytes());

String authStringEnc = new String(authEncBytes);

//def baseURL = "http://production.******.com/api/translation";
def baseURL = "https://tfsdev.service-now.com/api/now/table/incident";
URL url;

url = new URL(baseURL);

URLConnection connection = url.openConnection();
//connection.requestMethod = "POST"; ///***   Gets error at connection.requestMethod = "POST"; ****/// 
connection.setRequestMethod("POST"); ///***   Gets error at onnection.setRequestMethod("POST"); ****/// 
connection.setRequestProperty("Authorization", "Basic " + authStringEnc);
connection.doOutput = true
connection.setRequestProperty("Content-Type", "application/json;charset=UTF-8")
connection.outputStream.withWriter("UTF-8") { new StreamingJsonBuilder(it, body_req) }
connection.connect();


println("url: " + url);
println("Content:" + connection.getContent());
println("ResponseCode:" + connection.getResponseCode());   ///***   Gets error at connection.getResponseCode()) ****///
println("getResponseMessage:" + connection.getResponseMessage()); ///***   Gets error at connection.getResponseMessage() ****///

1 answer

0 votes
PD Sheehan
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.
April 26, 2019

For better or for worse, I always ignore static type checking errors.

Groovy is a dynamically (some have said "optionally" typed) language. Static type checking can help identify errors, but don't mean your script is bad.

Developer coming from pure java don't like those and tend to statically type everything. But that makes a scrip much bigger and harder to read and defeats that's, in my opinion, the beauty of groovy.

 

Here is how I would write your script:

import com.atlassian.jira.component.ComponentAccessor
import groovyx.net.http.ContentType
import groovyx.net.http.HTTPBuilder
import groovyx.net.http.Method

def user_id = "*******"
def issue = ComponentAccessor.issueManager.getIssueObject("DEPA-88")
def issueType = issue.issueType.name
def textToTranslate = ""
def targetLanguage = ""
def originalLanguage = ""
def user = "*********"
def passwd = "*******"

def acceptanceCriteria = "Testing Acceptance Criteria from JIRA 4-23-2019"
def summary = "Summary from JIRA with ID as ${issue.id}"

def body_req = [
user : user_id,
jiraTicket : issue.id,
route : "/resources/text",
description : acceptanceCriteria,
state : 1,
short_description: summary,
sys_updated_by : user_id
]

def baseURL = "https://tfsdev.service-now.com/api/now/table/incident"
def httpBuilder = new HTTPBuilder(baseURL)

def jsonResponse
httpBuilder.request(Method.POST, ContentType.JSON) {
headers.Authorization = "Basic ${"user_id:$passwd".bytes.encodeBase64().toString()}"
body = body_req
response.success = {resp, json ->
jsonResponse = json
}
response.failure = {resp, json ->
log.error "$resp.statusLine"
}
}
return jsonResponse

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events