import com.exalate.basic.domain.hubobject.v1.BasicHubIssue import org.slf4j.Logger /** * Use {@link #create} in the Create Processor Usage: ``` return CreateIssue.create( replica, issue, syncRequest, nodeHelper, issueBeforeScript, remoteReplica, traces, blobMetadataList) { // the issue has been created do any crazy things you would normally do after the issue has been created } ``` */ class CreateIssue { private static def log = org.slf4j.LoggerFactory.getLogger("com.exalate.scripts.CreateIssue") private static def deleteIssue = { proxyAppUser, jIssue -> final def im = com.atlassian.jira.component.ComponentAccessor.issueManager try { im.deleteIssue(proxyAppUser, jIssue as com.atlassian.jira.issue.Issue, com.atlassian.jira.event.type.EventDispatchOption.ISSUE_DELETED, false) } catch (Exception ignore) { im.deleteIssue(proxyAppUser?.getDirectoryUser(), jIssue as com.atlassian.jira.issue.Issue, com.atlassian.jira.event.type.EventDispatchOption.ISSUE_DELETED, false) } } private static def doCreate = { boolean reuseIssue, BasicHubIssue replica, BasicHubIssue issue, syncRequest, com.exalate.node.hubobject.v1_3.NodeHelper nodeHelper, BasicHubIssue issueBeforeScript, com.exalate.api.domain.INonPersistentReplica remoteReplica, List traces, List blobMetadataList, Logger log -> def issueLevelError = { String msg -> new IllegalStateException(msg) } def issueLevelError2 = { String msg, Throwable c -> new IllegalStateException(msg, c) } def toExIssueKey = { com.atlassian.jira.issue.MutableIssue i -> new com.exalate.basic.domain.BasicIssueKey(i.id, i.key) } final def authCtxInternal = com.atlassian.jira.component.ComponentAccessor.getJiraAuthenticationContext() def logIn = { u -> try { //Jira 7 authCtxInternal.setLoggedInUser(u) } catch (Exception ignore) { // Jira 6 //noinspection GroovyAssignabilityCheck authCtxInternal.setLoggedInUser(u?.getDirectoryUser()) } } final def imInternal = com.atlassian.jira.component.ComponentAccessor.issueManager final def umInternal = com.atlassian.jira.component.ComponentAccessor.userManager final def nservInternal = com.atlassian.jira.component.ComponentAccessor.getOSGiComponentInstanceOfType(com.exalate.api.node.INodeService.class) final def hohfInternal2 = com.atlassian.jira.component.ComponentAccessor.getOSGiComponentInstanceOfType(com.exalate.api.hubobject.IHubObjectHelperFactory.class) //noinspection GroovyAssignabilityCheck final def hohInternal2 = hohfInternal2.get(remoteReplica.payload.version) if (issue.key != null) { def existingIssue = imInternal.getIssueObject(issue.key as String) if (existingIssue != null) { if (reuseIssue) { return [existingIssue, toExIssueKey(existingIssue)] } else { throw issueLevelError(""" There is already an issue with key `${issue.key}`. Please delete it or contact Exalate Support """.toString()) } } } def proxyAppUserInternal = nservInternal.getProxyUser() def loggedInUser = authCtxInternal.getLoggedInUser() def reporterAppUser = null if (issue.reporter != null) { reporterAppUser = umInternal.getUserByKey(issue.reporter?.key) } reporterAppUser = reporterAppUser ?: proxyAppUserInternal issue.project = issue.project ?: ({ nodeHelper.getProject(issue.projectKey) })() issue.type = issue.type ?: ({ nodeHelper.getIssueType(issue.typeName) })() try { logIn(reporterAppUser) if (issue.key != null) { def existingIssue = imInternal.getIssueObject(issue.key as String) if (existingIssue != null) { if (reuseIssue) { issue.id = existingIssue.id issue.key = existingIssue.key return [existingIssue, toExIssueKey(existingIssue)] } else { throw issueLevelError(""" There is already an issue with key `${issue.key}`. Please delete it or contact Exalate Support """.toString()) } } } def cir = null try{ cir = hohInternal2.createNodeIssueWith(issue, hohInternal2.createHubIssueTemplate(), null, [:], blobMetadataList, syncRequest) } catch (MissingMethodException e){ cir = hohInternal2.createNodeIssueWith(issue, hohInternal2.createHubIssueTemplate(), null, [:], blobMetadataList, syncRequest.getConnection()) } def createdIssueKey = cir.getIssueKey(); def jIssueInternal = imInternal.getIssueObject(createdIssueKey.id) if(issue.key != null) { def oldIssueKey = jIssueInternal.key def oldIssueId = jIssueInternal.id try { jIssueInternal.key = issue.key jIssueInternal.store() } catch (Exception e) { log.error( """Failed to sync issue key: ${e.message}. Please contact Exalate Support. Deleting issue `$oldIssueKey` ($oldIssueId)""".toString(), e ) deleteIssue(proxyAppUserInternal, jIssueInternal) } } issue.id = jIssueInternal.id issue.key = jIssueInternal.key return [jIssueInternal, toExIssueKey(jIssueInternal)] } catch (com.exalate.api.exception.IssueTrackerException ite) { throw ite } catch (Exception e) { throw issueLevelError2("""Failed to create issue: ${e.message}. Please review the script or contact Exalate Support""".toString(), e) } finally { logIn(loggedInUser) } } /** * @param whenIssueCreatedFn - a callback closure executed after the issue has been created * */ static com.exalate.basic.domain.BasicIssueKey create( boolean reuseIssue, BasicHubIssue replica, BasicHubIssue issue, syncRequest, com.exalate.node.hubobject.v1_3.NodeHelper nodeHelper, BasicHubIssue issueBeforeScript, com.exalate.api.domain.INonPersistentReplica remoteReplica, List traces, List blobMetadataList, Closure whenIssueCreatedFn) { def (_jIssue, _exIssueKey) = doCreate(reuseIssue, replica, issue, syncRequest, nodeHelper, issueBeforeScript, remoteReplica, traces, blobMetadataList, log) com.atlassian.jira.issue.MutableIssue jIssue = _jIssue as com.atlassian.jira.issue.MutableIssue com.exalate.basic.domain.BasicIssueKey exIssueKey = _exIssueKey as com.exalate.basic.domain.BasicIssueKey try { whenIssueCreatedFn() UpdateIssue.update(replica, issue, syncRequest, nodeHelper, issueBeforeScript, traces, blobMetadataList, jIssue, exIssueKey) } catch (Exception e3) { if (!reuseIssue) { final def nservInternal3 = com.atlassian.jira.component.ComponentAccessor.getOSGiComponentInstanceOfType(com.exalate.api.node.INodeService.class) def proxyAppUserInternal3 = nservInternal3.getProxyUser() deleteIssue(proxyAppUserInternal3, jIssue) } throw e3 } return exIssueKey } static com.exalate.basic.domain.BasicIssueKey create( BasicHubIssue replica, BasicHubIssue issue, syncRequest, com.exalate.node.hubobject.v1_3.NodeHelper nodeHelper, BasicHubIssue issueBeforeScript, com.exalate.api.domain.INonPersistentReplica remoteReplica, List traces, List blobMetadataList, Closure whenIssueCreatedFn) { create(false, replica, issue, syncRequest, nodeHelper, issueBeforeScript, remoteReplica, traces, blobMetadataList, whenIssueCreatedFn) } }