在我的 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);
但即使在此之后我仍然无法克服错误消息。有人可以帮我解决这个问题吗?
就其值(value)而言,我正在使用 Jira:JIRA v6.3.5
最佳答案
经过一段时间的研究,我终于成功地自己解决了这个问题。
下面提到的初始化程序部分应该有助于轻松解决这个问题。
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())
}
关于jira - 处理 Jira 问题创建中 SelectList 的动态选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34715970/