Script Fields 脚本字段
Extend the information shown for an issue using ScriptRunner Script Fields. Use Groovy to calculate or consolidate data from one or more existing fields and display them in custom fields.
使用 ScriptRunner 脚本字段扩展显示问题的信息。使用 Groovy 计算或 consolide 从一个或多个现有字段中的数据,并将它们显示在自定义字段中。
You cannot manually edit the value, or update the value of a script field as the value is calculated at the time an issue is displayed or updated (in exactly the same way as a custom field plugin). It is possible to search on these values if you set up one of the four shipped searchers. If you do, the value is calculated and stored in the index at the time that the issue is indexed, for example, when it is modified or transitioned, or when you do a full reindex.
您无法手动编辑值,也无法更新脚本字段的值,因为该值是在问题显示或更新时计算的(与自定义字段插件完全相同)。如果您设置了四个预装搜索器之一,就可以对这些值进行搜索。如果这样做,该值将在问题被索引时计算并存储在索引中,例如在修改或转换问题时,或者进行完整重新索引时。
You could use a script field to:
您可以使用脚本字段来:
- Pull in data from an external system (such as a connected database).
从外部系统(如连接的数据库)中提取数据。 - Show a value from a linked instance.
显示来自链接实例的值。 - Show a value calculated from the values of other issue fields.
显示从其他问题字段的值计算出的值。 - Show a value calculated from the values of fields in other issues.
显示从其他问题字段值计算出的值。
ScriptRunner Script Fields are not available on the Issue Detail view of scrum or kanban boards.
ScriptRunner 脚本字段在 Scrum 或 Kanban 面板的问题详细视图上不可用。
New to Script Fields? Check out our Script Field Tutorial.
新手 Script Fields?查看我们的 Script Field 教程。
Getting Started 入门
The Script Fields page displays the different field configurations for this field type. It’s possible to have different scripts for different field configs for the same custom field, but don’t do this right now to keep things simple.
脚本字段页面显示了此字段类型的不同字段配置。对于相同的自定义字段,可以为不同字段配置使用不同的脚本,但现在为了保持简单,请不要这样做。
Select Create Script Field (see screenshot below) to see a list of built-in script fields available.
选择“创建脚本字段”(请参阅下面的截图)以查看可用的内置脚本字段列表。
Select the Custom Script Field.
选择自定义脚本字段。
You can either point to a script accessible to Jira using an absolute path, or a path relative to Jira’s working directory (preferred), or type your script in inline.
您可以指向一个使用绝对路径可访问 Jira 的脚本,或者相对于 Jira 工作目录的路径(首选),或者在内联中输入您的脚本。
The final part of configuration is to choose which renderer to use. The renderer also determines which issue panel, eg Dates, People, or the default main panel the value will be shown in in the view issue screen. As of 3.0, script fields using either user template can be used in notification schemes, which means you can dynamically generate the recipient of an email, eg based on component lead, priority, etc.
配置的最后一部分是选择要使用哪个渲染器。渲染器还确定要在查看问题屏幕中显示值的问题面板,例如日期、人员或默认的主面板。从 3.0 版本开始,使用任何用户模板的脚本字段都可以在通知方案中使用,这意味着您可以动态生成电子邮件的接收者,例如基于组件负责人、优先级等。
The names are taken from one of the internal Jira files so might not seem to make total sense. Use the following table as a guideline for which one to use:
这些名称取自内部 Jira 文件之一,因此可能看起来并不完全合理。请使用以下表格作为使用哪个的指南:
Your script returns… 您的脚本返回.. | Template | Issue Panel 问题小组 |
---|---|---|
A string 一个字符串 | Free text field 自由文本字段 | main |
A string as html 一个作为 HTML 的字符串 | HTML | main |
A date 一个约会 | Date Time 日期时间 | Dates |
A number 一个数字 | Number Field 数字领域 | main |
A user 一个用户 | User Picker 用户选择器 | People |
List of users 用户列表 | Multi User Picker 多用户选择器 | People |
A group 一组 | Group Picker 组选择器 | main |
List of groups 组列表 | Multi Group Picker 多组选择器 | main |
A project 一个项目 | Project Picker 项目选择器 | main |
Something else 其他事情 | Custom | main |
When getting started use the "free text" template without any indexer.
当开始使用时,请使用“自由文本”模板,不要使用任何索引器。
Obviously all of these three things need to tally:
显然,所有这三件事情都需要相符
Your return type from the script
您从脚本中返回的类型The indexer if you are using one
如果您正在使用索引器The template Next, get the key of an existing issue and enter it in the "Preview … " box, and press Go. Your script will be compiled and executed using the issue, so you can see how the field will appear on that particular issue.
接下来,获取现有问题的关键字,并将其输入到“预览...”框中,然后按“Go”键。您的脚本将使用该问题进行编译和执行,以便您可以看到该字段在该特定问题上的显示方式。
Depending on the complexity of the script you will need to test with multiple issues and different inputs.
根据脚本的复杂程度,您需要使用多个问题和不同的输入进行测试。
Indexing Free Text Fields
索引自由文本字段
If you use one of the built-in text searchers, you can query on this field.
如果您使用内置的文本搜索器之一,您可以在此字段上查询。
To use them in filter statistics gadgets (for example Two Dimensional Filter Statistics or Heatmap), select one of the Stattable searchers:
要在过滤器统计小工具(例如二维过滤器统计或热力图)中使用它们,请选择一个 Stattable 搜索器:
If you create a script field with a given Template from the ScriptRunner interface, some of the selectable templates will set the custom field's configured Search Template for you on creation. Make sure to choose the correct template before you create your field because once you have a searcher defined, it may require a full re-index to change it to another Search Template if the types are incompatible.
如果您在 ScriptRunner 界面中使用给定的模板创建脚本字段,一些可选择的模板将在创建时为您设置自定义字段的配置搜索模板。在创建字段之前,请确保选择正确的模板,因为一旦定义了搜索器,如果类型不兼容,则可能需要完全重新索引才能将其更改为另一个搜索模板。
Be cautious when converting to the Stattable version of indexers. Stattable searchers can be used in gadgets (as described above). In instances with hundreds of thousands of possible unique string values, the memory demand of Jira increases and may run out if required to render gadgets for fields using these searchers. Therefore, you should only use these indexers if your script field generates a finite number of possible string values (several thousand should be fine).
在将索引器转换为可统计版本时要小心。可统计搜索器可用于小工具(如上所述)。在可能有数十万个唯一字符串值的情况下,Jira 的内存需求会增加,如果需要为使用这些搜索器的字段呈现小工具,则可能会耗尽内存。因此,只有在您的脚本字段生成有限数量的可能字符串值时(几千个应该没问题),您才应该使用这些索引器。
Caching 缓存
It’s worth expanding a little on what I said above. You can only reliably use a calculated field to display a value that is based on the issue’s fields, or subtasks' fields, or linked issues. The reason why is because the value in the Lucene index is only updated when the issue is updated. If you compute some value based on data outside of the current issue, the value in the index may differ from the value displayed on the issue.
值得进一步扩展我上面说的内容。您只能可靠地使用计算字段来显示基于问题字段、子任务字段或关联问题的值。原因是因为 Lucene 索引中的值仅在更新问题时才会更新。如果您根据当前问题之外的数据计算某个值,索引中的值可能与问题上显示的值不同。
Jira asks the field for the value a ridonkulous amount of times - 8 times just for viewing an issue. This means your script will be executed 8 times. Therefore there is a thread local cache… for subsequent requests the computed value is just retrieved from the cache. If the issue has a different last update date the cache is invalidated. As of 2.1.4 (not released at time of writing), the following actions now also invalidate the cache - changes to the script text (for inline scripts) and changes to the last modified date of the script file, if you are using one. If, in previous versions of the plugin, refreshing the page showed a different value, it’s because different threads had different cached values - that should be fixed in 2.1.4.
Jira 为查看问题的值而请求字段的次数非常多 - 仅查看问题就有 8 次。这意味着您的脚本将被执行 8 次。因此有一个线程本地缓存… 对于后续请求,计算出的值只需从缓存中检索。如果问题具有不同的最后更新日期,则缓存将被使无效。截至 2.1.4 版本(撰写时尚未发布),以下操作现在也会使缓存无效 - 更改脚本文本(用于内联脚本)和更改脚本文件的最后修改日期。如果在插件的早期版本中刷新页面显示不同的值,那是因为不同的线程具有不同的缓存值 - 这应该在 2.1.4 版本中得到修复。
If your script relies on data from external system you can invalidate the cache altogether, although you should test first, particularly if you are doing things like running complex JQL queries. To disable the cache add the following line to your code:
如果您的脚本依赖于外部系统的数据,您可以完全使缓存无效,尽管您应该首先进行测试,特别是如果您正在执行诸如运行复杂的 JQL 查询之类的操作。要禁用缓存,请将以下行添加到您的代码中:
That’s an example for disabling it completely, you can use any other variables to return a Boolean. You may have to do this if you are computing data from linked issues, however the indexed value should always be correct.
这是一个完全禁用它的示例,您可以使用任何其他变量来返回一个布尔值。如果您正在计算来自关联问题的数据,可能需要这样做,但索引值应始终正确。
What if your field relies on data outside its scope and you want to search on it? You can’t, Jira doesn’t have this capability… write your own searcher.
如果您的领域依赖于其范围之外的数据,并且您想在其上进行搜索怎么办?您无法在 Jira 上实现这一功能...编写您自己的搜索器。
Performance and execution history
性能和执行历史
On the Script Fields page we provide Performance and History columns that display performance execution history records. Please note that performance and execution history records are only available for custom fields that require you to implement a custom script. Performance and History will remain blank in the rows of custom fields that do not require custom scripts.
在“脚本字段”页面上,我们提供了显示性能和历史列的功能,用于显示性能执行历史记录。请注意,性能和执行历史记录仅适用于需要您实现自定义脚本的自定义字段。对于不需要自定义脚本的自定义字段,性能和历史将保持空白。