ScriptRunner: How to update a custom field if Description changed?

Michael Voronkov November 5, 2017

Goal: identify all stories where Description field changed after certain date.

My thinking:

  • Create a custom field (number) and update counter each time Description is updated
  • Have ScriptRunner update the custom field on each Description update event
  • Use JQL and generate report (the easy part)

STEP 1: Created custom field

  "id": "customfield_16800",
    "name": "DescriptionChange",
    "custom": true,
    "orderable": true,
    "navigable": true,
    "searchable": true,
    "clauseNames": [
      "cf[16800]",
      "DescriptionChange"
    ],
    "schema": {
      "type": "number",
      "custom": "com.atlassian.jira.plugin.system.customfieldtypes:float",
      "customId": 16800
    }

STEP 2: created Custom Listener (All Projects, "All Issue Events")

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder

Issue issue = event.issue
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def change = event?.getChangeLog()?.getRelated("ChildChangeItem").find {it.field == "Description"}

if (change)
{
    def tgtField = customFieldManager.getCustomFieldObjects(issue).find {it.name == "DescriptionChange"}
    def changeHolder = new DefaultIssueChangeHolder()
    tgtField.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(tgtField), 1), changeHolder)
}
else return

 

The script doesn not update Description field.  I am new to ScriptRunner and need some help with the script.

Thanks in advance!

1 answer

1 vote
adammarkham
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
November 9, 2017

You could do this entirely with a scripted field. This is a variation of this example in the docs here.

You'll need the following script below:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.IssueFieldConstants

def changeHistoryManager = ComponentAccessor.getChangeHistoryManager()

def changeItems = changeHistoryManager.getChangeItemsForField(issue, IssueFieldConstants.DESCRIPTION)

if (changeItems) {
def changeItem = changeItems.last()

return new Date(changeItem.getCreated().time)
}

null

 You'll need to set your template and searcher to:

Template: Date Time picker
Searcher: Date Time Range picker

Then it should be just a case of crafting the right JQL to search on that field.

For your information ScriptRunner provides a number of JQL functions for comparing dates here. If you happen to have a more complex requirement in the future.

Hope this helps.

Michael Voronkov November 20, 2017

Hi Adam, thank you for the feedback.  During implementation of this functionality our requirement got refined into the following: Only track Description field changes during Active Sprint.  Should not track any changes before or after Active Sprint.

Here's the code for that:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.issue.IssueEvent
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import org.ofbiz.core.entity.GenericValue
import com.atlassian.core.util.map.EasyMap

def changeHistoryManager = ComponentAccessor.getChangeHistoryManager()
def event = (IssueEvent)event
def issue = event.issue
def customFieldManager = ComponentAccessor.getCustomFieldManager()

def sprintField = customFieldManager.getCustomFieldObjects(issue).find {it.name == "Sprint"}
if (sprintField)
{
    def state = issue.getCustomFieldValue(sprintField)?.state?.first().toString()
    if (state == "ACTIVE")
    {
        List<GenericValue> changeItems = null;
        try {
            GenericValue changeLog = event.getChangeLog()
            Map map = EasyMap.build("group", changeLog.get("id"))    
            changeItems = changeLog.getDelegator().findByAnd("ChangeItem", map)
            if(changeItems == null) {
                log.warn("nothing found")
            }
            for(GenericValue changeVal : changeItems) {
                String field = changeVal.getString("field")

                if (field == "description") {
                    log.warn("Detected change to description field")
                    def tgtField = customFieldManager.getCustomFieldObjects(issue).find {it.name == "DescriptionChange"}
                    log.warn("tgtField " + tgtField)
                    def changeHolder = new DefaultIssueChangeHolder()
                    Double num = (Double)(issue.getCustomFieldValue(tgtField))
                    if(num == null) {
                        num = 0;
                    }
                    log.warn("num " + num)
                    tgtField.updateValue(null, issue, new ModifiedValue(num, num + 1), changeHolder)
                }
            }
        } catch(Exception e) {

        }
    }
    else {return}
}
else {return}

 

Suggest an answer

Log in or Sign up to answer