I'm trying to create a single select list with a list of the active projects with my Jira instance. There are around 60 projects at the moment, and they all have unique names/keys. So far, I've tried using scriptrunner to create a scripted field, but when that didn't work I moved onto a behaviour instead. Ideally, I would want this field to update itself whenever a user interacts with it, and I want to pull this information directly from Jira itself. My team do not use the built-in archiving process in Jira, instead, we assign it to an archived permission scheme, as you can see in the code below.
import com.atlassian.jira.component.ComponentAccessor
import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript
@BaseScript FieldBehaviours fieldBehaviours
def cfManager = ComponentAccessor.getCustomFieldManager()
def optionsManager = ComponentAccessor.getOptionsManager()
def permSchemeManager = ComponentAccessor.getPermissionSchemeManager()
def prjManager = ComponentAccessor.getProjectManager()
final singleSelectName = 'Project Name'
def singleSelectField = cfManager.getCustomFieldObjectsByName(singleSelectName)[0]
def formSingleSelect = getFieldByName(singleSelectName)
def config = ComponentAccessor.fieldConfigSchemeManager.getRelevantConfig(issueContext, singleSelectField);
def options = optionsManager.getOptions(config)
def arch = permSchemeManager.getSchemeObject("Archived Permission Scheme")
def archivedPrjs = permSchemeManager.getProjects(arch).toArray()
def archivedPrjSize = archivedPrjs.size()
for (int i = 0; i < archivedPrjSize; i++) {
archivedPrjs[i] = archivedPrjs.getAt(i).toString().split(':')[1].substring(1)
}
def projects = prjManager.getProjects().toArray()
def projectsSize = projects.size()
for (int i=0; i<projectsSize;i++){
def currentPrjKey = projects.getAt(i).toString().split(':')[1].substring(1)
projects[i] = prjManager.getProjectByCurrentKey(currentPrjKey).name
}
for (int i = 0; i < archivedPrjSize; i++) {
for (int j = 0; j < projectsSize; j++) {
if (archivedPrjs[i] == projects[j]) {
projects[j] = null
}
}
}
projects.sort()
for (int i = 0; i< archivedPrjSize; i++) {
projects = projects.tail()
}
projectsSize = projects.size()
def projectsMap = projects.collectEntries( {
[(projects.getIndices()):(projects)]
})
formSingleSelect.setFieldOptions(projectsMap)
Can someone suggest a better way to do this, as I am currently stumped as to the best solution?
Many thanks,
Harry