【问题标题】:Dealing with dynamic options for SelectList in Jira issue creation在 Jira 问题创建中处理 SelectList 的动态选项
【发布时间】:2016-01-11 07:12:40
【问题描述】:

在我的 Jira 项目中,我有一个名为“CASE-Environment”的自定义字段。它是一个选择列表(多个值)

我希望此自定义字段具有动态填充的值。在我的实际项目中,我正在使用 RESTFul 调用,其中我从内部 RESTFul 服务获取实际值。但出于演示目的,我将此处的值显示为通过初始化程序硬编码。

我正在使用 Script Runner Jira 插件,它可以让我定义行为,将其与字段相关联。我在行为的初始化部分有以下内容。

import com.onresolve.jira.groovy.user.FieldBehaviours
import com.onresolve.jira.groovy.user.FormField

Map fieldOptions = [:]
fieldOptions.put("-1","None")
fieldOptions.put("100","Dev");
fieldOptions.put("101","Staging");
fieldOptions.put("102","QA");
fieldOptions.put("103","Production");

FormField env = getFieldByName("CASE-Environment");
env.setFieldOptions(fieldOptions)

当我尝试创建问题时,我能够成功地在 UI 上看到此值。

但是当我尝试提交问题时,我遇到了如下所示的 Jira 验证错误消息

Invalid value '102' passed for customfield 'CASE-Environment'

我猜 Jira 无法识别为我的 SelectList [多个值] 动态添加的选项值

我什至尝试为该字段构建一个映射,其中每次自定义字段 CASE-Environment 下发生更改时都会触发我的行为。

映射代码如下:

import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.MutableIssue;
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.customfields.option.Options
import com.atlassian.jira.issue.customfields.option.Option

import com.onresolve.jira.groovy.user.FieldBehaviours
import com.onresolve.jira.groovy.user.FormField


def adddOption = {
    Issue issue,
    CustomField customField,
    String value ->
        Options l = ComponentAccessor.getOptionsManager().getOptions(customField.getRelevantConfig(issue))
        int nextSequence = l.isEmpty() ? 1 : l.getRootOptions().size() + 1;
        Option newOption = ComponentAccessor.getOptionsManager().createOption(customField.getRelevantConfig(issue), null, (Long)nextSequence, value);
        return newOption;
}

FormField environment = getFieldById(fieldChanged)

Issue issue = getUnderlyingIssue()
MutableIssue mutableIssue = ComponentAccessor.getIssueManager().getIssueObject(issue.id);

CustomField field = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("CASE-Environment")
List<Option> allOptions = new LinkedList<>();
List<String> chosenValues = (List<String>) mutableIssue.getCustomFieldValue(field);

for (String eachValue : chosenValues) {
    allOptions.add(adddOption(issue, field, eachValue));
}
mutableIssue.setCustomFieldValue(field, allOptions);

但即使在此之后,我仍然无法通过错误消息。有人可以帮我解决这个问题吗?

为了它的价值,我正在使用 Jira:JIRA v6.3.5

【问题讨论】:

    标签: jira jira-plugin


    【解决方案1】:

    所以在玩了很长时间之后,我终于设法自己破解了。

    下面提到的初始化部分应该有助于轻松解决这个问题。

    import com.atlassian.jira.component.ComponentAccessor
    
    def optionsManager = ComponentAccessor.getOptionsManager()
    def fieldManager = ComponentAccessor.getCustomFieldManager()
    def envFld = fieldManager.getCustomFieldObjectByName("CASE-Environment")
    def fieldConfig = envFld.getRelevantConfig(getIssueContext())
    def newSeqId = 0
    //For the sake of e.g., I am using a initialized array. In real life one can
    // construct this by querying a DB or hitting a RestFul service etc.,
    def envs = ["QA", "Staging", "Dev", "Production"]
    def issueService = ComponentAccessor.getIssueService()
    def issueInputParameters = issueService.newIssueInputParameters()
    for (String env : envs) {
        def option = optionsManager.createOption(fieldConfig, null, newSeqId++, env)
        issueInputParameters.addCustomFieldValue(envFld.idAsLong, option.optionId.toString())
    }
    

    【讨论】:

      【解决方案2】:

      感谢@Krishan 只有一条评论,新的 JIRA 7/ScriptRunner 有静态类型检查问题。替换“newSeqID”的定义

      def newSeqId = 0
      

      Long newSeqId = 0
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-03-16
        • 1970-01-01
        • 2019-11-25
        • 2012-06-08
        • 2015-05-21
        • 2023-02-06
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多