Scriptrunner listener: get current issue data

Christian Schneider
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
February 2, 2022

Dear community,

Question: How do I get the data of the current issue, for with an event fired in a  Scriptrunner listener?

 

Background: I am developing a little groovy script to send issue-data to a sharepoint list. I did the development in the "Groovy Console" of the "JIRA MISC WORKFLOW EXTENSIONS"-App. This console is very handy, as one can test the script against an existing issue.

My code is working perfectly fine in the JMWE-environment.

Unfortunately, I need to use a Scriptrunner listener, because I want to run my script on certain events. Scriptrunner handles things slightly different and I am struggling at the very beginning: how do I get the data of the current issue, for witch an event fired? What do I need to "import" to have access on the current issue?

 

My code:

import groovyx.net.http.HTTPBuilder
import groovyx.net.http.RESTClient
import java.time.LocalDateTime


def issueKey = issue.getAsString("issuekey")
def issueStatus = issue.getAsString("status")
def issueTitle = issue.get("summary")
def issueDescription = issue.get('description')
def issueComment = issue.getAsString("comment")
def issueStartDate = issue.getAsString("customfield_10705")
def issueResolutionDate = issue.get("resolutiondate")

//log.info "${issueKey} --- ${issueStatus} --- ${issueTitle} --- ${issueDescription} --- ${issueStartDate} --- ${issueResolutionDate}"
//log.info "${issueComment}"


...

 

Any help or link to the documentation is very much appreciated.

 

1 answer

1 accepted

1 vote
Answer accepted
Fabio Racobaldo _Herzum_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
February 2, 2022

Hi @Christian Schneider ,

you need to retrieve your issue first and then any field associated. Try the following code :

 

 

Issue issue = event.getIssue()

def issueKey = issue.getKey()
def issueStatus = issue.getStatus().getName()
def issueTitle = issue.getSummary()
def issueDescription = issue.getDescription()
def issueResolutionDate = issue.getResolutionDate()

 Please take a look to the following API in order to get available methods https://docs.atlassian.com/software/jira/docs/api/8.5.1/index.html?com/atlassian/jira/issue/Issue.html

For comments, you need the following API https://docs.atlassian.com/software/jira/docs/api/8.5.3/com/atlassian/jira/issue/comments/CommentManager.html

Hope this helps,

Fabio

Christian Schneider
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
February 2, 2022

Dear @Fabio Racobaldo _Herzum_ 

thank you very much for your quick input. This works perfectly fine to get the Issue and field associated.

 

Regarding comments:

I did not yet fully understand the documentation under the provided link. Do I have to add this?

import com.atlassian.jira.issue.comments.CommentManager

def issueComment = issue.getComments()

Does not seem to work, as no matching method can be found.

 

 

Regarding custom fields for my issueStartDate-field:

Same question: this is not correct, is it?

import com.atlassian.jira.issue.customfields

def issueStartDate = issue.getValue("customfield_10705")
Fabio Racobaldo _Herzum_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
February 2, 2022

Hi @Christian Schneider ,

for comments and your field value, try the following code :

import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.comments.Comment;
import com.atlassian.jira.issue.comments.CommentManager;
import com.atlassian.jira.issue.fields.CustomField;

Issue issue = event.getIssue()

def issueKey = issue.getKey()
def issueStatus = issue.getStatus().getName()
def issueTitle = issue.getSummary()
def issueDescription = issue.getDescription()
def issueResolutionDate = issue.getResolutionDate()

CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager();
CustomField myField = customFieldManager.getCustomFieldObjectByName("YOUR_CF_NAME_HERE");
def myFieldValue = issue.getCustomFieldValue(myField)!=null?issue.getCustomFieldValue(myField).toString():null;

CommentManager commentManager = ComponentAccessor.getCommentManager();
List<Comment> comments = commentManager.getComments(issue);


Hope this helps, Fabio

Like Alex likes this
Christian Schneider
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
February 2, 2022

@Fabio Racobaldo _Herzum_ 

 

Thank you very much. The thing with the customField did not work out. I changed it from "...byName" to the one, where I have to put in the CustomField-Number. Nevertheless, it did not work either. perhaps, because it's a date? Anyway, I decided to use the "created"-date. Should do for now.

 

regarding the comments:

 I get the list of the comment-IDs, but not the text itself.

[com.atlassian.jira.issue.comments.CommentImpl@b90a0cf0, com.atlassian.jira.issue.comments.CommentImpl@e94800f7, com.atlassian.jira.issue.comments.CommentImpl@787f731, com.atlassian.jira.issue.comments.CommentImpl@153873f5, com.atlassian.jira.issue.comments.CommentImpl@5108e309, com.atlassian.jira.issue.comments.CommentImpl@9bf5d073, com.atlassian.jira.issue.comments.CommentImpl@b27add91, com.atlassian.jira.issue.comments.CommentImpl@57290461, com.atlassian.jira.issue.comments.CommentImpl@19396fd5, com.atlassian.jira.issue.comments.CommentImpl@86582134, com.atlassian.jira.issue.comments.CommentImpl@76fc0550]

I am struggling the get the comment as a string. Any idea?

Fabio Racobaldo _Herzum_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
February 2, 2022

Hi @Christian Schneider ,

here the code with your custom field id and comments management.

Each comment can be retrieved ussing method .getBody

Here new code :

import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.comments.Comment;
import com.atlassian.jira.issue.comments.CommentManager;
import com.atlassian.jira.issue.fields.CustomField;

Issue issue = event.getIssue()


def issueKey = issue.getKey()
def issueStatus = issue.getStatus().getName()
def issueTitle = issue.getSummary()
def issueDescription = issue.getDescription()
def issueResolutionDate = issue.getResolutionDate()

CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager();
CustomField myField = customFieldManager.getCustomFieldObject(10705L);
def myFieldValue = issue.getCustomFieldValue(myField)!=null?issue.getCustomFieldValue(myField).toString():null;

CommentManager commentManager = ComponentAccessor.getCommentManager();
List<Comment> comments = commentManager.getComments(issue);
for(Comment comment : comments){
YOUR_VARIABLE_HERE = comment.getBody();
}
Christian Schneider
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
February 3, 2022

Hi @Fabio Racobaldo _Herzum_ 

 

thank you very much. Your example only provides the last comment. I guess, while running through the for-loop, the variable "YOUR_VARIABLE_HERE" is overwritten everytime? So only the last comment is provided to the rest of the script?

 

I changed the script into this:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.comments.Comment
import com.atlassian.jira.issue.comments.CommentManager
import com.atlassian.jira.issue.fields.CustomField

import groovyx.net.http.HTTPBuilder
import groovyx.net.http.RESTClient
import java.time.LocalDateTime


Issue issue = event.getIssue()

def issueKey = issue.getKey()
def issueStatus = issue.getStatus().getName()
def issueTitle = issue.getSummary()
def issueDescription = issue.getDescription()
def issueStartDate = ( issue.getCreated() != null ? issue.getCreated().format( 'yyyy-MM-dd HH:mm' ) : null )
def issueResolutionDate = ( issue.getResolutionDate() != null ? issue.getResolutionDate().format( 'yyyy-MM-dd HH:mm' ) : null )


def commentManager = ComponentAccessor.commentManager
def comments = commentManager.getComments(issue)
def issueComments = []
comments.each { comment ->
issueComments.add(comment.getCreated().format("dd.MM.yy HH:mm") + ": " + comment.getBody() + System.getProperty("line.separator") + System.getProperty("line.separator"))
}


log.warn "Issue Key: ${issueKey}"
log.warn "Status: ${issueStatus}"
log.warn "Title: ${issueTitle}"
log.warn "Issue Description: ${issueDescription}"
log.warn "Startdate: ${issueStartDate}"
log.warn "Enddate: ${issueResolutionDate}"
log.warn "Comments: ${issueComments}"

 

Log:

2022-02-03 14:23:11,693 WARN [runner.ScriptBindingsManager]: Issue Key: ICTPRM-19
2022-02-03 14:23:11,694 WARN [runner.ScriptBindingsManager]: Status: Open
2022-02-03 14:23:11,694 WARN [runner.ScriptBindingsManager]: Title: Test Scriptrunner Listener to Sharepoint
2022-02-03 14:23:11,694 WARN [runner.ScriptBindingsManager]: Issue Description: sdfff
2022-02-03 14:23:11,694 WARN [runner.ScriptBindingsManager]: Startdate: 2022-02-02 12:31
2022-02-03 14:23:11,694 WARN [runner.ScriptBindingsManager]: Enddate: null
2022-02-03 14:23:11,694 WARN [runner.ScriptBindingsManager]: Comments: [02.02.22 16:27: Test
, 02.02.22 16:31: test2
, 02.02.22 16:39: Test3
, 02.02.22 16:42: Test4
, 02.02.22 16:48: Test 5
, 02.02.22 16:55: Test 6
, 02.02.22 16:59: Test 7
, 02.02.22 17:02: Test 8
, 02.02.22 17:04: Test 9
, 02.02.22 17:12: Test 10
, 02.02.22 17:17: Test11
, 03.02.22 13:28: Test 12 Kommentare abrufen
, 03.02.22 13:33: Test 13
, 03.02.22 14:23: Test 14
]

Now, all the comments are written into a List. I will use this variable to transfer all the text as a string with a RESTClient to a sharepoint list.

 

Thank you very much for help and effort. You definietely pointed me into the right directions. :-)

Fabio Racobaldo _Herzum_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
February 3, 2022

You're welcome @Christian Schneider . Please mark my answer as accepted ;)

Christian Schneider
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
February 3, 2022

Update

 

I found a nicer way to bring all comments into one variable:

def commentManager = ComponentAccessor.commentManager
def comments = commentManager.getComments(issue)
def issueComments = []
comments.each { comment ->
issueComments.add(comment.getCreated().format("dd.MM.yy HH:mm") + ": " + comment.getBody())
}

def issueCommentsString = issueComments.join('- --- --- --- -')

 

when transfered via RESTClient/JSON to a sharepoint list, all carriage return/linefeed are lost. This makes short comments almost unreadable.

I did not yet figure out, how to circumvent this. Must be something in the receiving engine of sharepoint, which removes all CRLF from the input...

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
SERVER
VERSION
8.20.4
TAGS
AUG Leaders

Atlassian Community Events