Scriptrunner Scripted field - How to verify if 'description' is edited during an active sprint?

Chandana February 5, 2021

Hi,

During an active sprint, if an issue description is edited then I would like to increment a counter, to know how many times description has been updated.

For this, I wrote the below piece of code to verify, if the issue is in active sprint or not. If the particular issue is in active sprint then the script should be looking for any updates to the 'description field' and update the counter. 

Any ideas on how to validate if the description field is edited (updated), during an active sprint?

 

import com.atlassian.jira.component.ComponentAccessor

import com.atlassian.greenhopper.service.sprint.Sprint

def description = issue.getDescription();

def sprintstatus =

    (issue.getCustomFieldValue(

ComponentAccessor.customFieldManager.getCustomFieldObjectByName("Sprint")

) as List).any {

(it as Sprint).active

}

if(sprintstatus == true) //if sprint is active

{

   //check if the 'description' field of the issue is edited and if edited then increment the counter for every edit

}

2 answers

1 accepted

0 votes
Answer accepted
Tuncay Senturk
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
February 7, 2021

Hi @Chandana 

I can't provide all code, but I can give a clue on this one.

You cahev to use ChangeHistoryManager

import com.atlassian.jira.issue.changehistory.ChangeHistoryManager

and of course get it

def changeHistoryManager = ComponentAccessor.getChangeHistoryManager()

then you can get every change item history against the description field

changeHistoryManager.getChangeItemsForField(issue, IssueFieldConstants.DESCRIPTION)
List<ChangeItemBean>

but you need to import in order to use it

import com.atlassian.jira.issue.IssueFieldConstants

This method returns the list of ChangeItemBean

List<ChangeItemBean>

ChangeItemBean has every information you need, date/time, old value, new value.

If you check the sprint start date then you will have the exact number which the counter would have.

I hope it was helpful.

Thanks

Tuncay

Chandana February 8, 2021

Hi @Tuncay Senturk - Thank you for your response!

 I figured out the way to do this. Thank you for your suggestion on <ChangeItemBean>

Tuncay Senturk
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
February 9, 2021

Perfect! I'm glad to hear that it helped.

0 votes
mogavenasan
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.
February 7, 2021

Hi @Chandana,

Not sure how you are using the script in your Jira. If you have Scriptrunner, then you can use Script listener to listen to the Issue Updated event. Next, within the script, you can defined what are the checks you want to do; for example, for creation issue types and etc. The script would something like this:

import org.apache.log4j.Logger
import org.apache.log4j.Level
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.greenhopper.service.sprint.Sprint

def log = Logger.getLogger("com.acme.CreateSubtask")
log.setLevel(Level.DEBUG)

def change = event?.getChangeLog()?.getRelated("ChildChangeItem")?.find {it.field == "description"}


log.debug(change)


if (change) {
    //log.debug("Old value : ${change.oldstring}")
    //log.debug("New value : ${change.newstring}")
    log.debug("Value changed by ${event.user} at ${event?.getChangeLog()?.created}")

def sprintstatus = (issue.getCustomFieldValue(ComponentAccessor.customFieldManager.getCustomFieldObjectByName("Sprint")) as List).any {
        (it as Sprint).active
    }

    if(sprintstatus == true) {//if sprint is active
        log.debug("Sprint is active")
// you can add some codes here to increase the counter, if you have a custom field with the counter value,
// then you will need to read the field value and increment by one and then store the value back into the custom field
    }
}

I have tested it on the bare minimum level. I hope that it helps.

Thanks,
Moga

Chandana February 8, 2021

Hi @mogavenasan  - Thank you for your response!

Like mogavenasan likes this

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events