Creating an Issue in Jira by Submitting a Form in Confluence

As a support manager, I want to allow users to submit a form using Forms for Confluence, which will be sent to ScriptRunner on a Jira instance. Once received, ScriptRunner creates a new issue with the information submitted. The form submission must also be recorded locally in Confluence in the internal database. 


This scenario describes the situation where a submitted form can be handled in the usual way and then submitted to ScripRunner for Jira where some processing can be done on the submitted form and an issue created.

Please note the following instructions are for demonstration. Each Confluence and Jira instance can have their own security policies and custom configurations that should be taken into consideration. This is just a small demonstration of what steps would be required to get basic functionality working. The powerful combination of ScriptRunner for Jira and Forms for Confluence is capable of much more.

Instructions

Setting up the application link between Confluence and Jira

In this step, we set up an application link that will link Confluence and Jira.

  1. In Confluence, navigate to Confluence Administration > General Configuration > Application Links. The location of Application Links in Jira depends on the version of Jira.
  2. Enter the URL of the relevant Jira instance in the Application field and click Create New Link.
    The Configure Application Links screen.

    You might be required to validate your credentials at this point.
  3. Tick the relevant boxes and continue. If you are an administrator on both instances make sure you select the associated checkbox. This can save some time as the remote application link will automatically be created on the Jira instance (username and password should be the same on both instances).
    Link applications, credential verification screen.
  4. You should see a message asking you to wait whilst being redirected to the Jira instance.
  5. You will once again be presented with a window to validate your credentials, but this time it is for Jira.
  6. The next window will display some information about the link that will be created in Jira (similar to the one above). Once Continue is clicked, the link will be created in Jira and you will be redirected back to Confluence.
  7. A success message should be displayed, and the application link will appear.

    Success message for creation of Application Link.

  1. Click Edit on the application link, in the Application Links section.
  2. Both outgoing and incoming authentication need to be set up. For the purpose of this demo we will be using 2-Legged OAuth. If there are problems with setting up the authentication such as the page not loading properly, try logging out and back in to Jira/Confluence.
    Outgoing Authentication screen in the Application Link configuration.
    Incoming Authentication screen in the Application Link configuration.
  3. Do the same authentication configuration for the application link in Jira and Confluence.

Setting up the ScriptRunner REST endpoint in Jira

  1. More information about ScriptRunner REST endpoints can be found in Adaptavist's ScriptRunner documentation.
  2. Navigate to Add-ons > ScriptRunner > REST Endpoints.
  3. Click Custom Endpoint and add some code that will process the form that will be submitted from Confluence.
  4. Our custom endpoint is below with imports omitted. It captures the submitted form which contain the following fields: Project, Summary and Description. The issue is then created and a message sent back to Confluence which the user who submitted the form will see as an info message.

    groovy
    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 @BaseScript CustomEndpointDelegate delegate receiveFromForms(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 projectManager = ComponentAccessor.getProjectManager() def project = projectManager.getProjectObjByKey(forms.project) // Get a user who has permissions to create issues // This must be changed to an existing user on your instance 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, that is 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.debug "Created issue : ${issue.key}" return Response.ok("Thanks for creating an issue").type(MediaType.TEXT_HTML).build() }
  5. Note that in this case if the form submitted does not contain these exact fields, an error will be shown to the user who submitted the form.

Setting up a ScriptRunner Form Id in Confluence

  1. Navigate to Confluence Administration > General Configuration > Forms for Confluence.
  2. Click Add a New Form Configuration.
  3. Set up a new Form ID using ScriptRunner as the destination.
  4. Set up another unique Form ID with destination type set as Internal Database. This one will be use to store the submitted form on Confluences internal database.

Setting up a Form in Confluence

  1. Create a new page.
  2. Add a Form to a page.
  3. Edit the Forms macro and enter the two Form IDs created in the previous step.
     
  4. Create the input fields in the Form that the ScriptRunner REST endpoint is expecting to receive, in this case it is Project, Summary and Description. They are case sensitive.
    Placeholder Input Field macros.
    The saved and rendered Create Issue Form.
  5. Populate the fields with some values. In this case the project input field value must match an actual project key in Jira. In this example our project in Jira is an Agile project. Other types of projects might not work with the REST endpoint in this example.

    On first submission you might be required to authenticate.

    Required authentication error message.

  6. Follow the link and click Allow.
  7. Submit the Form again.
  8. If all went well the user should receive a message back from the Jira instance. In this case we show a message containing a link to the Jira issue created.
    Successful response info message.
  9. Note that all responses from ScriptRunner, as above in the info message, will be saved in the internal database for Confluence through the associated Form ID. This can be accessed by navigating to Confluence Administration > General Configuration > Forms for Confluence > ScriptRunner Responses.
    Form Configuration screen.

On this page