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

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

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));
    }

 

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?

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

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

Hmm. Cast jie to DelegatingJiraIssueEvent perhaps

Your suggestion worked!

Thanks for your help, Matt.

 

Thanks

--Andrew

Suggest an answer

Log in or Join to answer
Community showcase
Teodora [Botron]
Published Thursday in Marketplace Apps

Jira Inferno: The Nine Circles of Jira Administration Hell

If you spend enough time as a Jira admin - whether you are managing a single, mid-sized instance, a large enterprise one or juggling multiple instances at once - you will eventually find yourself in ...

480 views 1 15
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
Atlassian Team Tour

Join us on the Team Tour

We're bringing product updates and pro tips on teamwork to ten cities around the world.

Save your spot