How to get a user by email in a JIRA plugin

2019-07-23 20:33发布

问题:

When writing a plugin for JIRA, how do you get a user, or just their username, given their email address?

It seems that you're supposed to use the findUsersByEmail method in the UserSearchService interface

https://docs.atlassian.com/jira/7.0.2/com/atlassian/jira/bc/user/search/UserSearchService.html

But how do you get an instance of this class? Or a singleton of it?

回答1:

The component system in JIRA is built on Spring. Therefore, if the class you're working on is autowired (e.g. plugin module like a macro or a Xwork action, servlet all will be), create an instance variable for UserSearchService and add it to the constructor:

public MyServlet(UserSearchService userSearchService) {
    this.userSearchService = userSearchService;
}

OR create an instance variable and add a setter for it:

public void setUserSearchService(UserSearchService userSearchService) {
    this.userSearchService= userSearchService;
}

If the class you're working on is not autowired, you can sometimes use the ComponentAccessor to access an instance statically but I can't see the UserSearchService in the list of methods.