简体   繁体   English

Jira自定义发行说明:速度if(..)语句和宏返回的值

[英]Jira Custom Release Notes: Velocity if(..) Statements and Returned Values from Macros

I have been experimenting with Jira's custom release notes feature, which requires writing a custom .vm file. 我一直在尝试Jira的自定义发行说明功能,该功能需要编写一个自定义.vm文件。 This is my first time working with Velocity and would greatly appreciate a hand with this minor issue: 这是我第一次使用Velocity,非常感谢您解决这个小问题:

I have a simple macro that checks is an $issue has a custom field called For Release Notes and gets its value. 我有一个简单的宏,它检查一个$issue具有一个名为For Release Notes的自定义字段,并获取其值。 Based on this value, I want Velocity to either output the issue summary, or skip it. 基于此值,我希望Velocity输出问题摘要或跳过它。

The problem is that even though the macro is returning true or false , I cannot get the if() statement to properly process the value. 问题是,即使宏返回的是truefalse ,我也无法获取if()语句来正确处理该值。

Macro: 巨集:

#macro(getForReleaseNotes $issue $customFieldManager)
    #set ($customFields = $customFieldManager.getCustomFieldObjects($issue.project.getLong("id"), $issue.issueType.getString("id")))
    #set( $retVal = "false" )
    #foreach($customField in $customFields)
        #if($customField.name.equals("For Release Notes"))
            #if($customField.getValue($issue)) #set( $retVal = "true" ) 
            #end
        #end
    #end
    $retVal
#end

HTML: HTML:

#foreach ($issueType in $issueTypes)
    #if($issueType.issues.size() > 0)
        <h2>$textUtils.htmlEncode($issueType.name)</h2>
        <ul>
        #foreach ($issue in $issueType.issues)
            #set( $tester = "#getForReleaseNotes($issue $customFieldManager)" )
            #if($tester == "true")
                <li>[<a href='$requestContext.canonicalBaseUrl/browse/$issue.key'>$issue.key</a>] - $textUtils.htmlEncode($issue.summary)</li>
            #else 
                <li> Value for macro: #getForReleaseNotes($issue $customFieldManager), $tester </li>
            #end
        #end
    </ul>
    #end
#end

I have tried a number of combinations within the if() , from if( $tester.equals("getForReleaseNotes(..))" ) , to simply if( "getForReleaseNotes(..) ) . 我已经在if()尝试了多种组合,从if( $tester.equals("getForReleaseNotes(..))" )到简单的if( "getForReleaseNotes(..) )

Now, the macro actually works and returns $retVal = false (or is it $retVal = "false" and does that make a difference?) when the issue does not have this property/the property is set to false . 现在,当问题不具有此属性/该属性设置为false时,宏实际上可以工作并返回$retVal = false (或者$retVal = "false"会有所不同吗?)。 The Macro will likewise return true if otherwise. 否则,宏将同样返回true The issue is simply getting the if(..) statement to properly evaluate, because as of right now it says everything I pass into it is false , and issues never get printed. 问题只是让if(..)语句正确评估,因为截至目前它说我传递给它的所有内容都是false ,而且问题从未得到印制。

Any help on this issue would be greatly appreciated. 在这个问题上的任何帮助将不胜感激。 Thanks for reading. 谢谢阅读。

I have found a solution for this problem, though I do not believe it to be the most efficient because it requires looping through the list of issues twice. 我已经找到了解决此问题的方法,尽管我不认为它是最有效的,因为它需要两次遍历问题列表。

The first loop checks if there are issues of the given issue type, and if any of those issues are marked as "For Release Notes." 第一个循环检查是否存在给定问题类型的问题,以及这些问题中的任何一个是否标记为“针对发行说明”。

If the first loop does find an issue marked For Release Notes, it moves to the second loop. 如果第一个循环确实找到了标记为“发行说明”的问题,那么它将移至第二个循环。 The second loop then pulls out the details of all the issues within that issue type that are marked For Release Notes. 然后,第二个循环提取出该问题类型中标记为“发行说明”的所有问题的详细信息。

NOTE: I added another field called "Release Note," so users have more control over what appears in the generated release notes. 注意:我添加了另一个字段,称为“发行说明”,因此用户可以更好地控制生成的发行说明中显示的内容。 If the field is not set, it defaults to using the issue summary. 如果未设置该字段,则默认使用问题摘要。

#if($issueType.issues.size() > 0)
        #set($flag = "")
        #foreach ($issue in $issueType.issues)
            #set ($customFields = $customFieldManager.getCustomFieldObjects($issue.project.getLong("id"), $issue.issueType.getString("id")))
            #foreach($customField in $customFields)
                #if($customField.name.equals("For Release Notes"))
                    #if($customField.getValue($issue))

                        #if($flag==$textUtils.htmlEncode($issueType.name))

                        #else                       
                            <h2>$textUtils.htmlEncode($issueType.name)</h2>
                            #set($flag = $textUtils.htmlEncode($issueType.name))
                        #end

                    #end
                #end
            #end
        #end

        ## Go through the list again, displaying all issues that are "for release notes."
        <ul>       
        #foreach ($issue in $issueType.issues)
            #set ($customFields = $customFieldManager.getCustomFieldObjects($issue.project.getLong("id"), $issue.issueType.getString("id")))
            #foreach($customField in $customFields)
                #if($customField.name.equals("For Release Notes"))
                    #if($customField.getValue($issue))

                        ## If the issue has the "Release Note" field set, use that, otherwise use the summary
                        #foreach($customField in $customFields)
                            #if($customField.name.equals("Release Note"))
                                #if($customField.getValue($issue))
                                    <li>[<a href='$requestContext.canonicalBaseUrl/browse/$issue.key'>$issue.key</a>] - $textUtils.htmlEncode($customField.getValue($issue))</li>
                                #else
                                    <li>[<a href='$requestContext.canonicalBaseUrl/browse/$issue.key'>$issue.key</a>] - $textUtils.htmlEncode($issue.summary)</li>
                                #end
                            #end
                        #end

                    #end
                #end
            #end
        #end
        </ul>       
    #end

http://velocity.apache.org/engine/devel/vtl-reference-guide.html提供了一些Velocity中条件的示例,并且您发现它们并不十分明显。

Create the following macro and simply call it from whatever place in your vm file where you want to include the issue summary. 创建以下宏,然后只需在vm文件中要包含问题摘要的任何地方调用它即可。

#macro( myMacro )
#set( $customFieldObj   = $customFieldManager.getCustomFieldObjectByName('For Release Notes') )
#set( $customFieldValue = $issue.getCustomFieldValue($customFieldObj) )
## next line only makes sure the 2 variables are not null
#if( $customFieldObj && $customFieldValue )
## now output the issue summary
$issue.getSummary()
#end
#end

According to the API , getCustomFieldObjectByName will return the 1st custom field object with the give name, so if you have more than one custom field called 'For Release Notes', you wanna try the method getCustomFieldObject and pass the Long id of the field as a parameter. 根据API ,getCustomFieldObjectByName将返回具有给定名称的第一个自定义字段对象,因此,如果您有多个名为“ For Release Notes”的自定义字段,则可以尝试使用getCustomFieldObject方法并将该字段的Long ID传递为参数。

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

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