Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Groovy error (working with API)

Andrew Striletskyi May 4, 2018

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

Suggest an answer

Log in or Sign up to answer
0 votes
Tuncay Senturk
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
May 4, 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.

Andrew Striletskyi May 4, 2018
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
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
May 4, 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
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
May 4, 2018

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

Andrew Striletskyi May 7, 2018

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

Tuncay Senturk
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
May 7, 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
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
May 4, 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

Andrew Striletskyi May 4, 2018

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

Andrew Striletskyi May 4, 2018

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)

TAGS
AUG Leaders

Atlassian Community Events