You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ScriptRunner获取当前用户用于数据库查询失效,求正确传递方法

解决ScriptRunner中获取当前用户用于数据库查询失效的问题

问题概述

在ScriptRunner的数据库查询配置脚本中,尝试通过ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()获取当前用户并传递给SQL查询时功能失效,但将用户账号设为固定字符串时脚本可正常运行。

原验证SQL:

select jiraaccount,displayname from mcigeneralname where jiraaccount = ?

原搜索SQL:

select jiraaccount,displayname from mcigeneralname
where lower(jiraaccount) like lower(?) || '%' 

原配置脚本:

import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.customfields.option.Option
import com.onresolve.scriptrunner.canned.jira.fields.editable.database.SqlWithParameters
import com.atlassian.jira.component.ComponentAccessor;


def userinfousername = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser().getUsername()
String userinfo = userinfousername


getSearchSql = { String inputValue, Issue issue, String originalValue ->

new SqlWithParameters(
"select jiraaccount,displayname from mcigeneralname where jiraaccount like ? || '%' and jiraaccount = ?",
[inputValue,userinfo]
)
}

getValidationSql = { String id, Issue issue,String originalValue ->

new SqlWithParameters("""
select jiraaccount,displayname from mcigeneralname
where displayname = ? and jiraaccount = ?
"""
, [id, userinfo])
}

问题原因

原脚本在闭包外部提前获取当前用户,而ScriptRunner的getSearchSql和getValidationSql闭包会在特定查询触发时机才执行,提前获取的用户变量可能因上下文切换(比如脚本初始化时的用户与实际查询时的用户不一致,或初始化时无登录用户)导致失效。

修复方案

将获取当前用户的逻辑移到每个闭包内部,确保在执行SQL查询的瞬间获取当前登录用户,避免上下文不一致问题。修改后的脚本如下:

import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.customfields.option.Option
import com.onresolve.scriptrunner.canned.jira.fields.editable.database.SqlWithParameters
import com.atlassian.jira.component.ComponentAccessor;


getSearchSql = { String inputValue, Issue issue, String originalValue ->
    // 在闭包内部实时获取当前登录用户
    def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
    if (!currentUser) {
        throw new IllegalStateException("No logged-in user found")
    }
    String userinfo = currentUser.getUsername()
    
    new SqlWithParameters(
        "select jiraaccount,displayname from mcigeneralname where jiraaccount like ? || '%' and jiraaccount = ?",
        [inputValue, userinfo]
    )
}

getValidationSql = { String id, Issue issue, String originalValue ->
    // 在闭包内部实时获取当前登录用户
    def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
    if (!currentUser) {
        throw new IllegalStateException("No logged-in user found")
    }
    String userinfo = currentUser.getUsername()
    
    new SqlWithParameters("""
        select jiraaccount,displayname from mcigeneralname
        where displayname = ? and jiraaccount = ?
        """, 
        [id, userinfo]
    )
}

关键改动说明

  • 移除闭包外部的用户变量定义,改为在每个闭包执行时实时获取当前登录用户
  • 添加空值判断,避免因无登录用户导致的空指针异常
  • 确保SQL查询使用的是触发查询时的当前用户,而非脚本初始化时的用户

内容的提问来源于stack exchange,提问作者MohammadReza moeini

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.14 22:37:41