Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Scripting with outdated API on version 7

Pat Noack July 8, 2016

I am trying to get a script that used to work on version 5.x on JIRA to run on a v7.1.6 version.  The script was initially taken from the following webpage

https://answers.atlassian.com/questions/97963

Here is the script with modifications  I am still having issues with line 13.  Any help on the matter would be greatly appreciated.

 

import com.atlassian.jira.ComponentManager
import com.atlassian.jira.event.issue.AbstractIssueEventListener
import com.atlassian.jira.event.issue.IssueEvent
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.comments.Comment
import com.atlassian.jira.component.ComponentAccessor
import org.apache.log4j.Category
 
def commentManager = ComponentAccessor.getCommentManager()
class SubtaskCommentListener extends AbstractIssueEventListener {
    Category log = Category.getInstance(SubtaskCommentListener.class)
    CommentManager commentManager = ComponentManager.instance.commentManager
 
    @Override
    void workflowEvent(IssueEvent event) {
        // only one central way...
        this.customEvent(event)
    }
 
    @Override
    void customEvent(IssueEvent event) {
        // set explicit to debug
        log.setLevel(org.apache.log4j.Level.DEBUG)
        log.debug "Event: \"${ComponentManager.instance.eventTypeManager.eventTypesMap[event.getEventTypeId()].name}\" fired for ${event.issue}"
 
        // Here you should put any subtask based restrictions for this task like
        // only for special subtask issue types or similar
 
        Comment comment = event?.getComment()
        if (comment && event.issue.isSubTask()) {
            log.debug "New commment for subtask found."
            Issue parent = event.issue.getParentObject()
            // Here you should put any parent task based restrictions like
            // only for special issue types or similar
            commentManager.create(parent, comment.author, comment.updateAuthor, comment.body, comment.groupLevel, comment.roleLevelId, comment.created, comment.updated, true, true)
            log.debug ("Created comment on ${parent.key}")
        }
    }
}

6 answers

1 accepted

1 vote
Answer accepted
Thanos Batagiannis [Adaptavist]
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.
July 8, 2016

Hi Pat,

You should create a new custom listener, associate it with the project/s you want and for event choose an Issue Commented. For inline script use 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.comments.Comment

Issue subtask = event.issue
Comment comment = event.comment
def commentManager = ComponentAccessor.getCommentManager()

if (subtask.isSubTask() && comment) {
    def parentIssue = subtask.getParentObject()
    def commentToParent = commentManager.create(
            parentIssue, //the parent issue to copy the comment
            comment.getAuthorApplicationUser(), //the author of the subtask's comment
            comment.getBody(), //the actual comment body
            comment.getGroupLevel(), // is the group name to limit comment visibility to, this must be a valid group name.
            comment.getRoleLevelId(), // is the id of the the ProjectRole to limit comment visibility to, this must reference a valid project role.
            false) //if true then an event of type EventType.ISSUE_COMMENTED_ID will be dispatched and any notifications listening for that event will be triggered. If false no event will be dispatched.
    log.info "Comment copied to parent issue ${parentIssue.getKey()}"
}

The link to the code/class you pasted is for version <= JIRA6, the scirpt above is updated to fit JIRA7 API

Regards

Thanos

Chase Brown April 22, 2019

I'm getting an error with the "&amp;&amp;" in the IF line. Any suggestions?

Nic Brough -Adaptavist-
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.
April 22, 2019

This question was converted over from the previous "Answers" site, which messed up some of the formatting in places.  &amp; is a reformatted &, so just swap them to that in your script.

Chase Brown April 22, 2019

Now I'm getting an error with:

 

def commentToParent = commentManager.create(

 

Cannot find matching method. 

Nic Brough -Adaptavist-
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.
April 22, 2019

Best guess is that you're on a different version of Jira to the one the script works for.  You'll need to use the api docs for the current version to work out what methods to use

0 votes
Pat Noack July 8, 2016

Thanos,

 

Although there was an error the script still appears to work for adding comments editing and deleting comments.

 

Thank you for your quick response to my issue.

 

Pat

0 votes
Pat Noack July 8, 2016

Thanos,

Thank you for the new code.

 

I tried your new code out on a system and although the code itself comes up with no errors I do get an error when it runs through the code.  It is still pointing at the same line.  The log information on this is as follows:

Time (on server): Fri Jul 08 2016 09:20:20 GMT-0700 (Pacific Daylight Time)

The following log information was produced by this execution. Use statements like:log.info("...") to record logging information.

2016-07-08 09:20:20,565 ERROR [runner.AbstractScriptListener]: *************************************************************************************
2016-07-08 09:20:20,581 ERROR [runner.AbstractScriptListener]: Script function failed on event: com.atlassian.jira.event.issue.IssueEvent, file: <inline script>
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
Script51.groovy: 13: unable to resolve class CommentManager 
 @ line 13, column 5.
       CommentManager commentManager = ComponentManager.instance.commentManager
       ^
1 error
0 votes
Pat Noack July 8, 2016

Thanos,

 

I tried your new code out on a system and although the code itself comes up with no errors I do get an error when it runs through the code.  It is still pointing at the same line.  The log information on this is as follows:

Time (on server): Fri Jul 08 2016 09:20:20 GMT-0700 (Pacific Daylight Time)

The following log information was produced by this execution. Use statements like:log.info("...") to record logging information.

2016-07-08 09:20:20,565 ERROR [runner.AbstractScriptListener]: *************************************************************************************
2016-07-08 09:20:20,581 ERROR [runner.AbstractScriptListener]: Script function failed on event: com.atlassian.jira.event.issue.IssueEvent, file: <inline script>
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
Script51.groovy: 13: unable to resolve class CommentManager 
 @ line 13, column 5.
       CommentManager commentManager = ComponentManager.instance.commentManager
       ^
1 error
0 votes
Pat Noack July 8, 2016

Sorry about that.  Thought I had included the error.  I think cutting and pasting into the webpage screwed up the lines for me.

This is the line it errors out on

CommentManager commentManager = ComponentManager.instance.commentManager

 

Unable to resolve class CommentManager @line13 column 5

0 votes
Nic Brough -Adaptavist-
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.
July 8, 2016

Which is line 13 (empty by my count) and what is the error?

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events