Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,293,914
Community Members
 
Community Events
165
Community Groups

Groovy error (working with API)

Edited

Hello everyone. I have a simple postfunction that creates a channel in messanger (in our case Cisco Spark) , adds members etc. But I want to create a room with a name and ID of issue. For example , TEST-5521 . For this purposes I use issue.getKey() (that return string). But I see 

No such property: issue for class: groovyx.net.http.HTTPBuilder

 

It's a part of my code

 

def http = new HTTPBuilder(targetUrl)
def result = http.request(POST) {
http.ignoreSSLIssues()
uri.path = '/v1/rooms'
requestContentType = URLENC
body = [title:issue.getKey()]
headers.'Authorization' = "Bearer " + access_token.toString()
headers.'Accept'='application/json'
response.success = { resp, JSON ->
return JSON
}
}

 

2 answers

0 votes
Tuncay Senturk Community Leader May 04, 2018

I do not know your problem but are you using that in postfunction.

Below code works and assigns issuekey variable in postfunction script.

import com.atlassian.jira.issue.Issue
Issue issue  = issue
def issuekey = issue.getKey()

Or simply type all code of yours so that we can better analyze.

import groovy.json.*
import groovyx.net.http.*
import static groovy.json.JsonOutput.*
import static groovyx.net.http.ContentType.*
import static groovyx.net.http.Method.*
import com.atlassian.jira.issue.Issue
import com.opensymphony.workflow.spi.Step
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.mail.server.MailServerManager
import com.atlassian.mail.server.SMTPMailServer
import com.atlassian.mail.Email
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.ComponentManager

def userManager = ComponentAccessor.getUserManager()
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def impact = (String)issue.getCustomFieldValue(customFieldManager.getCustomFieldObjectByName("Impact"))
def watcherManager = ComponentAccessor.getWatcherManager()
def watchersList = watcherManager.getCurrentWatcherUsernames(issue)
def watchersMailList = []
def assignee = userManager.getUserByName(issue.reporter.name)
def assigneeMail = assignee.getEmailAddress()

for (name in watcherManager.getCurrentWatcherUsernames(issue))
{
def user = userManager.getUserByName(name)
watchersMailList.add(user.getEmailAddress().toString())
}

public class RFC_Notification {

Issue issue = issue
def issueKey = issue.getKey()
IssueManager issueManager = ComponentManager.getInstance().getIssueManager()
def access_token = "*my_token*"
String targetUrl = '*my_api_link*'

def http = new HTTPBuilder(targetUrl)
def result = http.request(POST) {
http.ignoreSSLIssues()
uri.path = '/v1/rooms'
requestContentType = URLENC
body = [title:issueKey]
headers.'Authorization' = "Bearer " + access_token.toString()
headers.'Accept'='application/json'
response.success = { resp, JSON ->
return JSON
}
}

def memberAdding(String userMail,Boolean moderator) {
def http = new HTTPBuilder(targetUrl)
http.ignoreSSLIssues()
http.request(POST) {
uri.path = "/v1/memberships"
requestContentType = URLENC
body = [roomId:result['id'],personEmail:userMail,isModerator:moderator]
headers.'Authorization' = "Bearer " + access_token.toString()
headers.'Accept'='application/json'
response.success = { resp,json ->
println prettyPrint(toJson(json))
}
}
}
}



def test = new RFC_Notification()
for (i in watchersMailList)
{
if (assigneeMail.toString() == i)
{
test.memberAdding(i,true)
}
else {
test.memberAdding(i,false)
}
}
Tuncay Senturk Community Leader May 04, 2018

OK, thanks

You can not use below statement in your custom class.

Issue issue = issue

You should use this part above (in the main groovy part) and pass it to your class.

e.g.

def test = new RFC_Notification(issue)

public class RFC_Notification {
Issue issue;

RFC_Notification(Issue issue) {
this.issue = issue
}
}

 

I hope I was clear

Tuncay Senturk Community Leader May 04, 2018

Any update Andrew? Did it help? Do you need more help?

All is working. Thank you everyone)
Simply added a constructor

Tuncay Senturk Community Leader May 07, 2018

Great to hear that, please do not forget to accept the answer for the other community members to help them find the correct answer easily.

0 votes
Tuncay Senturk Community Leader May 04, 2018

Try to assign issue.getKey() to a variable and use that variable in anonymous method.

def issueKey = issue.getKey()
def http = new HTTPBuilder(targetUrl)

def result = http.request(POST) {
http.ignoreSSLIssues()
uri.path = '/v1/rooms'
requestContentType = URLENC
body = [title:issueKey]
headers.'Authorization' = "Bearer " + access_token.toString()
headers.'Accept'='application/json'
response.success = { resp, JSON ->
return JSON
}
}

Hope that helps

Still 

java.lang.NullPointerException: Cannot invoke method getKey() on null object

But when I debug my code using log.debug or log.error etc It return me correct issue name and ID . And I even review the type of this variable using getClass() and I see String type (all is okay). But when I put this variable to body , still see NullPointerException. I put for example userMail (using getEmailAddress) and all is working correctly. But getKey is not working. And I don't know why

I tried this method "${issueKey}" and now I see 

groovy.lang.MissingPropertyException: No such property: issue for class

 But I declared variable (Issue issue = issue), still the problem (without this variable or with this variable)

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Posted in Jira Service Management

Jira Service Management Documentation Opportunities

Hello everyone, Hope everyone is safe! A few months ago we posted an article sharing all the new articles and documentation that we, the AMER Jira Service Management team created. As mentioned ...

232 views 0 6
Join discussion

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