Custom field options having null sequence

Arpit Doshi May 8, 2014

Hi,

We fall into this issue https://jamieechlin.atlassian.net/browse/GRV-282

But I am not able to access workaround script from here https://gist.github.com/jamieechlin/5917773

Can anyone please post me here as an answer ??

Thanks,

Arpit

2 answers

0 votes
BenjiI
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
May 8, 2014
package examples
 
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.customfields.option.Option
import com.atlassian.jira.issue.fields.config.FieldConfig
import com.atlassian.jira.issue.fields.layout.field.AbstractFieldLayoutManager
import com.atlassian.jira.issue.managers.DefaultCustomFieldManager
import org.apache.log4j.Level
import org.apache.log4j.Logger
 
public class FixBrokenOptionsScript {
 
    def log = Logger.getLogger("examples.FixBrokenOptionsScript")
 
    // set isPreview to true to see what would be done - check the logs
    def isPreview = false
 
    public void run() {
 
        log.setLevel(Level.DEBUG)
        Logger.getLogger(AbstractFieldLayoutManager.class).setLevel(Level.WARN)
        Logger.getLogger(DefaultCustomFieldManager.class).setLevel(Level.OFF)
 
        def totalOptionsToUpdate = 0
        def optionsManager = ComponentAccessor.getOptionsManager()
 
        def Map<FieldConfig, List<Option>> optionsByFieldConfig = optionsManager.getAllOptions().groupBy {
            try {
                it.relatedCustomField
            }
            catch (e) {
                // some options can be broken, they refer to a deleted field config or something
                return null
            }
        }

0 votes
BenjiI
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
May 8, 2014
optionsByFieldConfig.each {fieldConfig, fcOptionList ->
            fcOptionList.groupBy {it.parentOption}.each {parentOption, optionList ->
 
                if (optionList.any {it.sequence == null}) {
 
                    log.debug("Modify options for field config ${fieldConfig.name}")
 
                    // find highest sequence number to use as the base
                    def highestSeq = optionList*.sequence.max() ?: -1
                    highestSeq++
 
                    optionList.findAll {it.sequence == null}.sort{it.optionId}.eachWithIndex {Option option, Integer kount ->
 
                        log.debug("Update option with ID ${option.optionId} and value ${option.value}")
                        if (! isPreview) {
                            option.setSequence(highestSeq + kount)
                            option.store()
                        }
                        totalOptionsToUpdate++
                    }
                }
            }
        }
 
        log.debug("Updated or will update: ${totalOptionsToUpdate} options in total")
    }
}
 
new FixBrokenOptionsScript().run()

Suggest an answer

Log in or Sign up to answer