Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Clone fails if the user is invalid

Aditya Sastry
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.
January 11, 2022

Hi ,

I am using the below code to clone issues which works fine until there is an user (in assignee field) who is invalid(no longer with company or without license) and haven't been removed from the user field. I am not sure if there is an option to bypass this field.

Any suggestions would be appreciated?

 

import com.atlassian.jira.component.ComponentAccessor
import org.apache.log4j.Category
import org.apache.log4j.Logger
import com.atlassian.jira.bc.issue.search.SearchService
import com.atlassian.jira.web.bean.PagerFilter
import com.onresolve.scriptrunner.runner.util.UserMessageUtil
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import org.slf4j.LoggerFactory;
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
def changeHolder = new DefaultIssueChangeHolder()

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

//def log =LoggerFactory.getLogger("com.onresolve.jira.groovy")

def issueManager = ComponentAccessor.getIssueManager()
def issueFactory = ComponentAccessor.getIssueFactory()

def loggedInUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser
def issue = issue


// **** start of cloning release logic ****
def toclone = issueFactory.cloneIssueWithAllFields(issue)
def cloned = issueManager.createIssueObject(loggedInUser,toclone)
log.info("CLONED ISSUE KEY: ${cloned.getKey()}")
// **** end of cloning release logic ****

 

1 answer

0 votes
Tim Perrault
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.
January 11, 2022

You could add logic to see if the assignee is active and if not set to the current logged in user as assignee. I haven't tried it, but I think it should work.

 

if ( issue.assignee == null) {
issue.setAssignee(loggedInUser)
}
Aditya Sastry
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.
January 11, 2022

Hi Tim,

Thanks for the suggestion but I have tried it and it didn't work. this script copies the data as is and creates a new issue and since the user is invalid, it fails. The only option as of now is to replace the user or make the assignee field to blank before trying the clone.

However I want to somehow skip the assignee field if the user is invalid. Unfortunately this doesn't work at this moment.

Thanks

Aditya

Tim Perrault
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.
January 11, 2022

Aditya,

 

What plugin, version, and type of post function are you using?

Aditya Sastry
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.
January 11, 2022

Tim,

Below are the details

Plugin and version - Scriptrunner version 5.5.0

Postfunction - Custom script post-function

Tim Perrault
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.
January 11, 2022

I am running Version 6.41.0 and I am able to get it to work. It copies everything over, even an inactive user. To test I created a ticket and then after creation I deactivated and removed all groups from the user in the assignee. I was still able to clone and it just moved the inactive user to the new ticket as the assignee, which may not be the final result you want, but it didn't block the postfunction. Not sure if the version in plugin makes a difference.  Code below. Removed like two lines and then just added a message at the end:

import com.atlassian.jira.component.ComponentAccessor
import org.apache.log4j.Category
import org.apache.log4j.Logger
import com.atlassian.jira.bc.issue.search.SearchService
import com.atlassian.jira.web.bean.PagerFilter
import com.onresolve.scriptrunner.runner.util.UserMessageUtil
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import org.slf4j.LoggerFactory;
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.onresolve.scriptrunner.runner.util.UserMessageUtil
def changeHolder = new DefaultIssueChangeHolder()

import org.apache.log4j.Category
log.setLevel(org.apache.log4j.Level.DEBUG)


def issueManager = ComponentAccessor.getIssueManager()
def issueFactory = ComponentAccessor.getIssueFactory()

def loggedInUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser


// **** start of cloning release logic ****
def toclone = issueFactory.cloneIssueWithAllFields(issue)
def cloned = issueManager.createIssueObject(loggedInUser,toclone)
log.info("CLONED ISSUE KEY: ${cloned.getKey()}")
// **** end of cloning release logic ****
UserMessageUtil.success("CLONED ISSUE KEY: ${cloned.getKey()}")

 Are you getting any errors?

 

The other thought I had was instead of using the custom script postfunction you could try the Clones an issue, and links [ScriptRunner] postfunction if it is available in version 5.5.0. This would be the easiest way to do it, otherwise I think you will need to rework the script to pull all the fields and then set from that list you retrieved.

Aditya Sastry
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.
January 12, 2022

Let me give it a try again and will update the results

Like Tim Perrault likes this

Suggest an answer

Log in or Sign up to answer