+ * This script should be used in a Jira REST Endpoint.
+ * The script creates an issue, baed based on the data sent by a Forms for Confluence form.
+ * Form attachments are also processed and added to the new issue.
+import com.atlassian.jira.component.ComponentAccessor
+import com.atlassian.jira.issue.IssueInputParametersImpl
+import com.onresolve.scriptrunner.runner.rest.common.CustomEndpointDelegate
+import groovy.json.JsonSlurper
+import groovy.transform.BaseScript
+import javax.ws.rs.core.MediaType
+import javax.ws.rs.core.MultivaluedMap
+import javax.ws.rs.core.Response
+import groovy.json.JsonBuilder
+import java.io.FileOutputStream
+import org.apache.commons.codec.binary.Base64
+import com.atlassian.jira.issue.AttachmentManager
+import com.atlassian.jira.issue.attachment.CreateAttachmentParamsBean
+import com.atlassian.jira.issue.history.ChangeItemBean
+import com.atlassian.jira.issue.AttachmentError
+import com.atlassian.fugue.Either
+import com.atlassian.jira.config.util.AttachmentPathManager.PropertiesAdaptor
+@BaseScript CustomEndpointDelegate delegate
+receiveFormWithAttachments(httpMethod: "POST", groups: ["jira-administrators"]) { MultivaluedMap queryParams, String body ->
+ def params = new IssueInputParametersImpl()
+ def issueService = ComponentAccessor.getIssueService()
+ def constantsManager = ComponentAccessor.getConstantsManager()
+ def forms = new JsonSlurper().parseText(body) as Map<String,String>
+ def attachmentManager = ComponentAccessor.getAttachmentManager()
+ def projectManager = ComponentAccessor.getProjectManager()
+ def project = projectManager.getProjectObjByKey(forms.project)
+ // Get a user who has permissions to create issues
+ def user = ComponentAccessor.getUserManager().getUserByKey("admin")
+ params.setProjectId(project.id)
+ // Defines the Type of Ticket to create. Please note all Jira types doesn't support all issue types, for example you can not create a "Bug" in Jira Core.
+ params.setIssueTypeId(constantsManager.allIssueTypeObjects.findByName("Task").id)
+ params.setReporterId(user.name)
+ params.setSummary(forms.summary)
+ params.setDescription(forms.description)
+ def createValidationResult = issueService.validateCreate(user, params)
+ assert !createValidationResult.errorCollection.hasAnyErrors()
+ def issue = issueService.create(user, createValidationResult).issue
+ log.warn "Created issue : ${issue.key}"
+ // Add attachment if present
+ boolean attachmentPresent = false
+ def formsAttachment = forms.attachments
+ if (formsAttachment != null) {
+ attachmentPresent = true
+ if (attachmentPresent) {
+ // Respect Jira's attachment configuration
+ if (attachmentManager.attachmentsEnabled()) {
+ // Set the attachments directory used in Jira
+ String path = PropertiesAdaptor.ATTACHMENTS_DIR + "/" + forms.project[0] + "/10000/"
+ File attachmentDir = new File(path)
+ if (attachmentDir.exists()) {
+ forms.attachments.each {
+ String fileName = it.filename
+ String contentType = it.contentType
+ String content = it.content
+ File issueAttachment = new File(path + fileName)
+ issueAttachment.getParentFile().mkdirs()
+ issueAttachment.createNewFile()
+ // Decode the attachment content
+ byte[] decodedContent = Base64.decodeBase64(content)
+ FileOutputStream outputStream = new FileOutputStream(issueAttachment)
+ outputStream.write(decodedContent)
+ // Create the attachment
+ CreateAttachmentParamsBean bean = new CreateAttachmentParamsBean.Builder(issueAttachment, fileName, contentType, user, issue).build()
+ Either<AttachmentError,ChangeItemBean> result = attachmentManager.tryCreateAttachment(bean)
+ AttachmentError attachmentError = (AttachmentError) result.left().get();
+ log.warn("AttachmentError '" + attachmentError.getLogMessage())
+ log.warn("Attachment created!")
+ log.warn("Could not create attachment - attachment directory does not exist!")
+ log.warn("Attachment submitted from Forms, but Jira attachments disabled!")
+ return Response.ok("Thanks for creating an issue").type(MediaType.TEXT_HTML).build()