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

Accepted Answer
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 Sign up to answer
Community showcase
Published 9 hours ago in Sourcetree

Tip from the team: configure your repos for hosting goodness!

Supported Platforms macOS Windows We recently introduced support for additional hosting services such as GitHub Enterprise, GitLab (Cloud, Community Edition, Enterprise Edition), and...

56 views 0 1
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