I am using an automation rule and executing the following code in scriptrunner to create a new issue:
Here is the code block causing the problem:
Issue newissue = Issues.create(projectName, 'Feature') {
setSummary(issue.getSummary())
setDescription(issue.getDescription())
setReporter(issue.getReporter())
setCustomFieldValue(customField.getFieldName(), value)
}
newissue.setFixVersions(mynewVersions)
log.warn("MOUNA CAMELIA 14"+ newissue.getFixVersions())
newissue.setComponent(mynewComponentList)
log.warn("MOUNA CAMELIA 15"+ newissue.getComponents())
am printing the value of newissue.getFixVersions() and I am getting: MOUNA CAMELIA 14 [2021Apr-Inbox]
I am printing the value of newissue.getComponents() and I am getting: MOUNA CAMELIA 15[ProjectComponentImpl { name='NER', description='Whole Department Database', lead='jfd', assigneeType='1', projectId='52315', id='19852', archived='false', deleted='false' }]
The values are not null. However when I execute the code through my automation rule, I am getting empty values for the components and the versions as you can see below:
The log where I am printing my values for debugging:
The full code:
package SuperFeature
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.Issue
import org.apache.log4j.Logger
import com.atlassian.jira.util.ImportUtils
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.index.IssueIndexManager
import com.atlassian.jira.issue.index.IssueIndexingService
import com.atlassian.jira.event.issue.IssueEventManager
import com.atlassian.jira.event.issue.IssueEvent
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.issue.link.IssueLinkManager
import com.atlassian.jira.issue.link.IssueLinkTypeManager
import com.atlassian.jira.event.issue.link.IssueLinkCreatedEvent
import com.atlassian.jira.event.issue.link.IssueLinkDeletedEvent
import org.apache.log4j.Level
import com.atlassian.jira.issue.link.IssueLinkType
import com.atlassian.jira.project.version.Version
import com.atlassian.jira.bc.project.component.ProjectComponent
import com.atlassian.jira.bc.project.component.ProjectComponentManager
import com.onresolve.jira.groovy.user.FieldBehaviours
import com.onresolve.scriptrunner.db.DatabaseUtil
import groovy.transform.BaseScript
import com.atlassian.jira.issue.label.LabelManager
import com.atlassian.jira.issue.label.Label
import com.atlassian.jira.security.JiraAuthenticationContext
import com.atlassian.jira.issue.IssueFactory
import com.atlassian.jira.user.util.UserUtil
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.issue.comments.CommentManager;
import com.atlassian.jira.util.velocity.CommonVelocityKeys
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.project.ProjectFactory
import java.util.Collection
import java.lang.*
import java.util.*
import groovy.lang.*
import groovy.util.*
import com.atlassian.jira.project.version.VersionManager
def log = Logger.getLogger('atlassian-jira.log')
List < String > componentList = new ArrayList < String > ()
def authenticationContext = ComponentAccessor.jiraAuthenticationContext
if (issue.getComponents().size() == 0) {
issue.update {
String text = "Issue does not have any components\n"
setCustomFieldValue('Execution Summary', text)
}
} else if (issue.getFixVersions().size() == 0) {
issue.update {
String text = "Issue does not have any fix versions\n"
setCustomFieldValue('Execution Summary', text)
}
} else {
int componentSize = issue.getComponents().size()
componentList = issue.getComponents().collect {
it.getName()
}
issue.update {
String text = "The super feature " + issue + " will be split into " + componentSize + " features, one for each component:\n"
text = text + componentList.toString() + "\n";
setCustomFieldValue('Execution Summary', text)
}
createFeature(issue, issue.getComponents())
}
void createFeature(Issue issue, Collection < ProjectComponent > componentList) {
IssueLinkManager issueLinkManager = ComponentAccessor.getIssueLinkManager();
JiraAuthenticationContext authenticationContext = ComponentAccessor.getJiraAuthenticationContext();
IssueFactory issueFactory = ComponentAccessor.getIssueFactory();
ApplicationUser currentUserObj = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser();
ProjectFactory projectFactory = ComponentAccessor.getProjectFactory()
VersionManager versionManager = ComponentAccessor.getVersionManager()
long issueLinkType = 10070 as long
long sequence = 1 as long
long myissueID = issue.getId() as long
// the key of the project under which the version will get created
final String projectKey = "SF"
// the start date - optional
final Date startDate = null
// the release date - optional
final Date releaseDate = null
// a description for the new version - optional
final String description = null
// id of the version to schedule after the given version object - optional
final Long scheduleAfterVersion = null
// true if this is a released version
final boolean released = false
def project = ComponentAccessor.projectManager.getProjectObjByKey(projectKey)
assert project: "Could not find project with key $projectKey"
ProjectComponentManager projectComponentManager = ComponentAccessor.getProjectComponentManager()
int counter = 0
for (ProjectComponent component: componentList) {
String componentName = component.getName()
def shortenedComponentName = componentName.substring(componentName.indexOf("-") + 1)
def projectName = componentName.substring(0, componentName.indexOf("-"))
def newIssueproject = ComponentAccessor.projectManager.getProjectObjByKey(projectName)
List < String > newIssueProjectVersionIDs = new ArrayList < String > ()
newIssueProjectVersionIDs = newIssueproject.getVersions().collect {
it.getName()
}
def customField = ComponentAccessor.getCustomFieldManager().getCustomFieldObject("customfield_10790"); // here replace the ID with ID of your custom field.
def value = issue.getCustomFieldValue(customField);
def matchedVersions = issue.getFixVersions().intersect(newIssueproject.getVersions(), Version.NAME_COMPARATOR)
ProjectComponent newComponent = null
List < ProjectComponent > newList = new ArrayList < > (newIssueproject.getComponents());
def found = newList.any {
it.getName().equals(shortenedComponentName)
}
//log.warn("MOUNA CAMELIA found--"+found+"--NEW LIST SIZE: "+newList)
if (found) {
newComponent = projectComponentManager.findByComponentName(newIssueproject.getId(), shortenedComponentName)
} else {
try {
newComponent = projectComponentManager.create(shortenedComponentName, component.getDescription(), component.getLead(), component.getAssigneeType(), newIssueproject.getId())
} catch (Exception e) {
log.warn("MOUNA CAMELIA EXCEPTION " + e)
}
}
List < ProjectComponent > mynewComponentList = new ArrayList < ProjectComponent > ()
mynewComponentList.add(newComponent)
def versionCreator = ComponentAccessor.versionManager.&createVersion.rcurry(startDate, releaseDate, description, newIssueproject.id, scheduleAfterVersion, released)
def mynewVersions = issue.fixVersions.intersect(newIssueproject.versions, Version.NAME_COMPARATOR).collect {
versionCreator it.name + '-Inbox'
}
Issue newissue = Issues.create(projectName, 'Feature') {
setSummary(issue.getSummary())
setDescription(issue.getDescription())
setReporter(issue.getReporter())
setCustomFieldValue(customField.getFieldName(), value)
}
log.warn("MOUNA CAMELIA 13")
newissue.setFixVersions(mynewVersions)
log.warn("MOUNA CAMELIA 14"+ newissue.getFixVersions())
newissue.setComponent(mynewComponentList)
log.warn("MOUNA CAMELIA 15"+ newissue.getComponents())
log.warn("MOUNA CAMELIA 16 " + newissue.getComponentObjects())
long newIssueCreatedID = newissue.getId() as long
def labelManager = ComponentAccessor.getComponent(LabelManager)
labelManager.addLabel(authenticationContext.getLoggedInUser(), newIssueCreatedID, "SF-Child", false)
issueLinkManager.createIssueLink(newIssueCreatedID, myissueID, issueLinkType, sequence, authenticationContext.getLoggedInUser())
Collection < ProjectComponent > componentList2 = issue.getComponents()
componentList2.removeAll(issue.getComponents())
projectComponentManager.updateIssueProjectComponents(issue, componentList2)
counter++
}
}
What can be done to fix this issue?
Issue newissue = Issues.create(projectName, 'Feature') {
setSummary(issue.getSummary())
setDescription(issue.getDescription())
setReporter(issue.getReporter())
setCustomFieldValue(customField.getFieldName(), value)
setFixVersions(mynewVersions as String[])
setComponents (componentArray as String[])
}