How to get the current logged-in user in MailListener.java (for Jira version 7.0.10) ?

Andrew L June 17, 2017

I need to modify MailListener.java (for Jira version 7.0.10) to NOT send out email if the current logged-in user is <some-userid>.

Here's the code snippet of MailListener.java :

...........
import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.user.ApplicationUser;
..............

private static final ApplicationUser currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser(); 

......

 @EventListener
 public void handleIssueEventBundle(final IssueEventBundle bundle) {
   
  if (currentUser == null) {
      log.error("Cannot determine current logged-in user");  // <<<<--------- this error shows up in the log
      return;
     }
  if (currentUser.getUsername().equals(specialUser)) {
      log.debug("1. Ignore sending email notification because user=[" + currentUser.getUsername() + "]");
      return;
     }

   issueEventBundleMailHandler.handle(bundle);
}

 

In the atlassian-jira.log, i see:

   [c.a.j.e.listeners.mail.MailListener] Cannot determine current logged-in user

What is the correct way to get the current logged-in user in MailListener.java ?

 

 

1 answer

1 accepted

0 votes
Answer accepted
MattS
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.
June 19, 2017

https://docs.atlassian.com/jira/7.0.9/com/atlassian/jira/event/issue/IssueEvent.html has a getUser() method that should tell you the info you want

 protected void handleDefaultIssueEvent(final IssueEvent event) {
        if (event.isRedundant()) {
            return;
        }
        handleIssueEventBundle(issueEventBundleFactory.wrapInBundle(event));
    }

 

Andrew L June 19, 2017

Thanks.  I saw that method, but the debug messages i've put in indicate that handleIssueEventBundle() is STILL being invoked by Jira.  As far as i can tell, emails are getting sent out by handleIssueEventBundle().

The argument to handleIssueEventBundle is of type "IssueEventBundle".  I have not been able to get the appropriate code in place in order to convert IssueEventBundle to an IssueEvent.

Do you have any pointers on how to get an IssueEvent from within handleIssueEventBundle() method?

MattS
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.
June 20, 2017

DefaultIssueEventBundle has a getEvents method that looks like it returns JiraIssueEvent objects which have a asIssueEvent method to access the original IssueEvent object

Andrew L June 20, 2017

Matt, thanks again for your help.

I also saw that earlier in the docs and already tried:

@EventListener
public void handleIssueEventBundle(final IssueEventBundle bundle) {
   Collection<JiraIssueEvent> Cjie = bundle.getEvents();
       for (JiraIssueEvent jie: Cjie) {
            IssueEvent event = jie.asIssueEvent(); // <<--- compiler complains about asIssueEvent() being not found
            log.debug("user of current event object is [" + event.getUser().getName() + "]");
       }

      issueEventBundleMailHandler.handle(bundle);
}

However, i got compilation errors:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project jira-core: Compilation failure
[ERROR] /...../atlassian-jira-software-7.0.10-source/jira-project/jira-components/jira-core/src/main/java/com/atlassian/jira/event/listeners/mail/MailListener.java:[147,35] cannot find symbol
[ERROR] symbol:   method asIssueEvent()
[ERROR] location: variable jie of type com.atlassian.jira.event.issue.JiraIssueEvent

Any other suggestions would be greatly appreciated.

Thanks

--Andrew

MattS
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.
June 20, 2017

Hmm. Cast jie to DelegatingJiraIssueEvent perhaps

Andrew L June 28, 2017

Your suggestion worked!

Thanks for your help, Matt.

 

Thanks

--Andrew

Suggest an answer

Log in or Sign up to answer