JIRA Script runner failed to run groovy scripts

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.customfields.view.CustomFieldParams
import com.atlassian.jira.issue.link.RemoteIssueLinkBuilder
import com.atlassian.jira.issue.link.RemoteIssueLinkManager
import java.net.URL
import java.net.URLEncoder
import java.io.OutputStreamWriter
import groovy.json.JsonSlurper

def key = issue.getKey()

def connection
def jenkinsJob = "http://10.90.168.29:8081/job/HPA_Build/"
def slurper = new JsonSlurper()
def jsonResult
def buildJob
def buildNumber
def buildJobLink

connection = new URL(jenkinsJob + "api/json?tree=builds[number,actions[parameters[name,value]]]").openConnection()
jsonResult = slurper.parseText(connection.content.text)

buildJob = jsonResult.builds.find {build -> build.actions.parameters.getAt(0)?.getAt(1)?.value == key}
buildNumber = buildJob.number
buildJobLink = jenkinsJob + buildNumber + "/"

def linkService = ComponentAccessor.getComponent(RemoteIssueLinkManager.class)
def builder = new RemoteIssueLinkBuilder()
def link = builder.globalId("jenkinsBuildNumber=$buildNumber&issueKey=$key").
issueId=(issue.getId()).
relationship("is executed by").
applicationName("Jenkins").
applicationType("Build Server").
iconUrl("http://10.90.168.29:8081/favicon.ico").
iconTitle("Jenkins").
title("Job #$buildNumber").
url(buildJobLink).
summary("build").
statusIconUrl ("http://10.90.168.29:8081/images/16x16/terminal.png").
statusIconTitle("Building...").
statusIconLink(buildJobLink + "console").
build()

def linkResult = linkService.createRemoteIssueLink(link, jenkinsUser)

 

 

This is the script I'm running on script runner. I'm trying to attach jenkins job in to JIRA as a Issue Link. What am i doing wrong? Please check the error log

 

2015-01-22 09:46:16,623 http-bio-8080-exec-21 ERROR admin 586x859x1 1g7qgc5 127.0.0.1 /secure/WorkflowUIDispatcher.jspa
[scriptrunner.jira.workflow.ScriptWorkflowFunction] Script function failed on issue: HBR-272, actionId: 11, file: C:\T
rans_jira\link.groovy
javax.script.ScriptException: javax.script.ScriptException: java.lang.NullPointerException: Cannot get property 'number
' on null object
at com.onresolve.scriptrunner.runner.ScriptRunnerImpl.runFileAsScript(ScriptRunnerImpl.groovy:194)
at com.onresolve.scriptrunner.runner.ScriptRunner$runFileAsScript$0.call(Unknown Source)
at com.onresolve.scriptrunner.canned.jira.utils.CustomScriptDelegate.doScript(CustomScriptDelegate.groovy:47)
at com.onresolve.scriptrunner.canned.jira.utils.CustomScriptDelegate$doScript.call(Unknown Source)
at com.onresolve.scriptrunner.canned.jira.workflow.postfunctions.CustomScriptFunction.doScript(CustomScriptFunct
ion.gr
oovy:20)
Caused by: javax.script.ScriptException: java.lang.NullPointerException: Cannot get property 'number' on null object
... 5 more
Caused by: java.lang.NullPointerException: Cannot get property 'number' on null object
at Script9.run(Script9.groovy:26)
... 5 more

 

 

 

6 answers

This widget could not be displayed.

What code is on: Caused by: java.lang.NullPointerException: Cannot get property 'number' on null object at Script9.run(Script9.groovy:26) Can you format the code properly? Either use a code block or post to gist.github.com.

This widget could not be displayed.

import com.atlassian.jira.component.ComponentAccessor import com.atlassian.jira.issue.MutableIssue import com.atlassian.jira.issue.CustomFieldManager import com.atlassian.jira.issue.customfields.view.CustomFieldParams import com.atlassian.jira.issue.link.RemoteIssueLinkBuilder import com.atlassian.jira.issue.link.RemoteIssueLinkManager import java.net.URL import java.net.URLEncoder import java.io.OutputStreamWriter import groovy.json.JsonSlurper def key = issue.getKey() def customFieldManager = ComponentAccessor.getCustomFieldManager() def connection def jenkinsJob = "http://localhost:8081/HPA_Build/"; def slurper = new JsonSlurper() def jsonResult def buildJob def buildNumber def buildJobLink //job is out of the queue. find the one for our issue connection = new URL(jenkinsJob + "api/json?tree=builds[number,actions[parameters[name,value]]]").openConnection() jsonResult = slurper.parseText(connection.content.text) buildJob = jsonResult.builds.find {build -> build.actions.parameters.getAt(0)?.getAt(1)?.value == key} buildNumber = buildJob.number buildJobLink = jenkinsJob + buildNumber + "/" def linkService = ComponentAccessor.getComponent(RemoteIssueLinkManager.class) def jenkinsUser = ComponentAccessor.getUserManager().getUserByName("jenkins") def builder = new RemoteIssueLinkBuilder() def link = builder.globalId("jenkinsBuildNumber=$buildNumber&issueKey=$key"). issueId(issue.getId()). relationship("is executed by"). applicationName("Jenkins"). applicationType("Build Server"). iconUrl("http://localhost:8081/favicon.ico";). iconTitle("Jenkins"). title("Job #$buildNumber"). url(buildJobLink). summary("build-$domain-$application"). statusIconUrl("http://localhost:8081/images/16x16/terminal.png";). statusIconTitle("Building..."). statusIconLink(buildJobLink + "console"). build() def linkResult = linkService.createRemoteIssueLink(link, jenkinsUser)

This widget could not be displayed.

It's actually failling in the last line when its trying to create a link in JIRA

This widget could not be displayed.

I think it's failing in line 28 of your gist, that is, buildJob is null. Can you add some logging so you can see what it is.

This widget could not be displayed.

buildJob seems to be null so
You may add something like (~ line 25 of your gist):

jsonResult = slurper.parseText(connection.content.text)

// Take a look at this output by adding a log
log.debug jsonResult

// Take a look at the value of key
log.debug key

buildJob = jsonResult.builds.find {build -> build.actions.parameters.getAt(0)?.getAt(1)?.value == key}

Which values do you have in "jsonResult" and "key" before the line buildJob=....  ?

Suggest an answer

Log in or Sign up to answer
Community showcase
Published Aug 22, 2018 in Marketplace Apps

How a Marketplace app tech team is achieving gender diversity

Hello! My name is Genevieve Blanch, and I'm the Marketing Manager at RefinedWiki, creators of apps to give teams the tools to customize Atlassian platforms. Currently, 44% of the tech team at Re...

534 views 3 19
Read article

Atlassian User Groups

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

Find a group

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

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you