Imagine, you have a Jira project where your users create tickets with a “fixed” structured text in the description field. And we want to parse the fields and place it the values in real custom fields (for example, we want to do or execute this in a Workflow transition).
Example of description field with structured text/content
In this example we want to parse the “Started at” value and the “issue/Reference” number (to link the issue to the target ticket. Remember to set the ID of the type of link. Replace in TYPE_OF_LINK_ID.) We will set the date-time value in a customfield of type Date-time picker called “Start date”.
We can try to use a Script Postfunction in Jira Server with Adaptavist Scriptrunner or MyGroovy plugins.
Remember to rename the name of the field for your real Jira customfield! You can try to execute the action using concrete privileges, just rename user_with_privileges with the username who will launch the automatic action.
import com.atlassian.jira.bc.issue.search.SearchService import com.atlassian.jira.component.ComponentAccessor import com.atlassian.jira.web.bean.PagerFilter import com.atlassian.jira.issue.CustomFieldManager import com.atlassian.jira.issue.fields.CustomField import com.atlassian.jira.issue.MutableIssue; import java.text.SimpleDateFormat import java.util.Date import java.io.*; import java.util.*; log.setLevel(org.apache.log4j.Level.DEBUG) log.debug "DEBUG ENABLED" String desc = issue.description String[] aut = desc.replace(":\n","").split("\\*") def attention = ""; def status = ""; def started = ""; def reference = ""; def int i = 0 log.debug "Lenght description " + aut.length log.debug issue.getKey() while ( i < aut.length) { if (aut[i].trim()=="Attention") { attention = aut[i+1].trim()} if (aut[i].trim()=="Status") {status = aut[i+1].trim()} if (aut[i].trim()=="Started at") {started = aut[i+1].trim()} if (aut[i].trim()=="Reference Number") {reference = aut[i+1].trim()} i++ } log.debug attention + "_" + status + "_" + started + "_" + reference; def userManager = ComponentAccessor.getUserManager(); def currentUserObj = userManager.getUserByName("user_with_privileges"); def issueMgr = ComponentAccessor.getIssueManager() def linkMgr = ComponentAccessor.getIssueLinkManager() def customFieldMananger = ComponentAccessor.getCustomFieldManager() def issuedef = (MutableIssue) issue try { if ( started != "" ) { def field = customFieldMananger.getCustomFieldObjectByName("Start date") String[] start = started.split("/") String[] start_hour = start[2].split(" ") String[] start_time = start_hour[1].split(":") log.debug "Start:"+ start_hour[0] + "-" + start[1] + "-" + start[0] + " " + start_time[0] + ":" + start_time[1] def superdate = start_hour[0] + "-" + start[1] + "-" + start[0] + " " + start_time[0] + ":" + start_time[1] def fechaca = new Date().parse('yyyy-MM-dd HH:mm', superdate) //.format('yyyy-MM-dd'); log.debug "Setting Start date" issuedef.setCustomFieldValue(field, fechaca.toTimestamp()) } } catch (Exception) { } try { if ( reference != "") { def issue2 = issueMgr.getIssueObject(reference) linkMgr.createIssueLink (issue.id, issue2.id, Long.parseLong("TYPE_OF_LINK_ID"),Long.valueOf(1), currentUserObj); } } catch (Exception) { }
By MrAddon