Triggering an event from groovy script

I am trying to trigger a custom event from my groovy script.

IssueEventManager.dispatchEvent(new Long(10001),x,ManagerFactory.getWorkflowManager().getCaller(transientVars),true);

I am getting this error

groovy.lang.MissingMethodException: No signature of method: com.atlassian.jira.workflow.OSWorkflowManager.getCaller() is applicable for argument types: (java.util.HashMap) values: [[createdStep:SimpleStep@11[owner=, actionId=0, status=Open], ...]]

Can anyone provide me a working snippet of how to trigger a custom event programatically

1 answer

1 accepted

0 vote
Henning Tietgens Community Champion Feb 05, 2014

This depends on your JIRA version and where you trigger the script. I suspect it's in a workflow postfunction, where you could use currentUser for the user part of dispatchEvent().

I got it to work with this snippet

HashMap params = new HashMap();
params.put("baseurl", ManagerFactory.getApplicationProperties().getString(APKeys.JIRA_BASEURL));
//prevent a mail from being generated
params.put("dispatchEvent", new Boolean(false));
EventType event = ComponentManager.getInstance().getEventTypeManager().getEventType( 10001);
IssueEvent issueEvent = new IssueEvent( issue, params, remoteUser, event.getId() );
IssueEventDispatcher.dispatchEvent( issueEvent );

I am not being able to get an outgoing mail when I fire the event . I have created a custom event and have configured it in my default notification scheme . I am using that id over here in the snippet(10001). Is there any other configuraton I need to send the email

Henning Tietgens Community Champion Feb 05, 2014

I would stay with

issueEventManager.dispatchEvent (Long eventTypeId, Issue issue, User remoteUser, boolean sendMail)

this works for me. With the last param you could configure if an email should be send.


What are using to get the remoteUser. I am using User remuser= ComponentAccessor.getJiraAuthenticationContext().getUser().getDirectoryUser();

but this is causing an error when I pass that to the

issueEventManager.dispatchEvent (Long eventTypeId, Issue issue, UserremoteUser, boolean sendMail)

Henning Tietgens Community Champion Feb 05, 2014

Is it a listener you're runing? In this case you could use event.user otherwise your approach should work. Maybe you could log which user you get from the authenticationContext, sometimes this is null.

when running

issueEventManager.dispatchEvent (Long eventTypeId, Issue issue, UserremoteUser, boolean sendMail)

in a script listener, the check tells me that the variable "issueEventManager" is undeclared.

What can i do?

Henning Tietgens Community Champion Jun 13, 2016

Add

import com.atlassian.jira.component.ComponentAccessor
def issueEventManager = ComponentAccessor.getIssueEventManager()

to your code to import the ComponentAccessor and use this class to get the issueEventManager.

thank you

now i get the following

image2016-6-13 15:1:16.png

 

sorry, i'm not really into scripting smile

 

Henning Tietgens Community Champion Jun 13, 2016

Could you post the complete code you're using? The message indicates you may be using the wrong parameters.

here the original code.

It syncs comments between issues in two projects. What it won't do, is to trigger a issuecommented Event in the destination issue. This is what i'm trying to add.

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Category;

import com.atlassian.jira.component.ComponentAccessor;
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.IssueManager;
import com.atlassian.jira.issue.MutableIssue;
import com.atlassian.jira.issue.comments.Comment;
import com.atlassian.jira.issue.comments.CommentManager;
import com.atlassian.jira.issue.comments.MutableComment;
import com.atlassian.jira.issue.link.IssueLink;
import com.atlassian.jira.issue.link.IssueLinkManager;


        
        
Category log = Category.getInstance("SyncComments");
IssueManager issueManager = ComponentAccessor.getIssueManager();        
log.info("SyncComments started");
Comment c = event.getComment();
if (c == null)
    return;        

List<Issue> linkedIssues = new ArrayList<Issue>();
IssueLinkManager issueLinkManager = ComponentAccessor.getIssueLinkManager();

List<IssueLink> links = issueLinkManager.getInwardLinks(event.getIssue().getId());
if (links != null && links.size() > 0)
for (IssueLink issueLink : links) {
    switch (Integer.parseInt(String.valueOf(issueLink.getIssueLinkType().getId()))) {
    case 10801:
        linkedIssues.add(issueManager.getIssueObject(issueLink.getSourceId()));
        break;
    }
}

links = issueLinkManager.getOutwardLinks(event.getIssue().getId());
if (links != null && links.size() > 0)
for (IssueLink issueLink : links) {
    int linkid = Integer.parseInt(String.valueOf(issueLink.getIssueLinkType().getId()));
    log.info("linkid: "+linkid);
    switch (linkid) {
    case 10801:
        linkedIssues.add(issueManager.getIssueObject(issueLink.getDestinationId()));
        break;            
    }
}    

log.info("linkedIssues: "+linkedIssues.size());
for (Issue issueDest : linkedIssues) {            
    MutableIssue issueDestination = issueManager.getIssueObject(issueDest.getId());

    if (issueDestination != null) {
        CommentManager commentManager = ComponentAccessor.getCommentManager();

        Comment destinationComment = null;                
        List<Comment> comments = commentManager.getComments(issueDestination);
        for (Comment comment : comments) {
            if (comment.getAuthorApplicationUser().equals(c.getAuthorApplicationUser())    && comment.getCreated().equals(c.getCreated()))
                destinationComment = comment;
        }
        
        Comment triggerComment = event.getComment();

        if (destinationComment != null) {
            // comment is still there and has to be updated
            MutableComment destinationMutableComment = commentManager
                    .getMutableComment(destinationComment.getId());
            destinationMutableComment.setBody(triggerComment.getBody());
            destinationMutableComment.setGroupLevel(triggerComment.getGroupLevel());
            destinationMutableComment.setRoleLevelId(triggerComment.getRoleLevelId());
            destinationMutableComment.setUpdateAuthor(triggerComment.getUpdateAuthorApplicationUser());
            destinationMutableComment.setUpdated(triggerComment.getUpdated());
            commentManager.update(destinationMutableComment, false);
            log.info("SyncComment edited a comment with id " + destinationMutableComment.getId()+ " at the issue with key " + issueDestination.getKey());
        } else {
            // Comment has to be created
            Comment comment = commentManager.create(issueDestination, triggerComment.getAuthorApplicationUser(), triggerComment.getUpdateAuthorApplicationUser(), triggerComment.getBody(), triggerComment.getGroupLevel(), triggerComment.getRoleLevelId(), triggerComment.getCreated(), triggerComment.getUpdated(), false);
            log.info("SyncComment created a comment with id " + comment.getId() + " at the issue with key "+ issueDestination.getKey());
        }
    }
}
log.info("SyncComment finished");
Henning Tietgens Community Champion Jun 14, 2016

Try this

import com.atlassian.jira.event.type.EventType

def issueEventManager = ComponentAccessor.getIssueEventManager()


issueEventManager.dispatchEvent(EventType.ISSUE_COMMENTED_ID, issueDestination, c.authorApplicationUser.directoryUser, true)


issueEventManager.dispatchEvent(EventType.ISSUE_COMMENT_EDITED_ID, issueDestination, c.updateAuthorApplicationUser.directoryUser, true)

The first dispatchEvent is for the else{} part, the second for the if{} part.

Henning Tietgens Community Champion Jun 14, 2016

Forget my comment... just replace the last parameter false of commentManager.update() and commentManager.create() with true and the correct event should be triggered.

great! works perfectly

thank you very much smile

Suggest an answer

Log in or Sign up to answer
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Published yesterday in Jira

5 ways you can make the most of Jira Software and Bitbucket Cloud

As part of the Bitbucket product team I'm always interested in better understanding what kind of impact the use of our tools have on the way you work. In a recent study we conducted of software devel...

32 views 0 4
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