Scripting with outdated API on version 7

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
Accepted answer

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

0 votes

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

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

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

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

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

Suggest an answer

Log in or Sign up to answer
Community showcase
Published Oct 31, 2018 in Marketplace Apps

Marketplace Spotlight: Zephyr

Hello Atlassian Community! Each month, we run a series of Spotlights to highlight Marketplace vendors and apps that our team thinks this Community would find valuable. In last month's Spotlig...

415 views 0 3
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you