/src/main/resources/com/onresolve/jira/groovy/canned/admin/BulkImportCustomFieldValues.groovy
Groovy | 147 lines | 122 code | 25 blank | 0 comment | 14 complexity | 983b67ead14bb98ee28bcb46047bd284 MD5 | raw file
- package com.onresolve.jira.groovy.canned.admin
-
- import com.atlassian.jira.ComponentManager
- import com.atlassian.jira.component.ComponentAccessor
- import com.atlassian.jira.issue.customfields.impl.CascadingSelectCFType
- import com.atlassian.jira.issue.customfields.option.Option
- import com.atlassian.jira.issue.fields.config.FieldConfigScheme
- import com.atlassian.jira.issue.fields.config.manager.FieldConfigSchemeManager
- import com.atlassian.jira.util.ErrorCollection
- import com.atlassian.jira.util.SimpleErrorCollection
- import com.onresolve.jira.groovy.canned.CannedScript
- import com.onresolve.jira.groovy.canned.utils.CannedScriptUtils
- import org.apache.log4j.Category
-
- class BulkImportCustomFieldValues implements CannedScript {
-
- public static String FIELD_IMPORT_VALUES = "FIELD_IMPORT_VALUES"
- public static String FIELD_FCS = "FIELD_FCS"
-
- Category log = Category.getInstance(BulkImportCustomFieldValues.class)
- def componentManager = ComponentManager.getInstance()
- def customFieldManager = componentManager.getCustomFieldManager()
- def optionsManager = ComponentAccessor.getOptionsManager()
-
- String getName() {
- return "Bulk import custom field values"
- }
-
- String getDescription() {
- return "Bulk import custom field values"
- }
-
- public String getHelpUrl() {
- "https://studio.plugins.atlassian.com/wiki/display/GRV/Built-In+Scripts#Built-InScripts-BulkImportCustomFieldValues"
- }
-
- List getCategories() {
- ["ADMIN"]
- }
-
- List getParameters(Map params) {
- [
- [
- Name:FIELD_FCS,
- Label:"Name of custom field config scheme",
- Description:"The name of the field config scheme in which to import these values",
- Type:"groupedlist", // multilist requires map of maps
- Values: CannedScriptUtils.getFieldConfigSchemesByCustomField(true)
- ],
- [
- Name:FIELD_IMPORT_VALUES,
- Label:"Values to import",
- Description:"Each line will be imported as a new field value. For cascading selects, indent child options " +
- "with a tab or two spaces. If a value exists it will be skipped.",
- Type:"text",
- ],
- ]
- }
-
- ErrorCollection doValidate(Map params, boolean forPreview) {
- def errorCollection = new SimpleErrorCollection()
-
- def importValues = params[FIELD_IMPORT_VALUES]
- FieldConfigScheme scheme = getSchemeFromParams(params)
-
- if (!scheme) {
- errorCollection.addError(FIELD_FCS, "No field config scheme with this ID found, or you have not selected one.")
- }
- else {
- if (!importValues) {
- errorCollection.addError(FIELD_IMPORT_VALUES, "Please enter values to import.")
- }
- }
-
- errorCollection
- }
-
- private FieldConfigScheme getSchemeFromParams(Map params) {
- def fcsId = params[FIELD_FCS] as Long
- def fieldConfigSchemeManager = ComponentManager.getComponentInstanceOfType(FieldConfigSchemeManager.class)
- def scheme = fieldConfigSchemeManager.getFieldConfigScheme(fcsId)
- scheme
- }
-
- Map doScript(Map params, boolean forPreview = false) {
- String importValues = params[FIELD_IMPORT_VALUES]
- FieldConfigScheme fcs = getSchemeFromParams(params)
- Integer existingOptions = 0
- Integer newOptions = 0
-
- def oneAndOnlyConfig = fcs.getOneAndOnlyConfig()
- def cf = oneAndOnlyConfig.getCustomField()
- def isCascadingSelect = cf.getCustomFieldType() instanceof CascadingSelectCFType
-
- Option previousOption = null
-
- importValues.eachLine {String line ->
-
- def option = optionsManager.getOptions(oneAndOnlyConfig).getOptionForValue(line.trim(),
- (isCascadingSelect && isIndented(line)) ? previousOption?.optionId : null)
- if (option) {
- existingOptions++
- }
- else {
- newOptions++
- if (isIndented(line) && isCascadingSelect) {
- if (!forPreview) {
- optionsManager.createOption(oneAndOnlyConfig, previousOption.optionId, null, line.trim())
-
- }
- log.debug("Adding option with value ${line.trim()} to parent option ${previousOption?.getValue()}")
- }
- else {
- log.debug("Adding option with value ${line.trim()}")
- if (!forPreview) {
- option = optionsManager.createOption(oneAndOnlyConfig, null, null, line.trim())
- }
- }
- }
- if (!isIndented(line)) {
- previousOption = option
- }
- }
-
- def rt = new StringBuffer()
- rt << (forPreview ? "Will add" : "Added")
-
- rt << " $newOptions new option(s) to scheme: <b>${fcs.name}</b> for custom field:" <<
- " <b>${cf.name}</b>."
- if (existingOptions) {
- rt << " $existingOptions option(s) already existed"
- }
- return [output: rt.toString()]
- }
-
- private boolean isIndented(String line) {
- line ==~ /^\t.*/ || line ==~ /^\s\s.*/
- }
-
- String getDescription(Map params, boolean forPreview) {
- doScript(params, forPreview).get("output")
- }
-
- Boolean isFinalParamsPage(Map params) {
- return true
- }
- }