Delete an Issue among several Edited

Hi everybody,

I am trying to code a post function during the creation of an issue. The goal of my script is to create one or many other issue according to the completed fields of the "originaln issue". At the end, the script should delete the original issue.

Here is my code :

 

import com.atlassian.jira.ComponentManager
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.security.groups.GroupManager
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.customfields.manager.OptionsManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.project.ProjectManager
import com.atlassian.jira.issue.IssueImpl
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder

def componentManager = ComponentManager.getInstance()
IssueManager issueManager = ComponentAccessor.getIssueManager()
ProjectManager projectMgr =ComponentAccessor.getProjectManager()
issueFactory = ComponentAccessor.getIssueFactory()
ApplicationUser currentUserObj = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
MutableIssue issue = issue
//def issue = ComponentAccessor.getIssueManager().getIssueObject("PSD-1904")
CustomFieldManager cfManager = ComponentAccessor.getCustomFieldManager()

CustomField cf = cfManager.getCustomFieldObject((long)26904) // Details of the Request PP
CustomField backJava = cfManager.getCustomFieldObject((long)27005) // Back Java PP
CustomField backPhp = cfManager.getCustomFieldObject((long)27004) // Back PHP PP
CustomField front = cfManager.getCustomFieldObject((long)27003) // Front End PP
CustomField epicLink = cfManager.getCustomFieldObject((long)19901) // EpicLink PP
CustomField product = cfManager.getCustomFieldObject((long)26900) // EpicLink PP


def cfValueFront = issue.getCustomFieldValue(front)
def cfValueBackPhp = issue.getCustomFieldValue(backPhp)
def cfValueBackJava = issue.getCustomFieldValue(backJava)
def cfValue = issue.getCustomFieldValue(cf)


def createMainIssue2(def productCF, def epicLinkCF, def issueFactory, String var,def issue, ApplicationUser myUser,IssueManager issueManager){

newissue = issueFactory.getIssue()
newissue.setSummary (var + issue.summary)
newissue.setProject (issue.project)
newissue.setIssueTypeId("15")
newissue.description = issue.description
newissue.reporter = issue.getReporter()
newissue.assignee = issue.getAssignee()
newissue.priority = issue.getPriority()
params = ["issue":newissue]
issueManager.createIssueObject(myUser,params)
def changeHolder = new DefaultIssueChangeHolder();
def epicLinkValue = issue.getCustomFieldValue(epicLinkCF)
epicLinkCF.updateValue(null,newissue,new ModifiedValue(newissue.getCustomFieldValue(epicLinkCF),epicLinkValue),changeHolder)
def productValue = issue.getCustomFieldValue(productCF)
productCF.updateValue(null,newissue,new ModifiedValue(newissue.getCustomFieldValue(productCF),productValue),changeHolder)
return true
}


if(cfValue != null){
for(int i = 0;i < cfValue.size();i++){
if( (String) cfValue.get(i) == 'Catalog - New Product'){
createMainIssue2(product, epicLink, issueFactory,"Catalog - New Product (Product Label Creation) : ",issue,currentUserObj,issueManager)
createMainIssue2(product, epicLink, issueFactory,"Catalog - New Product (Product Creation) : ",issue,currentUserObj,issueManager)
createMainIssue2(product, epicLink, issueFactory,"Catalog - New Product (Pricing Review) : ",issue,currentUserObj,issueManager)
}

else if( (String) cfValue.get(i) == 'Adaptive Pricing / MVPT'){
createMainIssue2(product, epicLink, issueFactory,"Adaptive Pricing / MVPT (Rules Creation) : ",issue,currentUserObj,issueManager)
}

else if( (String) cfValue.get(i) == 'AB Test'){
createMainIssue2(product, epicLink, issueFactory,"AB Test (ABTest Creation) : ",issue,currentUserObj,issueManager)
createMainIssue2(product, epicLink, issueFactory,"AB Test (BVO - ABTest Creation) : ",issue,currentUserObj,issueManager)
createMainIssue2(product, epicLink, issueFactory,"AB Test (ABTest Closure) : ",issue,currentUserObj,issueManager)

}

else if( (String) cfValue.get(i) == 'New RateCard'){
createMainIssue2(product, epicLink, issueFactory,"New Ratecard (FrontEnd Integration) : ",issue,currentUserObj,issueManager)
createMainIssue2(product, epicLink, issueFactory,"New Ratecard (FrontEnd Configuration) : ",issue,currentUserObj,issueManager)
}

else if( (String) cfValue.get(i) == 'New Activation Service (Privileges and Rights)'){
createMainIssue2(product, epicLink, issueFactory,"New Activation Service (Privileges and Rights) : ",issue,currentUserObj,issueManager)
}

else if( (String) cfValue.get(i) == 'New Web Service'){
createMainIssue2(product, epicLink, issueFactory,"New Web Service : ",issue,currentUserObj,issueManager)
}
}
}

if(cfValueBackJava != null){
String myVar = new String()
myVar = "Back Java :"
for(int i =0; i< cfValueBackJava.size();i++){
myVar = myVar + " "+cfValueBackJava.get(i)+"/"
}
myVar = myVar + " : "
createMainIssue2(product, epicLink, issueFactory,myVar,issue,currentUserObj,issueManager)
}

if(cfValueBackPhp != null){
String myVar2 = new String()
myVar2 = "Back PHP :"
for(int i =0; i< cfValueBackPhp.size();i++){
myVar2 = myVar2 + " "+cfValueBackPhp.get(i)+"/"
}
myVar2 = myVar2 + " : "
createMainIssue2(product, epicLink, issueFactory,myVar2,issue,currentUserObj,issueManager)
}

if(cfValueFront != null){
createMainIssue2(product, epicLink, issueFactory,"Front New Web Service : ",issue,currentUserObj,issueManager)
}

issueManager.deleteIssue(currentUserObj, issue,
EventDispatchOption.ISSUE_DELETED, false);

 

The creation of the "cloned issues" works well.

The main point that I don't understand is that my function is called several times ((number of issues created + 1) calls).

The second point which is link to the first one, is that all my "clones" are delete and not my original issue.

Thanks for your answers :)

1 answer

1 accepted

There's three problems here.

1.  It sounds like your post-function is associated with the create on all issues, so the code is executing many times


2. Deleting issues in JIRA is generally a bad idea, as it completely destroys them

3. Deleting an issue within a post-function is potentially suicidal in terms of data integrity and functionality.  Especially when creating an issue.  Think of the creation as like the lifecycle of a cake:

  1. someone asks for cake (clicks create),
  2. you get all the ingredients togther, mix them together, turn the oven on, put the cake in, wait, take it out, let it cool and decorate it. (post functions)
  3. then eat it (delete)
    You can see the problem with what you're doing is that you're eating the cake before it goes in the oven, so the rest of the process falls over in a heap, because you've already destroyed what you were working with.

If you insist on doing this poorly designed process in this way, then do two things:

1.  Do the deletion in a Listener.  The "create other issues" is fine in a post-function, but the delete should be a separate step, done after the issue is fully created

2.  Think through where things are being run - make sure the listener only reacts to the "create" issue for the one you want to kill.

 

Thank you very much for your answer Nic. I have find a better way to do what I wanted :)

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 Apr 17, 2018 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...

809 views 2 19
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