jira-plugin-intellij /src/test/resources/examples/Scratch.groovy

Language Groovy Lines 184
MD5 Hash 75172c8dcf4a7bef963236aee430861e
Repository https://bitbucket.org/sorin/jira-plugin-intellij View Raw File View Project SPDX
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package examples

import com.atlassian.jira.ComponentManager
import com.atlassian.jira.ManagerFactory
import com.atlassian.jira.config.properties.ApplicationProperties
import com.atlassian.jira.issue.fields.config.manager.FieldConfigSchemeManager
import com.atlassian.velocity.VelocityManager
import org.apache.log4j.Logger
import com.atlassian.jira.issue.fields.config.FieldConfigScheme
import com.atlassian.jira.issue.context.manager.JiraContextTreeManager
import com.atlassian.jira.issue.customfields.CustomFieldUtils
import com.atlassian.jira.jql.builder.JqlQueryBuilder
import com.atlassian.jira.issue.search.SearchProvider
import com.atlassian.jira.issue.search.SearchRequest
import com.atlassian.jira.issue.search.SearchResults
import com.atlassian.jira.web.bean.PagerFilter
import com.atlassian.jira.issue.customfields.option.Option
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.fields.option.OptionSetManager
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.IssueChangeHolder
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.util.ImportUtils
import com.atlassian.jira.issue.index.IssueIndexManager
import com.atlassian.jira.issue.customfields.view.CustomFieldParamsImpl
import com.atlassian.jira.issue.customfields.view.CustomFieldParams
import com.atlassian.jira.issue.customfields.impl.TextCFType
import com.atlassian.jira.issue.changehistory.ChangeHistoryManager
import org.apache.log4j.Category
import org.apache.log4j.Level

Logger log = Logger.getLogger(this.class);
log.setLevel(Level.DEBUG)

log.debug("=====================================================================================================")

ComponentManager componentManager = ComponentManager.getInstance()

//def issueManager = componentManager.getIssueManager()
//def issue = issueManager.getIssueObject("JRA-1358")
//issue.setSecurityLevelId(null)
//issue.store()

final ApplicationProperties applicationProperties = ComponentManager.getInstance().getApplicationProperties();

def applicationLinkService = ComponentManager.getComponentInstanceOfType(ApplicationLinkService.class)
applicationLinkService



VelocityManager velocityManager = ManagerFactory.getVelocityManager();
def jiraAuthenticationContext = componentManager.getJiraAuthenticationContext()

def issueManager = componentManager.getIssueManager()
def issue = issueManager.getIssueObject("JRTWO-137")
def fieldLayoutManager = componentManager.getFieldLayoutManager()
def customFieldManager = componentManager.getCustomFieldManager()
def fieldConfigSchemeManager = ComponentManager.getComponentInstanceOfType(FieldConfigSchemeManager.class)
def optionSetManager = ComponentManager.getComponentInstanceOfType(OptionSetManager.class)
def projectManager = componentManager.getProjectManager()
def targetProj = projectManager.getProjectObjByKey("JRTWO")
SearchProvider searchProvider = componentManager.getSearchProvider()
def optionsManager = ComponentAccessor.getOptionsManager()
IssueIndexManager indexManager = ComponentManager.getInstance().getIndexManager()


import org.apache.log4j.Category
import org.apache.log4j.Level
import com.atlassian.applinks.api.EntityLinkService
import com.atlassian.applinks.api.ApplicationLinkService

Category.getInstance("com.onresolve.jira.groovy").setLevel(Level.DEBUG)


def changeHistoryManager = ComponentManager.getComponentInstanceOfType(ChangeHistoryManager.class)
changeHistoryManager.getAllChangeItems()
[].getAt(1)


def cf = customFieldManager.getCustomFieldObjectByName("testTextField")
// cf.getCustomFieldType().class.isAssignableFrom(GenericTextCFType.class)
def assignableFrm = ["com.atlassian.jira.issue.customfields.impl.TextCFType",
    "com.atlassian.jira.issue.customfields.impl.GenericTextCFType"].any {
    try {
        return Class.forName(it).isAssignableFrom(cf.getCustomFieldType().class)
    }
    catch (Exception e) {
        return false
    }
}

log.debug(assignableFrm)

// log.debug TextCFType.class.isAssignableFrom(cf.getCustomFieldType().class)

return
fieldConfigSchemeManager.getConfigSchemesForField(cf).each {fcs ->
    log.debug(fcs.name)
    log.debug(fcs.getConfigs())
}

def value = issue.getCustomFieldValue(cf) as CustomFieldParams
log.debug value
log.debug value.getFirstValueForKey(null).class
log.debug(value.getKeysAndValues().values()*.first()*.optionId)

log.debug optionsManager.findByParentId(10665).find {it.value == "A1"}.optionId

return


def cfValue = issue.getCustomFieldValue(cf) as Option
log.debug cfValue.genericValue

def fieldConfig = fieldConfigSchemeManager.getFieldConfigScheme(11591L).getOneAndOnlyConfig()

fieldConfigSchemeManager.getConfigSchemesForField(cf).each {fcs ->
    if (! fcs.name.startsWith("Default")) {
        fieldConfigSchemeManager.removeFieldConfigScheme(fcs.id)
    }
}

log.debug(fieldConfig.name)

def originalOptions = optionsManager.getOptions(fieldConfig)
log.debug(originalOptions)

JiraContextTreeManager jiraContextTreeManager = new JiraContextTreeManager(projectManager, ComponentManager.getInstance().getConstantsManager())

List contexts = CustomFieldUtils.buildJiraIssueContexts(false,
    null,
    [targetProj.id] as Long[],
    jiraContextTreeManager);

// todo: update original FCS to remove the items selected

def targetFCS = fieldConfigSchemeManager.createFieldConfigScheme(
    new FieldConfigScheme.Builder().setName("my arse").setDescription("description").toFieldConfigScheme(),
    contexts,
    FieldConfigSchemeManager.ALL_ISSUE_TYPES,
    cf
)

log.debug(targetFCS.name)
def targetFieldConfig = targetFCS.getOneAndOnlyConfig()

componentManager.getFieldManager().refresh()

originalOptions.each {Option opt ->
    log.debug(opt.class)
    // todo: enabled,disabled, child options
    optionsManager.createOption(targetFieldConfig, null, null, opt.getValue())
}
// add third opt C
optionsManager.createOption(targetFieldConfig, null, null, "C")

// set default value
cf.getCustomFieldType().setDefaultValue(targetFieldConfig, cf.getCustomFieldType().getDefaultValue(fieldConfig))


def query = JqlQueryBuilder.newBuilder().where().project(targetProj.id).and().issueType("1").and().customField(cf.idAsLong).isNotEmpty().buildQuery()
log.debug(query)

SearchRequest sr = new SearchRequest(query)
SearchResults results = searchProvider.search(sr.getQuery(), componentManager.getJiraAuthenticationContext().getUser(), PagerFilter.getUnlimitedFilter())
log.debug(results.total)

results.issues.each {
    def iss = issueManager.getIssueObject(it.id)

    def oldFieldValue = iss.getCustomFieldValue(cf)

    def targetOption = optionsManager.getOptions(targetFieldConfig).find {it.value == oldFieldValue.value}
    log.debug("targetOption: ${targetOption.genericValue}")
    IssueChangeHolder changeHolder = new DefaultIssueChangeHolder()
    cf.updateValue(null, iss, new ModifiedValue(oldFieldValue, targetOption), changeHolder)

    iss.setCustomFieldValue(cf, targetOption)
    iss.store()
    boolean wasIndexing = ImportUtils.isIndexIssues();
    indexManager.reIndex(issue);
    ImportUtils.setIndexIssues(wasIndexing);
}
Back to Top