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

创建计算两个Jira过滤器数量比值的Confluence用户宏遇输出异常求助

问题:Confluence用户宏Jira变量未解析,输出原始变量字符串

我尝试创建一个用于计算两个Jira过滤器数量比值的Confluence用户宏,宏代码如下:

## Macro title: totalBugs
## Macro has a body: Y
## Body processing: Rendered
## Output: Rendered
##
## Developed by: Margus Martsepp
## Date created: 06/02/2023
## Installed by: Margus Martsepp
## @param Search1:title=Search1|type=string|required=true|desc=Choose search 1
## @param Search2:title=Search2|type=string|required=true|desc=Choose search 2
#set($jiraUrl = "https://.../")
#set($proxyHost = "http://.../")
#set($proxyPort = "8080")
#set($action = $helper.getActionContext())
#set($jira = $action.getApplicationContext().getComponent("jira"))
#if($jira)
  #error("Jira variable is not initialized, check if the Jira plugin is properly installed and configured")
#end

#set($result1 = $jira.search($paramSearch1, $jiraUrl, $proxyHost, $proxyPort))
#set($result2 = $jira.search($paramSearch2, $jiraUrl, $proxyHost, $proxyPort))

#if($result1)
  #error("Search 1 has returned no results, check the query and connection details")
#end
#if($result2)
  #error("Search 2 has returned no results, check the query and connection details")
#end
#if($result1.total == 0)
  #error("Search 1 has returned no results, division by zero is not possible")
#end
#if($result2.total == 0)
  #error("Search 2 has returned no results, division by zero is not possible")
#end

#set($count1 = $result1.total)
#set($count2 = $result2.total)
#if($count2 == 0)
#error("Search 2 has returned no results, division by zero is not possible")
#end
#set($result = $count1 / $count2 * 100)
#set($output = "Result of dividing search 1 count ($count1) by search 2 count ($count2) is: $result")
$output

除此之外,我还尝试使用getIssuesFromJqlSearch API,示例代码为:$jira.getIssuesFromJqlSearch($paramSearch1, 1000)

但两种方式最终都输出未解析的变量字符串:

Result of dividing search 1 count ($count1) by search 2 count ($count2) is: $result

请问是JIRA服务配置存在遗漏,还是API使用方式有误?


问题分析与解决

核心问题:Velocity模板逻辑错误+API使用不当

  1. Jira变量初始化判断逻辑颠倒
    原代码中#if($jira)会在$jira初始化成功时抛出错误,应该改成#if(!$jira),否则变量未初始化时反而不会触发错误提示,导致后续代码在$jira为空的情况下执行,所有依赖$jira的变量都无法赋值,最终输出原始字符串。

  2. Jira API调用方式错误

    • 你使用的$jira.search()并非Confluence集成Jira的标准API,没有对应的实现逻辑,无法正确返回结果。
    • getIssuesFromJqlSearch方法需要传入用户对象作为第一个参数,且仅返回指定数量的Issue对象,不是直接获取总数,效率低下且不符合需求。

修正后的宏代码示例

## Macro title: totalBugs
## Macro has a body: N
## Body processing: Rendered
## Output: Rendered
##
## Developed by: Margus Martsepp
## Date created: 06/02/2023
## Installed by: Margus Martsepp
## @param Search1:title=Search1|type=string|required=true|desc=JQL语句1
## @param Search2:title=Search2|type=string|required=true|desc=JQL语句2

#set($action = $helper.getActionContext())
#set($jiraSearchService = $action.getApplicationContext().getComponent("com.atlassian.jira.bc.issue.search.SearchService"))
#set($currentUser = $action.getUser())

#if(!$jiraSearchService)
  #error("无法获取Jira搜索服务,请检查Confluence与Jira的集成配置")
#end

#set($queryParser1 = $jiraSearchService.parseQuery($currentUser, $paramSearch1))
#if(!$queryParser1.isValid())
  #error("Search1的JQL语句无效:$queryParser1.getErrors()")
#end

#set($queryParser2 = $jiraSearchService.parseQuery($currentUser, $paramSearch2))
#if(!$queryParser2.isValid())
  #error("Search2的JQL语句无效:$queryParser2.getErrors()")
#end

#set($count1 = $jiraSearchService.searchCount($currentUser, $queryParser1.getQuery()))
#set($count2 = $jiraSearchService.searchCount($currentUser, $queryParser2.getQuery()))

#if($count1 == 0)
  #error("Search1返回结果数量为0,无法计算比值")
#end
#if($count2 == 0)
  #error("Search2返回结果数量为0,除法运算将出现除以零错误")
#end

#set($ratio = ($count1 / $count2) * 100)
#set($formattedRatio = $numberTool.format("#.##", $ratio))
"Search1数量($count1)与Search2数量($count2)的比值为:$formattedRatio%"

额外配置检查

  • 确保Confluence已安装并配置Jira Issues插件,且Confluence与Jira的应用链接已正确建立,具备访问Jira数据的权限。
  • 宏的Body processing设置为Rendered,Output设置为Rendered,确保Velocity模板能被正确解析。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.01 06:05:55