Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 14 Current »

Purpose

Hosting

Compatible Tools

Dump data from multiple ProForma fields into the Jira Description field.

Server or Data Center

Any scripting tool, such as JMWE, Power Scripts or Scriptrunner, that uses Groovy.

Script

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.bc.issue.properties.IssuePropertyService
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.entity.property.EntityPropertyService
import org.apache.commons.lang3.StringUtils

//// When using this script in the script console uncomment the next line:
Issue issue = ComponentAccessor.getIssueManager().getIssueObject("ISSUE-1")
String formName = "Form Name"
def fieldNames = ["Field One", "Field Two", "Text Field", "Radio Field", "Checkboxes Field"]
String noValue = "No value"
ApplicationUser loggedInUser = ComponentAccessor.jiraAuthenticationContext.getLoggedInUser()
IssuePropertyService issuePropertyService = ComponentAccessor.getComponentOfType(IssuePropertyService.class)
IssueManager issueManager = ComponentAccessor.getIssueManager()

Closure updateDescription = { MutableIssue i, String fieldValues ->
    i.setDescription(fieldValues)
    //i.setDescription(i.description + " " + fieldValues)
    issueManager.updateIssue(loggedInUser, i, EventDispatchOption.DO_NOT_DISPATCH, false)
}

Closure getFormIds = { def property, String name ->
	def slurper = new groovy.json.JsonSlurper()
    def parsedJson = slurper.parseText(property.value)
    def ids = []
    parsedJson.forms.each(){ form ->
        String id
        if(form.name.equals(name)){
            id = form.id.toString()
        }
        if(id){
            ids.add(Long.valueOf(id))
        }
    }
    return ids
}

Closure<String> getFieldId = { def property, String fieldName ->
    def slurper = new groovy.json.JsonSlurper()
    def parsedJson = slurper.parseText(property.value)
    for (def question : parsedJson.design.questions){
        if(fieldName.equals(StringUtils.substringBetween(question.toString(), "label=", ","))){
            return question.toString().substring(0, question.toString().indexOf("="))
        }
    }
}

Closure getChoiceValue = { def property, String fieldName, String choiceId ->
    def fieldId = getFieldId(property, fieldName)
    def slurper = new groovy.json.JsonSlurper()
    def parsedJson = slurper.parseText(property.value)
    for (def question : parsedJson.design.questions){
        if(question.key.equals(fieldId)){
            for(def choice : question.value.choices){
                if(choice.id == choiceId){
                    return choice.label
                }
            }
        }
    }
}

Closure<String> getFieldValue = { def property, String fieldName ->
    def slurper = new groovy.json.JsonSlurper()
    def parsedJson = slurper.parseText(property.value)
    String fieldId = getFieldId(property, fieldName)
    String text = ""
    for (def answer : parsedJson.state.answers){
        if(fieldId.equals(answer.key.toString())){
            if(answer.value.text &&  !answer.value.text.toString().equals("")){
                return answer.value.text.toString()
            } else if(answer.value.choices){
                String choiceValues = ""
                for(def choice : answer.value.choices){
                    if(choiceValues.equals("")){
                        choiceValues = getChoiceValue(property, fieldName, choice)
                    } else {
                        choiceValues = choiceValues + ", " + getChoiceValue(property, fieldName, choice)
                    }
                }
                return choiceValues.equals("") ? noValue : choiceValues
            } else if(answer.value.users){
                String userValues = ""
                for(def user : answer.value.users){
                    if(userValues.equals("")){
                        userValues = user.name
                    } else {
                        userValues = userValues + ", " + user.name
                    }
                }
                return userValues.equals("") ? noValue : userValues
            } else if(answer.value.date || answer.value.time){
                String dateTime = ""
                if(answer.value.date){
                    dateTime = dateTime + answer.value.date
                }
                if(answer.value.time){
                    if(answer.value.date){
                        dateTime = dateTime + " " + answer.value.time
                    } else {
                        dateTime = dateTime + answer.value.time
                    }
                }
                return dateTime.equals("") ? noValue : dateTime
            }
        }
    }
    if(!text.equals("")){
        return text
    } else {
        return noValue
    }
}

def allProperties = issuePropertyService.getProperties(loggedInUser, issue.id)
def forms = []
for(def property : allProperties){
    if(property.key.equals("proforma.forms")){
   	    forms = getFormIds(property, formName)
    }
}

String result = ""
for (def formId : forms){
    for(def property : allProperties){
        if(property.key.equals("proforma.forms" + ".i" + formId) && property.value.contains("\"schemaVersion\":8")){
            for(def fieldName : fieldNames) {
                if(result.equals("")){
                    result = getFieldValue(property, fieldName)
                } else {
                    result = result + ", " + getFieldValue(property, fieldName)
                }
            }
        }
    }
}

if(!result.equals("")){
    updateDescription((MutableIssue) issue, result)
}

Script (amendment for radio buttons)

There is a method in the script above called GetFieldID. If you would like to pull data from radio buttons, you will need to replace the GetFieldID method with the following:

Closure<String> getFieldId = { def property, String field ->
 def slurper = new groovy.json.JsonSlurper()
 def parsedJson = slurper.parseText(property.value)
 for (def question : parsedJson.design.questions){
 if(field.equals(question.value.label)){
 return question.key
 }
 }
}

Installation instructions

  • If used in a transition:

    • Comment line 10

    • Change formName in line 13 to the name of the form that needs the approval.

    • Change subject in line 14 to the subject of the email you want to send.

    • Change body in line 15 to the body of the email you want to send.

    • If using Jira approvers field:

      • Change approversFieldId in line 65 to the id of the Approvers field in JIRA.

    • If using a ProForma multiselect/single select user field:

      • Comment from line 65 to line 71

      • Uncomment lines 73 to 91

      • Change fieldName in line 73 to the name/label of the ProForma field with the approvers.

  • If used in the script console:

    • Change issue key in line 10 to the issue that has the form that needs approval.

    • Change formName in line 13 to the name of the form that needs the approval.

    • Change subject in line 14 to the subject of the email you want to send.

    • Change body in line 15 to the body of the email you want to send.

    • If using Jira approvers field:

      • Change approversFieldId in line 65 to the id of the Approvers field in JIRA.

    • If using a ProForma multiselect/single select user field:

      • Comment from line 65 to line 71

      • Uncomment lines 73 to 91

      • Change fieldName in line 73 to the name/label of the ProForma field with the approvers.

Possible use cases

  • Copying data from multiple ProForma fields into a single Jira fields makes the data searchable in JQL queries and Jira reports.

  • Summarize key data points collected in long, complex forms.

Limitations

  • All fields that will be included must be on the same form, and the same issue.

  • If one of the fields being collected has no value, then that field will be shown as “No value” in the Description field.

  • The script can be used to override or amend to, existing values in the Description field.

  • The script will run for each copy of the indicated form on the issue.

  • This script may not work if the form is over 32 KB. You can limit the size of your forms by using multiple smaller forms, and by including character/word limits on your text fields.

  • No labels