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

Next challenges

Recent achievements

Recognition

  • Give kudos
  • My kudos

Leaderboard

  • Global

Trophy case

Kudos (beta program)

Kudos logo

You've been invited into the Kudos (beta program) private group. Chat with others in the program, or give feedback to Atlassian.

View group

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

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

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)

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)
}
}

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

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

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

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.

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Confluence Cloud

Share your Confluence Cloud experience for a chance to win $500!

Take our 5-minute survey to win a $500 Visa gift card! Are you currently using Confluence Cloud? We want to hear from you! Fill out this quick survey about your Confluence Cloud experience so we ...

293 views 5 14
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