Set Issue Due Date based on fixVersion release Date with Script Listener

Marcus Reiche November 27, 2017

Hello,

I am new to jira and been trying to get this script to run.
Iam working on a Server Installation on Version 7.5.2.

I am trying to achieve the following:

When a issue is created or updated, the dueDate of the issue will be set to the associated fixVersion releaseDate.

It tried a bit, but didn't get to far:

import com.atlassian.jira.component.ComponentAccessor
import org.apache.log4j.Category

def Category log = Category.getInstance("com.onresolve.jira.groovy")
log.setLevel(org.apache.log4j.Level.DEBUG)

def issue = event.issue
def project = issue.getProjectObject()
project.getVersions().each {
log.debug it
}


log.debug issue.getFixVersions()

 

I am stuck at now loading the Versions based on the name, that I got. To then retrieve the release date, if equal to the Issue.fixVersion.

I know the script needs to check if exactly one fixVersion is defined and only then it should run. But Iam far away from this step yet.

I hope somebody can help me with that. It would speedup our workenvironment immensely.

 

SOLVED

 

Thanks to @Tarun Sapra here is the complete solution:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.project.version.Version
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.IssueManager

IssueManager issueManager = ComponentAccessor.getComponent(IssueManager.class)

def issue = (MutableIssue)event.issue
def version = (Version)issue.getFixVersions().first()
if(version) {
def releaseDate = version.getReleaseDate()
if(releaseDate) {
issue.setDueDate(new java.sql.Timestamp(releaseDate.getTime()))
issueManager.updateIssue(event.getUser(), issue , EventDispatchOption.ISSUE_UPDATED, false)
}
}

 

1 answer

1 accepted

1 vote
Answer accepted
Tarun Sapra
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
November 27, 2017

Once you have the issue object you can try something like this

def version = (Version)issue.getFixVersions().first()
if(version) {
def releaseDate = version.getReleaseDate()
if(releaseDate) {
issue.setDueDate(new java.sql.Timestamp(releaseDate.getTime()))
}
}

 API reference - https://developer.atlassian.com/static/javadoc/jira/latest/reference/com/atlassian/jira/issue/MutableIssue.html

Tarun Sapra
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
November 27, 2017

Here I take into account that the issue has only one fixVersion associated with it or the first one is taken into consideration if there are many values for fixVersion on an issue.

Also TimeStamp is of type java.sql and Date(releaseDate) is of type java.Util .

Marcus Reiche November 28, 2017

Wow Thank you for the fast anwser.

I implemented your code:

import com.atlassian.jira.project.version.Version
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.MutableIssue

def issue = (MutableIssue)event.issue
def version = (Version)issue.getFixVersions().first()
if(version) {
def releaseDate = version.getReleaseDate()
if(releaseDate) {
issue.setDueDate(new java.sql.Timestamp(releaseDate.getTime()))
}
}

 

The Code is running and it doesnt but out any errors, but it although doesn't set the DueDate on the Issue.

Did I miss something?

Tarun Sapra
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
November 28, 2017

Hello Marcus,

This code should work fine in the "post-function" of the create transition.

Where are you using the code?

Marcus Reiche November 28, 2017

I wanted to use it in create and update.

Tarun Sapra
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
November 28, 2017

But where do you want to use it? In the *post-function* of create transition or in the *Listener* ??

Marcus Reiche November 28, 2017

Oh sorry, as a Listener, as writen in the title. 

Tarun Sapra
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
November 28, 2017

To update the issue in "Listener" you have to use the "issueManager"

please see code snippet here

https://community.atlassian.com/t5/Jira-questions/Custom-update-listener-to-set-subtask-s-fix-version/qaq-p/591808

 

basically add in the end and that should do the trick

issueManager.updateIssue(event.getUser(), issue , EventDispatchOption.ISSUE_UPDATED, false)
Marcus Reiche November 28, 2017

Thanks! It works!

Tarun Sapra November 28, 2017

Good to know @Marcus Reiche that it works! Please accept/upvote the answer. thanks

siva September 1, 2021

Hi @Tarun Sapra @Tarun Sapra 

Sir, I saw your script based on fixversion date due date is updated.

Similar related question i had raised in community but no responce can you please look into my question

https://community.atlassian.com/t5/Jira-Software-questions/Script-Runner-Post-function-Creates-sub-task-automatically/qaq-p/1793744#M158285

Based on fixversion value subtask will be created please suggest me how to achieve this script

Thanks a lot

Suggest an answer

Log in or Sign up to answer