Jira 插件:通过数据库动态填充 Select 自定义字段
Jira Plugin: Dynamically Populating a Select Custom Field via Database
我一直在尝试使用 Java 制作 Jira 插件,以尝试使用数据库动态填充 SelectCFType。我目前从我发现的一些代码中得到了一个有效的 SelectCFType here。唯一的问题是,在通过数据库填充它时,我不知道从哪里开始。我尝试手动填充一次,但 Jira 在创建工单时给了我一个错误,因为自定义字段的内部数据库中存储的值与我提供的值不同。任何帮助将不胜感激!
Java class
import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.customfields.impl.SelectCFType;
import com.atlassian.jira.issue.customfields.manager.OptionsManager;
import com.atlassian.jira.issue.customfields.option.Option;
import com.atlassian.jira.issue.customfields.option.Options;
import com.atlassian.jira.issue.fields.config.FieldConfigScheme;
import com.atlassian.jira.issue.fields.rest.json.beans.JiraBaseUrls;
import com.atlassian.jira.issue.search.SearchContextImpl;
import org.apache.commons.collections.MultiMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.atlassian.jira.issue.customfields.manager.GenericConfigManager;
import com.atlassian.jira.issue.customfields.persistence.CustomFieldValuePersister;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.issue.fields.config.FieldConfig;
import com.atlassian.jira.issue.fields.layout.field.FieldLayoutItem;
import com.atlassian.plugin.spring.scanner.annotation.imports.JiraImport;
import javax.inject.Inject;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class DynamicSelectCF extends SelectCFType{
private static final Logger log = LoggerFactory.getLogger(DynamicSelectCF.class);
private final OptionsManager optionsManager;
private final JiraBaseUrls jiraBaseUrls;
@Inject
public DynamicSelectCF(@JiraImport CustomFieldValuePersister customFieldValuePersister,
@JiraImport OptionsManager optionsManager,
@JiraImport GenericConfigManager genericConfigManager,
@JiraImport JiraBaseUrls jiraBaseUrls){
super(customFieldValuePersister, optionsManager, genericConfigManager, jiraBaseUrls);
this.optionsManager = optionsManager;
this.jiraBaseUrls = jiraBaseUrls;
}
@Override
public Map<String, Object> getVelocityParameters(final Issue issue,
final CustomField field,
final FieldLayoutItem fieldLayoutItem) {
final Map<String, Object> parameters = super.getVelocityParameters(issue, field, fieldLayoutItem);
FieldConfig fieldConfiguration = null;
if(issue == null)
{
fieldConfiguration = field.getReleventConfig(new SearchContextImpl());
} else
{
fieldConfiguration = field.getRelevantConfig(issue);
}
Options options = this.optionsManager.getOptions(fieldConfiguration);
if (options.isEmpty()) {
this.optionsManager.createOption(fieldConfiguration, null, new Long(1), "A");
this.optionsManager.createOption(fieldConfiguration, null, new Long(2), "B");
}
options = this.optionsManager.getOptions(fieldConfiguration);
Map<Long, String> results = new HashMap<Long, String>();
Long selectedId= (long) -1;
boolean selected = false;
Object value = field.getValue(issue);
if (value!=null) {
selected=true;
}
for (Option option : (Iterable<Option>) options) {
results.put(option.getOptionId(), option.getValue());
if (selected && value.toString().equals(option.getValue())) {
selectedId = option.getOptionId();
}
}
parameters.put("results", results);
parameters.put("selectedId", selectedId);
return parameters;
}
}
edit.vm(网络资源)
#* @vtlvariable name="results" type="java.util.Map" *#
#* @vtlvariable name="selectedId" type="java.lang.String" *#
#controlHeader ($action $customField.id $customField.name $fieldLayoutItem.required $displayParameters.noHeader)
<select name="$customField.id" id="$customField.id" >
<option value="-1">Not selected</option>
#foreach ($mapEntry in $results.entrySet())
#if ( $selectedId == $mapEntry.key )
<option selected="selected" value="$mapEntry.key">$mapEntry.value</option>
#else
<option value="$mapEntry.key">$mapEntry.value</option>
#end
#end
</select>
#controlFooter ($action $fieldLayoutItem.fieldDescription $displayParameters.noHeader)
所以我想我想出了一个好方法。我创建了一个方法,到目前为止效果很好。
public void populatingOprions(FieldConfig fieldConfiguration, ArrayList<String> optionPopulation){
for (int i = 0; i < population.size(); i++){
this.optionsManager.createOption(fieldConfiguration, null, new Long(i), optionPopulation.get(i));
}
hasBeenCalled = true;
}
在 getVelocityParameters 方法中,我在 if(options.isEmpty()) 语句
下添加了这段代码
if(!hasBeenCalled) {
String arr[] = {"---------------", "This", "Is", "Just", "A", "Test", "For", "Population"};
for(int i = 0; i < arr.length; i++){
population.add(arr[i]);
}
populatingOprions(fieldConfiguration, population);
}
我有一个包含所有值的 ArrayList,我想我们有一个 mySQL 数据库,从中获取数据似乎很容易。 hasBeenCalled 是一个布尔值,用于跟踪是否已调用该方法来填充 SelectCF 的选项,因此它不会重复它们。
并不是要阻止您享受编写 Jira 插件的乐趣,但几乎没有其他选择
- https://marketplace.atlassian.com/apps/23337/elements-connect-external-data-fields?hosting=cloud&tab=overview
- 您也可以使用 ScriptRunner 编写脚本来执行此操作
我们使用第二种方法用来自公司 REST API 的数据填充单行文本字段。感觉就像使用了组件之类的预输入字段。
我一直在尝试使用 Java 制作 Jira 插件,以尝试使用数据库动态填充 SelectCFType。我目前从我发现的一些代码中得到了一个有效的 SelectCFType here。唯一的问题是,在通过数据库填充它时,我不知道从哪里开始。我尝试手动填充一次,但 Jira 在创建工单时给了我一个错误,因为自定义字段的内部数据库中存储的值与我提供的值不同。任何帮助将不胜感激!
Java class
import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.customfields.impl.SelectCFType;
import com.atlassian.jira.issue.customfields.manager.OptionsManager;
import com.atlassian.jira.issue.customfields.option.Option;
import com.atlassian.jira.issue.customfields.option.Options;
import com.atlassian.jira.issue.fields.config.FieldConfigScheme;
import com.atlassian.jira.issue.fields.rest.json.beans.JiraBaseUrls;
import com.atlassian.jira.issue.search.SearchContextImpl;
import org.apache.commons.collections.MultiMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.atlassian.jira.issue.customfields.manager.GenericConfigManager;
import com.atlassian.jira.issue.customfields.persistence.CustomFieldValuePersister;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.issue.fields.config.FieldConfig;
import com.atlassian.jira.issue.fields.layout.field.FieldLayoutItem;
import com.atlassian.plugin.spring.scanner.annotation.imports.JiraImport;
import javax.inject.Inject;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class DynamicSelectCF extends SelectCFType{
private static final Logger log = LoggerFactory.getLogger(DynamicSelectCF.class);
private final OptionsManager optionsManager;
private final JiraBaseUrls jiraBaseUrls;
@Inject
public DynamicSelectCF(@JiraImport CustomFieldValuePersister customFieldValuePersister,
@JiraImport OptionsManager optionsManager,
@JiraImport GenericConfigManager genericConfigManager,
@JiraImport JiraBaseUrls jiraBaseUrls){
super(customFieldValuePersister, optionsManager, genericConfigManager, jiraBaseUrls);
this.optionsManager = optionsManager;
this.jiraBaseUrls = jiraBaseUrls;
}
@Override
public Map<String, Object> getVelocityParameters(final Issue issue,
final CustomField field,
final FieldLayoutItem fieldLayoutItem) {
final Map<String, Object> parameters = super.getVelocityParameters(issue, field, fieldLayoutItem);
FieldConfig fieldConfiguration = null;
if(issue == null)
{
fieldConfiguration = field.getReleventConfig(new SearchContextImpl());
} else
{
fieldConfiguration = field.getRelevantConfig(issue);
}
Options options = this.optionsManager.getOptions(fieldConfiguration);
if (options.isEmpty()) {
this.optionsManager.createOption(fieldConfiguration, null, new Long(1), "A");
this.optionsManager.createOption(fieldConfiguration, null, new Long(2), "B");
}
options = this.optionsManager.getOptions(fieldConfiguration);
Map<Long, String> results = new HashMap<Long, String>();
Long selectedId= (long) -1;
boolean selected = false;
Object value = field.getValue(issue);
if (value!=null) {
selected=true;
}
for (Option option : (Iterable<Option>) options) {
results.put(option.getOptionId(), option.getValue());
if (selected && value.toString().equals(option.getValue())) {
selectedId = option.getOptionId();
}
}
parameters.put("results", results);
parameters.put("selectedId", selectedId);
return parameters;
}
}
edit.vm(网络资源)
#* @vtlvariable name="results" type="java.util.Map" *#
#* @vtlvariable name="selectedId" type="java.lang.String" *#
#controlHeader ($action $customField.id $customField.name $fieldLayoutItem.required $displayParameters.noHeader)
<select name="$customField.id" id="$customField.id" >
<option value="-1">Not selected</option>
#foreach ($mapEntry in $results.entrySet())
#if ( $selectedId == $mapEntry.key )
<option selected="selected" value="$mapEntry.key">$mapEntry.value</option>
#else
<option value="$mapEntry.key">$mapEntry.value</option>
#end
#end
</select>
#controlFooter ($action $fieldLayoutItem.fieldDescription $displayParameters.noHeader)
所以我想我想出了一个好方法。我创建了一个方法,到目前为止效果很好。
public void populatingOprions(FieldConfig fieldConfiguration, ArrayList<String> optionPopulation){
for (int i = 0; i < population.size(); i++){
this.optionsManager.createOption(fieldConfiguration, null, new Long(i), optionPopulation.get(i));
}
hasBeenCalled = true;
}
在 getVelocityParameters 方法中,我在 if(options.isEmpty()) 语句
下添加了这段代码if(!hasBeenCalled) {
String arr[] = {"---------------", "This", "Is", "Just", "A", "Test", "For", "Population"};
for(int i = 0; i < arr.length; i++){
population.add(arr[i]);
}
populatingOprions(fieldConfiguration, population);
}
我有一个包含所有值的 ArrayList,我想我们有一个 mySQL 数据库,从中获取数据似乎很容易。 hasBeenCalled 是一个布尔值,用于跟踪是否已调用该方法来填充 SelectCF 的选项,因此它不会重复它们。
并不是要阻止您享受编写 Jira 插件的乐趣,但几乎没有其他选择
- https://marketplace.atlassian.com/apps/23337/elements-connect-external-data-fields?hosting=cloud&tab=overview
- 您也可以使用 ScriptRunner 编写脚本来执行此操作
我们使用第二种方法用来自公司 REST API 的数据填充单行文本字段。感觉就像使用了组件之类的预输入字段。