简体   繁体   English

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

[英]How to get a user by email in JIRA Script Runner

When writing a Groovy script for JIRA Script Runner , how do you get a user, or just their username, given their email address? 在为JIRA Script Runner编写Groovy脚本时,给定用户的电子邮件地址,如何获得用户或仅获得用户名?

It seems that you're supposed to use the findUsersByEmail method in the UserSearchService interface. 似乎您应该在UserSearchService接口中使用findUsersByEmail方法。

https://docs.atlassian.com/jira/7.0.2/com/atlassian/jira/bc/user/search/UserSearchService.html 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? 但是,如何获得此类的实例?

Related question: How to get a user by email in a JIRA plugin . 相关问题: 如何通过JIRA插件中的电子邮件获取用户 The difference is that question is about a plugin, and my question is about JIRA Script Runner. 不同之处在于该问题与插件有关,而我的问题与JIRA Script Runner有关。

This code does not work: 此代码不起作用:

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

This is the error I got: 这是我得到的错误:

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)

EDIT 编辑

Based on @Oldskultxo's and @BjörnKautler suggestions, this is now my working code: 基于@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();
}

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

I usually get components this way: 我通常以这种方式获取组件:

ComponentManager.getComponentInstanceOfType(UserSearchService.class);

And then just look for its methods. 然后只寻找其方法。

Regards 问候

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM