You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何获取Jira中指定项目下某CustomField的所有选项?

解决Jira跨项目复制问题时自定义字段选项匹配的问题

在跨项目复制Jira问题并保留自定义字段值时,核心问题是不同项目的自定义字段可能绑定了独立的选项集,直接复用原问题的选项ID会失效。你遇到的availableOptions为空的情况,大概率是没有正确获取目标项目对应的字段配置方案,下面是具体的解决步骤和脚本示例:

1. 先确认目标项目的字段配置

首先检查:

  • 目标项目是否已经关联了你要复制的自定义字段
  • 该字段在目标项目的字段配置中是否启用,并且绑定了对应的选项集
    如果这两步没做,脚本肯定拿不到选项。

2. 获取目标项目自定义字段选项的正确脚本

用Groovy脚本(适配Jira Server/Data Center)的话,需要通过FieldConfigScheme关联目标项目和自定义字段,再获取专属选项:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.fields.config.FieldConfigScheme

// 替换成你的实际参数
def customFieldId = "customfield_12345" // 自定义字段ID
def targetProjectKey = "TARGET_PROJ"   // 目标项目Key

def customFieldManager = ComponentAccessor.getCustomFieldManager()
def projectManager = ComponentAccessor.getProjectManager()

// 获取自定义字段和目标项目对象
CustomField customField = customFieldManager.getCustomFieldObject(customFieldId)
def targetProject = projectManager.getProjectByKey(targetProjectKey)

if (!customField || !targetProject) {
    log.error("自定义字段或目标项目不存在")
    return
}

// 获取目标项目对应的字段配置方案
FieldConfigScheme configScheme = customFieldManager.getConfigSchemeForFieldAndProject(customField, targetProject)
if (configScheme) {
    // 获取该方案下的字段配置
    def fieldConfig = configScheme.getOneAndOnlyConfig()
    // 拿到目标项目的可用选项
    def availableOptions = customField.getOptions(fieldConfig)
    
    if (availableOptions) {
        availableOptions.each { option ->
            log.info("选项:${option.value} | ID: ${option.id}")
        }
    } else {
        log.error("该字段在目标项目中未配置任何选项")
    }
} else {
    log.error("自定义字段未关联到目标项目${targetProjectKey}的字段配置方案")
}

3. 匹配原问题与目标项目的选项

拿到两边的选项后,不要用选项ID匹配(不同项目的选项ID通常不同),建议通过以下方式匹配:

  • 选项的显示文本(option.value):如果两个项目的选项文本一致,直接找相同文本的选项
  • 选项的外部ID(如果配置了):适合多语言环境或文本可能变化的场景

示例:复制原问题的单选字段值到目标项目

// 假设原问题的自定义字段值是一个Option对象
def originalOption = originalIssue.getCustomFieldValue(customField) as Option
if (originalOption) {
    // 在目标项目选项中找文本匹配的选项
    def targetOption = availableOptions.find { it.value == originalOption.value }
    if (targetOption) {
        newIssue.setCustomFieldValue(customField, targetOption)
    } else {
        log.warn("目标项目中不存在匹配的选项:${originalOption.value}")
    }
}

4. 常见排查点

  • 脚本权限:运行脚本的账号需要有目标项目的浏览权限和字段配置查看权限
  • 字段类型:如果是用户、日期、文本类字段,不需要匹配选项,直接复制值即可
  • 选项集共享:如果两个项目共用同一个选项集,也可以直接复用选项ID,但不推荐(后续选项集拆分会出问题)

内容的提问来源于stack exchange,提问作者Wired5149

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.31 14:40:45