在我的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
答案 0 :(得分: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())
}
答案 1 :(得分:1)
谢谢@Krishan 只有一条评论,新的JIRA 7 / ScriptRunner有静态类型检查的问题。替换“newSeqID”的定义
def newSeqId = 0
带
Long newSeqId = 0