Hi all,
Our Jira admin team is trying to save time by using the scriptrunner console to pull all customfields and their details within our instance at the same time.
Our main goal is to reteive the following for each customfield being pulled within the report:
1) Name
2) ID
3) Type
4) Value (if applicable - text fields don't have a 'value')
5) Issue Type the customfield is associated with
So far we have the script below, but it's not working. Can anyone point us in the correct direction?
import com.atlassian.jira.component.ComponentAccessor
import groovy.xml.MarkupBuilder
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def stringWriter = new StringWriter()
def content = new MarkupBuilder(stringWriter)
customFieldManager.customFieldObjects.each { customField ->
def customFieldName = customField.name
def customFieldId = customField.id
def customFieldType = customField.getCustomFieldType().name
def associatedIssueTypes = customField.getAssociatedIssueTypes().collect { it.name }
content.html {
p {
"${customFieldName};${customFieldId};${customFieldType};${associatedIssueTypes}"
}
}
}
stringWriter.toString()
Hi all,
We have figured this out on our own; and just in-case someone else is being required to do the same thing, please find the code below, as it will provide you with all of the details we previously asked for with each detail separated by ";" making it very easy to copy and paste all of the findings into excel and format via the "text to columns" feature.
import com.atlassian.jira.component.ComponentAccessor
import groovy.xml.MarkupBuilder
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def optionsManager = ComponentAccessor.getOptionsManager()
def stringWriter = new StringWriter()
def content = new MarkupBuilder(stringWriter)
content.div {
//Get customfield name, id, and type.
customFieldManager.customFieldObjects.each { customField ->
def customFieldName = customField.name
def customFieldId = customField.id
def customFieldType = customField.getCustomFieldType().name
// Get all values associated with the custom field
def customFieldValues = []
def configurationSchemes = customField.getConfigurationSchemes()
configurationSchemes.each { scheme ->
def fieldConfig = scheme.getOneAndOnlyConfig()
if (fieldConfig) {
def options = optionsManager.getOptions(fieldConfig)
if (options) {
customFieldValues.addAll(options.collect { it.value })
}
}
}
// Get all issue types associated with the custom field
def associatedIssueTypes = customField.getAssociatedIssueTypes()
def associatedIssueTypeNames = associatedIssueTypes ? associatedIssueTypes.collect { it?.name ?: "none" } : ["none"]
//Put everything together in an excel 'formatable' way.
p {
mkp.yieldUnescaped "${customFieldName};${customFieldId};${customFieldType};${customFieldValues.join(", ")};${associatedIssueTypeNames.join(", ")}<br/>"
}
}
}
return stringWriter.toString()
Hi @Michael
For your requirements, you will need to use the REST Endpoint.
Below is a sample working code for your reference:-
import com.atlassian.jira.component.ComponentAccessor
import com.onresolve.scriptrunner.runner.rest.common.CustomEndpointDelegate
import groovy.json.JsonBuilder
import groovy.transform.BaseScript
import groovyx.net.http.HttpResponseDecorator
import groovyx.net.http.RESTClient
import javax.ws.rs.core.MultivaluedMap
import javax.ws.rs.core.Response
@BaseScript CustomEndpointDelegate delegate
extractCustomFieldDetails { MultivaluedMap queryParams ->
def applicationProperties = ComponentAccessor.applicationProperties
def hostUrl = applicationProperties.getString('jira.baseurl')
def username = 'admin'
def password = 'q'
def path = "/rest/api/2/customFields"
final def headers = ['Authorization': "Basic ${"${username}:${password}".bytes.encodeBase64()}", 'Accept': 'application/json'] as Map
def http = new RESTClient(hostUrl)
http.setHeaders(headers)
def resp = http.get(path: path) as HttpResponseDecorator
if (resp.status != 200) {
log.warn 'Commander did not respond with 200 for retrieving project list'
}
def issueJson = resp.data as Map
def customFieldDetails = issueJson['values']
Response.ok(new JsonBuilder(customFieldDetails).toPrettyString()).build()
}
Below is a screenshot of the REST Endpoint configuration:-
You can then invoke the REST Endpoint via the ScriptRunner Console:-
import groovy.json.JsonSlurper
def baseUrl = 'http://localhost:9292'
def hostUrl = "${baseUrl}/rest/scriptrunner/latest/custom/extractCustomFieldDetails".toString()
def response = hostUrl.toURL().text
def json = new JsonSlurper().parseText(response)
def artifacts = json.collect().sort()
artifacts.collect {
log.warn "ID: ${it['id']}, Name: ${it['name']}, Type: ${it['type']}"
}
Below is the sample output:-
I hope this helps to solve your question. :-)
Thank you and Kind regards,
Ram
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hello @Ram Kumar Aravindakshan _Adaptavist_
Thanks for trying to help! Is there any way to reteive all customfield data within the console without creating a REST point?
We are not very comfortable using a REST endpoint due to both un-familiarity with this type of code and security concerns.
Also, we noticed that one of the main datasets for the pull - each customfield's values are not present. Is it also possible to pull each individual field's values? (Assume that values are there to pull)
Thanks,
Mike
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi @Michael
To answer your question, there is no direct way to do this via the ScriptRunner Console unless the issue the custom field(s) belong to are invoked.
Since your goal is to only get the field details directly, the REST Endpoint is the safest approach.
I hope this helps to solve your question. :-)
Thank you and Kind regards,
Ram
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi again @Ram Kumar Aravindakshan _Adaptavist_
Thanks for the additional details - we'll look into the REST endpoint. Is there a way to also pull any values within customfields (if applicable?) within the same report pull?
Thanks again,
Mike
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Micheal
In your last comment, you asked:-
Thanks for the additional details - we'll look into the REST endpoint. Is there a way to also pull any values within customfields (if applicable?) within the same report pull?
Custom field values depend on the issue that owns them. If you intend to extract the value, you need to know which issue you want to extract it from.
Let's say you want to extract the value from all Issues, then you can do something like:-
Issues.search('project = <PROJECT_KEY>').each {
log.warn it.getCustomFieldValue('<CUSTOM_FIELD_NAME>')
}
I hope this helps to solve your question. :-)
Thank you and Kind regards,
Ram
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi again @Ram Kumar Aravindakshan _Adaptavist_
We have never seen or setup anything in Jira where specific values for specific customfields are "owned by specific issues" - so this part doesn't make sense to us.
We aren't at all interested in anything that has to do with issues. We are trying to pull all data relating to specific customfields regardless of whether or not any of these customfields (or their values) have been used within issues out from the backend of Jira / database through the scriptrunner console.
It looks like we will need to go to the scriptrunner devs directly for this type of question.
Thanks for all the help so far.
~Mike
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.