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
Atlassian Community Anniversary

Happy Anniversary, Atlassian Community!

This community is celebrating its one-year anniversary and Atlassian co-founder Mike Cannon-Brookes has all the feels.

Read more
Community showcase
Julia Dillon
Posted Tuesday in Jira

Tell us how your team runs on Jira!

Hey Atlassian Community! Today we are launching a bunch of customer stories about the amazing work teams, like Dropbox and Twilio, are doing with Jira. You can check out the stories here. The thi...

229 views 1 18
Join discussion

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