如何通过JIRA Script Runner中的电子邮件获取用户

时间:2016-04-18 15:44:52

标签: groovy jira jira-plugin jira-rest-api

JIRA Script Runner编写Groovy脚本时,如何根据用户的电子邮件地址获取用户或用户名?

您似乎应该在findUsersByEmail界面中使用UserSearchService方法。

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

但是你怎么得到这个类的实例?

相关问题:How to get a user by email in a JIRA plugin。 区别在于问题是关于插件的,我的问题是关于JIRA Script Runner。

此代码不起作用:

setUserProperties(httpMethod: "POST", groups: ["jira-administrators"])
{ MultivaluedMap queryParams, String body, HttpServletRequest request ->

    def userPropertyManager = ComponentAccessor.getUserPropertyManager()
    def userManager = ComponentAccessor.getUserManager()
    def userSearchService = DefaultUserPickerSearchService;
    def users = userSearchService.findUsersByEmail("felicity.smoak@queenconsolidated.com")

    users.each {
        aUser ->
            userPropertyManager.getPropertySet(aUser).setString("jira.meta.Company", "Smoak Technologies")
    }

    return Response.ok(users).build();
}

这是我得到的错误:

2016-04-18 15:23:06,168 ERROR [common.UserCustomScriptEndpoint]: *************************************************************************************
2016-04-18 15:23:06,168 ERROR [common.UserCustomScriptEndpoint]: Script endpoint failed on method: POST setUserProperties
groovy.lang.MissingMethodException: No signature of method: static com.atlassian.jira.bc.user.search.DefaultUserPickerSearchService.findUsersByEmail() is applicable for argument types: (java.lang.String) values: [felicity.smoak@queenconsolidated.com]
Possible solutions: findUsersByEmail(java.lang.String), findUserKeysByEmail(java.lang.String)
    at Script462$_run_closure3.doCall(Script462.groovy:40)
    at com.onresolve.scriptrunner.runner.rest.common.UserCustomScriptEndpoint.doEndpoint(UserCustomScriptEndpoint.groovy:308)
    at com.onresolve.scriptrunner.runner.rest.common.UserCustomScriptEndpoint.postUserEndpoint(UserCustomScriptEndpoint.groovy:208)

修改

基于@ Oldskultxo和@BjörnKautler的建议,现在这是我的工作代码:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.user.*
import com.atlassian.jira.bc.user.search.UserSearchService
import com.atlassian.sal.api.user.UserManager
import com.onresolve.scriptrunner.runner.rest.common.CustomEndpointDelegate
import groovy.json.*
import groovy.transform.BaseScript

import javax.servlet.http.HttpServletRequest
import javax.ws.rs.core.MultivaluedMap
import javax.ws.rs.core.Response

@BaseScript CustomEndpointDelegate delegate

setUserProperties(httpMethod: "POST", groups: ["jira-administrators"])
{ MultivaluedMap queryParams, String body, HttpServletRequest request ->

    def userPropertyManager = ComponentAccessor.getUserPropertyManager()
    def userManager = ComponentAccessor.getUserManager()
    def userSearchService = ComponentAccessor.getComponent(UserSearchService.class)
    def users = userSearchService.findUsersByEmail("felicity.smoak@queenconsolidated.com")

    users.each {
        aUser ->
            userPropertyManager.getPropertySet(aUser).setString("jira.meta.Company", "Smoak Technologies")
    }

    return Response.ok("200").build();
}

2 个答案:

答案 0 :(得分:1)

如果没有具体的ComponentAccessor.getComponent(UserSearchService)方法,请使用getUserSearchService()获取正确的服务。

答案 1 :(得分:1)

我通常以这种方式获得组件:

ComponentManager.getComponentInstanceOfType(UserSearchService.class);

然后只是寻找它的方法。

此致