Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,366,111
Community Members
 
Community Events
168
Community Groups

Get sprint end date and set it to another field scriptrunner

Hello,

We would like to set a value in a date field, based on the sprint end date.

So if the sprint end date is 2/26/2019, that field would get this value.

Would this be possible using scriptrunner? If so, would anyone know how to do that?

Thank you!

3 answers

2 accepted

0 votes
Answer accepted

Hi @Nir Haimov 

When looking for it in custom fields, it appears as: Date Picker . 

Example of values from an issue that has it filled in: 1/Apr/19 . 

 

Thank you!

Nir Haimov Community Leader Mar 02, 2019

Hi @Mihai Mihai 

Here is the complete code:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.issue.ModifiedValue
import java.sql.Timestamp;

def customFieldManager = ComponentAccessor.getCustomFieldManager()
def sprint = customFieldManager.getCustomFieldObjectByName("Sprint")
def changeHolder = new DefaultIssueChangeHolder();
def myCustomField = customFieldManager.getCustomFieldObject("customfield_12000")

//End Date
Date endDate = event.issue.getCustomFieldValue(sprint)?.endDate?.first()?.toDate()


if (endDate != null) {
Calendar cal = Calendar.getInstance();
cal.setTime(endDate);
cal.set(Calendar.MILLISECOND, 0);
log.error(new Timestamp(endDate.getTime()));
log.error(new Timestamp(cal.getTimeInMillis()));

myCustomField.updateValue(null, event.issue, new ModifiedValue(event.issue.getCustomFieldValue(myCustomField), new Timestamp(cal.getTimeInMillis())),changeHolder);
} else {
myCustomField.updateValue(null, event.issue, new ModifiedValue(event.issue.getCustomFieldValue(myCustomField), null),changeHolder);
}

Just change "customfield_12000" to your custom date field.
And it will work perfectly.

Put it in Listener for any "issue update" event

Thank you very much @Nir Haimov  , it works great!

@Nir Haimov 

I am trying to copy Sprint Enddate to issue customField of type DateTime picker field, I am using below two ways it's not working, could you please correct me 

Approach 1:

def latestSprintEndDate = customFieldManager.getCustomFieldObject("Latest Sprint End Date")

Timestamp tsFormat = (Timestamp) issue.getCustomFieldValue(sprint)?.endDate?.first()?.toDate()
Date csNewDateValue = new Date(tsFormat.getTime() + 15*24*60*60*1000);

latestSprintEndDate.updateValue(null, epic, new ModifiedValue(epic.getCustomFieldValue(latestSprintEndDate), (Object)csNewDateValue),changeHolder)

 

Approach 2: your approach

Date endDate = issue.getCustomFieldValue(sprint)?.endDate?.last()?.toDate()

log.error "Sprint endDate ${endDate} Sprint Name : ${sprintName.toString()}"

if (endDate != null) {
Calendar cal = Calendar.getInstance();
cal.setTime(endDate);
cal.set(Calendar.MILLISECOND, 0);
log.error(new Timestamp(endDate.getTime()));
log.error(new Timestamp(cal.getTimeInMillis()));

latestSprintEndDate.updateValue(null, epic, new ModifiedValue(epic.getCustomFieldValue(latestSprintEndDate), new Timestamp(cal.getTimeInMillis())),changeHolder);
} else {
latestSprintEndDate.updateValue(null, epic, new ModifiedValue(epic.getCustomFieldValue(latestSprintEndDate), null),changeHolder);
}

2020-06-23 08:31:26,741 ERROR [runner.ScriptBindingsManager]: Sprint endDate Tue Jun 23 10:46:00 PDT 2020   Sprint Name : TPK Sprint 1
2020-06-23 08:31:26,745 ERROR [runner.ScriptBindingsManager]: 2020-06-23 10:46:00.0
2020-06-23 08:31:26,745 ERROR [runner.ScriptBindingsManager]: 2020-06-23 10:46:00.0
2020-06-23 08:31:26,747 ERROR [runner.AbstractScriptListener]: *************************************************************************************
2020-06-23 08:31:26,747 ERROR [runner.AbstractScriptListener]: Script function failed on event: com.atlassian.jira.event.issue.IssueEvent, file: null
java.lang.NullPointerException
 at com.atlassian.jira.issue.IssueImpl.getCustomFieldValue(IssueImpl.java:896)
 at com.atlassian.jira.issue.Issue$getCustomFieldValue$2.call(Unknown Source)
 at Script1884.run(Script1884.groovy:92)

I'm trying to do the same using the provided script, but I'm getting an error:

Schermafbeelding 2020-10-01 om 11.23.12.png

Not sure what to change in order to make the script work.

@Berry KerstenWere you ever able to get this working?

0 votes
Answer accepted
Nir Haimov Community Leader Feb 28, 2019

Hi @Mihai Mihai 

You want to update a customfield of type "date" with the value of "sprint end date"?

Hello @Nir Haimov 

Yes. For all the issues (that have a sprint) inside one certain project .

Nir Haimov Community Leader Feb 28, 2019

Hi @Mihai Mihai 

Instead of creating customfield and than a script to update that customfield.

Simply create scripted field with script runner, in the scripted field template, choose "Date Time".

Copy paste this script:

import com.atlassian.jira.component.ComponentAccessor

def customFieldManager = ComponentAccessor.getCustomFieldManager()
def sprint = customFieldManager.getCustomFieldObjectByName("Sprint")

//End Date
Date endDate = issue.getCustomFieldValue(sprint)?.endDate?.first()?.toDate()

ignore the errors if ther are any, it should work any way.

Put the scripted field in your "view issue" screen.

If your issue have sprint, the scripted field will show you the "end date" as you wanted.

Hi @Nir Haimov 

 

Thank you for the suggestion. I should have mentioned earlier that we already have a date field created, and we want that existing one to get values based on the sprint date.

 

I know this complicates things.

 

Thank you!

Hi @Mihai Mihai 

OK, you will have to tell me if this customfield is of type "date" or "date time" this is a big difference.

Please check to be sure and et me know, i will help you

Hi @Nir Haimov ,

I am able to get sprint end date using below code you shared in this page. Similar to Sprint start, i am looking for code sprint start date. i tried to put "startDate" in "endDate" place. But it showing as sprint started 4 days ago instead of date 8-july-2022.

Can you help with  code for sprint start similar to sprint end date as below. what needs to be modified for getting sprint start date?

Sprint End Date:

import com.atlassian.jira.component.ComponentAccessor

def customFieldManager = ComponentAccessor.getCustomFieldManager()
def sprint = customFieldManager.getCustomFieldObjectByName("Sprint")

//End Date
Date endDate = issue.getCustomFieldValue(sprint)?.endDate?.first()?.toDate()

I know there have been several responses and answers on this. I tested the March 02, 2019 response and used it as a base, though there were a few errors on some functions. I wanted to set a custom date field called Start Date using the Sprint Start Date and also set a custom date field called End Date using the Sprint End Date. Got it working using the listener script below:

 

import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.event.issue.IssueEvent
import com.atlassian.greenhopper.service.sprint.Sprint
import java.sql.Timestamp;

// Define functions needed
def customFieldManager = ComponentAccessor.getCustomFieldManager() def changeHolder = new DefaultIssueChangeHolder(); def issue = event.issue // Grab and define the Start Date Custom Field
def StartDateField = customFieldManager.getCustomFieldObject("customfield_00001") // Grab and define the End Date Custom Field
def EndDateField = customFieldManager.getCustomFieldObject("customfield_00002"// Check to see if the Sprint field was changed
def changeItems = ComponentAccessor.changeHistoryManager.getChangeHistoryById(event.changeLog.id as Long).changeItemBeans if (!changeItems.any { it.field == 'Sprint' }) {     //Skip this listener if the sprint field wasn't changed   
return } // Get the current list of sprints on the issue
def sprintCf = ComponentAccessor.customFieldManager.getCustomFieldObjectsByName("Sprint")[0] def sprints = issue.getCustomFieldValue(sprintCf) as List<Sprint> if (!sprints) {     //if sprint field is empty, then we need to exit   
return } // Determine the last sprint
def sprint = sprints.last() // Grab the Start Date from the Last Sprint.
def startDate = sprint.startDate.toDate() // Grab the End Date from the Last Sprint.
def endDate = sprint.endDate.toDate() // Log errors where needed against Start Date
if (startDate != null) { Calendar cal = Calendar.getInstance(); cal.setTime(startDate); cal.set(Calendar.MILLISECOND, 0); log.error(new Timestamp(startDate.getTime())); log.error(new Timestamp(cal.getTimeInMillis())); // Set the Start Date Field Value
StartDateField.updateValue(null, event.issue, new ModifiedValue(event.issue.getCustomFieldValue(StartDateField), new Timestamp(cal.getTimeInMillis())),changeHolder); } else { StartDateField.updateValue(null, event.issue, new ModifiedValue(event.issue.getCustomFieldValue(StartDateField), null),changeHolder); } // Log errors where needed against End Date
if (endDate != null) { Calendar cal = Calendar.getInstance(); cal.setTime(endDate); cal.set(Calendar.MILLISECOND, 0); log.error(new Timestamp(endDate.getTime())); log.error(new Timestamp(cal.getTimeInMillis())); // Set the End Date Field Value
EndDateField.updateValue(null, event.issue, new ModifiedValue(event.issue.getCustomFieldValue(EndDateField), new Timestamp(cal.getTimeInMillis())),changeHolder); } else { EndDateField.updateValue(null, event.issue, new ModifiedValue(event.issue.getCustomFieldValue(EndDateField), null),changeHolder); }

Suggest an answer

Log in or Sign up to answer
TAGS

Atlassian Community Events