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 :)
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:
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 :)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.