I'm trying to make a listener (Issue Created) which checks metadata en an attached Excel file, and if the metadata value is not what I need it to be, the issue should transition to Canceled. If I set the listener to listen on Issue Updated, it works like a charm, but not Issue Created.
I made a workflow transaction so that all statuses can transition to Canceled.
import org.apache.poi.xssf.usermodel.XSSFWorkbook
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue
import org.apache.poi.POIXMLProperties
import org.apache.poi.POIXMLProperties.CustomProperties
import com.atlassian.jira.project.Project
import com.atlassian.jira.project.ProjectManager
import com.atlassian.jira.bc.issue.IssueService
import com.atlassian.jira.issue.comments.CommentManager
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.workflow.WorkflowTransitionUtil;
import com.atlassian.jira.workflow.WorkflowTransitionUtilImpl;
import com.atlassian.jira.util.JiraUtils
import com.atlassian.jira.security.JiraAuthenticationContext
import com.atlassian.jira.user.ApplicationUser
def ApplicationUser user = ComponentAccessor.userManager.getUserByKey("sysadmin")
def JiraAuthenticationContext authContext = ComponentAccessor.getJiraAuthenticationContext()
authContext.setLoggedInUser(user)
MutableIssue issue = (MutableIssue) event.issue
def attachmentPath = "/var/atlassian-data/jira/data/attachments/"
def projectKey = "TEST"
def propertyName = "Template Version"
def propertyValue = "2"
def workflowActionId = 961 // Action ID for status canceled
def ProjectManager projectManager = ComponentAccessor.projectManager
def Project p = projectManager.getProjectByCurrentKey(projectKey)
def propertyValues = []
issue.attachments.each {
if (it.getFilename().contains(".xlsx")) {
def FileInputStream fis = new FileInputStream(attachmentPath + p.getKey() + "/" + p.getId() + "/" + issue.getKey() + "/" + it.id.toString());
XSSFWorkbook workbook = new XSSFWorkbook(fis)
def org.openxmlformats.schemas.officeDocument.x2006.customProperties.CTProperties props = workbook.getProperties().getCustomProperties().getUnderlyingProperties();
def org.openxmlformats.schemas.officeDocument.x2006.customProperties.CTProperty[] properties = props.getPropertyArray()
for(int i = 0; i < properties.length; i++) {
propertyValues.add(properties[i].getName() + ":" + properties[i].getLpwstr())
}
if (propertyValues.contains(propertyName + ":" + propertyValue)) {
log.warn("All good")
} else {
def comment = "Issue closed automatically due to use of wrong template."
def params = ["resolution": "10003"] // Sets resolution to Declined
def CommentManager commentManager = ComponentAccessor.getCommentManager()
def issueManager = ComponentAccessor.issueManager
commentManager.create(issueManager.getIssueObject(issue.getKey()), user, comment, true)
def workflowTransitionUtil = (WorkflowTransitionUtil) JiraUtils.loadComponent(WorkflowTransitionUtilImpl.class)
workflowTransitionUtil.setIssue(issue)
workflowTransitionUtil.setUserkey(user.getUsername())
workflowTransitionUtil.setParams(params)
workflowTransitionUtil.setAction(workflowActionId)
workflowTransitionUtil.validate()
workflowTransitionUtil.progress()
}
}
}