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

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
Tarun Sapra Community Champion Nov 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 Champion Nov 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 .

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 Champion Nov 28, 2017

Hello Marcus,

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

Where are you using the code?

I wanted to use it in create and update.

Tarun Sapra Community Champion Nov 28, 2017

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

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

Tarun Sapra Community Champion Nov 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)

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

Suggest an answer

Log in or Sign up to answer
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Published Thursday in Jira

5 ways you can make the most of Jira Software and Bitbucket Cloud

As part of the Bitbucket product team I'm always interested in better understanding what kind of impact the use of our tools have on the way you work. In a recent study we conducted of software devel...

80 views 0 5
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you