import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.model.ChangeGroup
import com.atlassian.jira.model.ChangeItem
import com.atlassian.jira.issue.history.ChangeItemBean
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.context.IssueContext
import com.atlassian.jira.issue.context.IssueContextImpl
import com.atlassian.jira.issue.fields.config.manager.PrioritySchemeManager
import com.atlassian.jira.issue.IssueManager;
import com.atlassian.jira.issue.MutableIssue;
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def cField = customFieldManager.getCustomFieldObject("customfield_10113")
def cFieldValue = issue.getCustomFieldValue(cField)
IssueManager im = ComponentAccessor.getIssueManager();
MutableIssue currentIssue = im.getIssueObject(issue.key) as MutableIssue;
def java.lang.String originalSummary = currentIssue.getSummary();
def java.lang.String originalDesc = currentIssue.getDescription()
//Epic Link
def epiclinkKey = currentIssue.key
//this gets the newest value of the custom field 'FieldName'
def newValue = cFieldValue.toString()
def changeHistoryManager = ComponentAccessor.getChangeHistoryManager()
def changeItems = changeHistoryManager.getChangeItemsForField(issue,'Impacted Projects')
//if field has no change history, this returns false
if (changeItems){
//this gets the previous value of the field
def lastValue = changeItems.last().getFromString()
if((lastValue.contains("Project B")))
{
//return lastValue
// the project key under which the issue will get created
final projectKey = 'PB'
// the issue type for the new issue
final issueTypeName = 'Story'
// user with that user key will be the reporter of the issue
final reporterKey = 'jira'
final priorityName = 'Major'
def issueService = ComponentAccessor.issueService
def constantsManager = ComponentAccessor.constantsManager
def loggedInUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser
def prioritySchemeManager = ComponentAccessor.getComponent(PrioritySchemeManager)
def project = ComponentAccessor.projectManager.getProjectObjByKey(projectKey)
assert project : "Could not find project with key $projectKey"
def issueType = constantsManager.allIssueTypeObjects.findByName(issueTypeName)
assert issueType : "Could not find issue type with name $issueTypeName"
// if we cannot find user with the specified key or this is null, then set as a reporter the logged in user
def reporter = ComponentAccessor.userManager.getUserByKey(reporterKey) ?: loggedInUser
// if we cannot find the priority with the given name or if this is null, then set the default priority
def issueContext = new IssueContextImpl(project, issueType) as IssueContext
def priorityId = constantsManager.priorities.findByName(priorityName)?.id ?: prioritySchemeManager.getDefaultOption(issueContext)
def issueInputParameters = issueService.newIssueInputParameters().with {
setIssueTypeId(issueType.id)
setSummary(originalSummary)
setDescription(originalDesc)
setPriorityId(priorityId)
}
def validationResult = issueService.validateCreate(loggedInUser, issueInputParameters)
assert validationResult.valid : validationResult.errorCollection
def result = issueService.create(loggedInUser, validationResult)
assert result.valid : result.errorCollection
//def issue = event.issue
//def epicLink = customFieldManager.getCustomFieldObjectByName("Epic Link")
//epicLink.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(epicLink), epiclinkKey),new DefaultIssueChangeHolder())
}
}
else{
return false
}