It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

Updating parent link of portfolio through Groovy Script by calling Rest API. Edited

SWATI MISHRA Sep 27, 2018

We are trying to update the Parent Link of Portfolio by using the Rest API. We want to set the Parent Link value to the Issue Id (which is associated to the current issue through Issue Link having "is a child of" relationship).

Below is the script

import org.apache.log4j.Category
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.project.Project
import com.atlassian.jira.issue.link.IssueLink
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.bc.issue.IssueService
import com.atlassian.jira.bc.issue.IssueService.TransitionValidationResult
import com.atlassian.crowd.embedded.api.User
import com.atlassian.jira.issue.MutableIssue;
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.issue.link.IssueLinkManager
import com.atlassian.jira.issue.link.IssueLinkType
import com.atlassian.jira.issue.link.IssueLinkTypeManager
import com.atlassian.jira.bc.issue.search.SearchService;
import com.atlassian.jira.web.bean.PagerFilter;
import com.atlassian.jira.workflow.WorkflowTransitionUtil;
import com.atlassian.jira.workflow.WorkflowTransitionUtilImpl;
import com.atlassian.jira.util.JiraUtils;
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.issue.util.IssueChangeHolder
import com.atlassian.jira.issue.ModifiedValue
import groovyx.net.http.RESTClient
import groovyx.net.http.HttpResponseException
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import groovy.json.*

String results = "";
def valid = false;

List<IssueLink> allOutIssueLink = ComponentAccessor.getIssueLinkManager().getInwardLinks(issue.getId());
log.info("allOutIssueLink...."+allOutIssueLink)
for (Iterator<IssueLink> outIterator = allOutIssueLink.iterator(); outIterator.hasNext();) {
IssueLink issueLink = (IssueLink) outIterator.next();
log.info("issueLink.........."+issueLink)
def linkedIssue = issueLink.getSourceObject()
log.info("linkedIssue......"+linkedIssue)
String type = linkedIssue.getIssueType().getName();
// if(type.contains("Change"))
//{
valid = true;
//results += linkedIssue.getKey() + "<br />"
results = linkedIssue.getKey()
// }
}
log.info("results in Inwards is:" + results)


//Trying to set value in parent link

//def newParentLink = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Parent Link") // here is ID custom field what u have to update.
//def jsonObj = new JsonSlurper().parseText('{ "fields": { "customfield_23280": "'+newParentLink+'"} }') //customfield_10115 is Parent Link ID
def jsonObj = new JsonSlurper().parseText('{ "fields": { "customfield_10115": "'+results+'"} }') //customfield_10115 is Parent Link ID
log.info("jsonObj.............." + jsonObj)
def parentLink = ComponentAccessor.getCustomFieldManager().getCustomFieldObject(10115L) // id parent link custom field
log.info("parentLink.........." + parentLink)

def slurper = new groovy.json.JsonSlurper()
def jsonEx = slurper.parseText('{"person":{"name":"Guillaume","age":33,"pets":["dog","cat"]}}')
log.info("jsonEx....." + jsonEx)

def userName = "admin"
def password = "admin"
def fullAddress = "http://localhost:8044/jira/rest/api/2/issue/${issue.key}"
def authString = "${userName}:${password}".getBytes().encodeBase64().toString()
log.info("fullAddress" + fullAddress)
log.info("authString..." + authString)


def client = new RESTClient(fullAddress)
try{
//necessary to overwrite the value of custom field, via rest only update if custom field have value null.
log.info("Before Update")
parentLink.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(parentLink), null),new DefaultIssueChangeHolder())
log.info("After Update")
client.put(path: fullAddress,
//contentType: groovyx.net.http.ContentType.JSON,
contentType: 'application/json',
body: jsonObj,
headers: [Accept: 'application/json', Authorization: "Basic ${authString}"]
//headers: [Accept: 'application/json']
)
log.info("After put")
log.info("issue.getCustomFieldValue(parentLink)...." + issue.getCustomFieldValue(parentLink))
}catch(final HttpResponseException e){
log.info e
}

/*
def customFieldManager = ComponentAccessor.getCustomFieldManager();
def parentLink = customFieldManager.getCustomFieldObjectByName("Parent Link");
log.info("parent link "+ parentLink)

issue.setCustomFieldValue(parentLink, results)*/

We are getting the Below Exceptions :

 

Error parsing 'text/html;charset=UTF-8' response
groovy.json.JsonException: Unable to determine the current character, it is not a string, number, array, or object

The current character read is '<' with an int value of 60
Unable to determine the current character, it is not a string, number, array, or object
line number 11
index number 10
<html>
^
at groovy.json.internal.JsonParserCharArray.decodeValueInternal(JsonParserCharArray.java:207)
at groovy.json.internal.JsonParserCharArray.decodeValue(JsonParserCharArray.java:158)
at groovy.json.internal.JsonParserCharArray.decodeFromChars(JsonParserCharArray.java:46)
at groovy.json.internal.JsonParserCharArray.parse(JsonParserCharArray.java:386)
at groovy.json.internal.BaseJsonParser.parse(BaseJsonParser.java:125)
at groovy.json.JsonSlurper.parse(JsonSlurper.java:221)
at groovyx.net.http.ParserRegistry.parseJSON(ParserRegistry.java:280)
... 3 filtered
at java.lang.reflect.Method.invoke(Method.java:498)
at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:93)
at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:325)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1212)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1079)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1021)
at groovy.lang.Closure.call(Closure.java:426)
at groovy.lang.Closure.call(Closure.java:442)
at groovyx.net.http.HTTPBuilder.parseResponse(HTTPBuilder.java:560)
at groovyx.net.http.HTTPBuilder$1.handleResponse(HTTPBuilder.java:489)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:223)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:165)
at groovyx.net.http.HTTPBuilder.doRequest(HTTPBuilder.java:515)
at groovyx.net.http.RESTClient.put(RESTClient.java:163)
at groovyx.net.http.RESTClient$put.call(Unknown Source)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:125)
at Script145.run(Script145.groovy:78)
at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:352)
at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:153)
at javax.script.AbstractScriptEngine.eval(AbstractScriptEngine.java:233)
at javax.script.ScriptEngine$eval.call(Unknown Source)
at com.onresolve.scriptrunner.runner.ScriptRunnerImpl.runScriptAndGetContext(ScriptRunnerImpl.groovy:159)
at com.onresolve.scriptrunner.runner.ScriptRunner$runScriptAndGetContext$7.callCurrent(Unknown Source)
at com.onresolve.scriptrunner.runner.ScriptRunnerImpl.runStringAsScript(ScriptRunnerImpl.groovy:148)
at com.onresolve.scriptrunner.runner.ScriptRunner$runStringAsScript$6.call(Unknown Source)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:133)
at com.onresolve.scriptrunner.canned.jira.utils.CustomScriptDelegate.doScript(CustomScriptDelegate.groovy:72)
at com.onresolve.scriptrunner.canned.jira.utils.CustomScriptDelegate$doScript$2.call(Unknown Source)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:125)
at com.onresolve.scriptrunner.canned.jira.workflow.postfunctions.CustomScriptFunction.doScript(CustomScriptFunction.groovy:46)
at com.onresolve.scriptrunner.canned.CannedScript$doScript$4.call(Unknown Source)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)
at com.onresolve.scriptrunner.canned.CannedScript$doScript$0.call(Unknown Source)
at com.onresolve.scriptrunner.runner.ScriptRunnerImpl.validateAndRunCannedInternal(ScriptRunnerImpl.groovy:445)
... 2 filtered
at java.lang.reflect.Method.invoke(Method.java:498)
at org.codehaus.groovy.runtime.callsite.PogoMetaMethodSite$PogoCachedMethodSiteNoUnwrapNoCoerce.invoke(PogoMetaMethodSite.java:210)
at org.codehaus.groovy.runtime.callsite.PogoMetaMethodSite.callCurrent(PogoMetaMethodSite.java:59)
at com.onresolve.scriptrunner.runner.ScriptRunnerImpl.runCanned(ScriptRunnerImpl.groovy:386)
at com.onresolve.scriptrunner.runner.ScriptRunner$runCanned$5.call(Unknown Source)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:133)
at com.onresolve.scriptrunner.jira.workflow.ScriptWorkflowFunction.run(ScriptWorkflowFunction.groovy:178)
at com.onresolve.scriptrunner.jira.workflow.ScriptWorkflowFunction$run$1.callCurrent(Unknown Source)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallCurrent(CallSiteArray.java:52)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:154)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:174)
at com.onresolve.scriptrunner.jira.workflow.ScriptWorkflowFunction.execute(ScriptWorkflowFunction.groovy:102)
at com.opensymphony.workflow.AbstractWorkflow.executeFunction(AbstractWorkflow.java:1014)
at com.opensymphony.workflow.AbstractWorkflow.transitionWorkflow(AbstractWorkflow.java:1407)
at com.opensymphony.workflow.AbstractWorkflow.doAction(AbstractWorkflow.java:557)
at com.atlassian.jira.workflow.OSWorkflowManager.doWorkflowActionInsideTxn(OSWorkflowManager.java:830)
at com.atlassian.jira.workflow.OSWorkflowManager.doWorkflowAction(OSWorkflowManager.java:787)
at com.atlassian.jira.bc.issue.DefaultIssueService.transition(DefaultIssueService.java:492)
at com.atlassian.jira.web.action.workflow.SimpleWorkflowAction.doExecute(SimpleWorkflowAction.java:28)
... 1 filtered
at com.atlassian.jira.action.JiraActionSupport.execute(JiraActionSupport.java:63)
... 7 filtered
at javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
... 48 filtered
at com.atlassian.greenhopper.jira.filters.ClassicBoardRouter.doFilter(ClassicBoardRouter.java:62)
... 12 filtered
at com.atlassian.web.servlet.plugin.request.RedirectInterceptingFilter.doFilter(RedirectInterceptingFilter.java:21)
... 20 filtered
at com.intenso.jira.contentinjection.filter.ContentInjectionFilter.doFilter(ContentInjectionFilter.java:71)
... 30 filtered
at com.xiplink.jira.git.action.GitThreadCacheDestroyFilter.doFilter(GitThreadCacheDestroyFilter.java:35)
... 14 filtered
at com.atlassian.jira.security.JiraSecurityFilter.lambda$doFilter$0(JiraSecurityFilter.java:66)
... 1 filtered
at com.atlassian.jira.security.JiraSecurityFilter.doFilter(JiraSecurityFilter.java:64)
... 39 filtered
at com.atlassian.jira.servermetrics.CorrelationIdPopulatorFilter.doFilter(CorrelationIdPopulatorFilter.java:30)
... 5 filtered
at com.valiantys.jira.plugins.sql.service.servletcontext.ContextListenerServletFilter.doFilter(ContextListenerServletFilter.java:24)
... 8 filtered
at com.atlassian.web.servlet.plugin.request.RedirectInterceptingFilter.doFilter(RedirectInterceptingFilter.java:21)
... 4 filtered
at com.atlassian.web.servlet.plugin.LocationCleanerFilter.doFilter(LocationCleanerFilter.java:36)
... 26 filtered
at com.atlassian.jira.servermetrics.MetricsCollectorFilter.doFilter(MetricsCollectorFilter.java:25)
... 24 filtered
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:745)

 

1 answer

Suggest an answer

Log in or Sign up to answer
This widget could not be displayed.
This widget could not be displayed.
Community showcase
Published in Marketplace Apps & Integrations

Webinar invitation: Generate custom Jira Cloud PDF reports and exports

Jira may be a staple part of your workday, but your colleagues, customers, business partners or an auditor might need information presented in a different format, that is easy to access and read. And...

64 views 0 1
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you