REST Service Caller Post Function

Easy Integrations comes with a handy post function that allows you to create Confluence pages via Jira workflow transitions. The issue's field/custom field values can be used to generate Confluence pages from templates.

Here are the steps:

  • Go to the desired transition of your workflow, switch to the Post functions tab and click on the Add post function link.
  • Locate the Create Confluence Page using a template page post function and click on the Add button.

Then configure the REST service parameters.



Post Function Name 

You can create a name for your post function. 


Condition

In this section, you can create a decision to execute the REST according to the result of Groovy script.

By default, it can only return true statement. However, you can type your script and return true; to execute REST, or return false; to not execute.

Below there is a sample code of how you can type your own script.


Condition code example:

Condition
import com.atlassian.jira.component.ComponentAccessor import com.atlassian.jira.user.ApplicationUser ApplicationUser currentUser = ComponentAccessor.getJiraAuthenticationContext().loggedInUser log.info("current user:" + currentUser.username) def reporter = issue.reporter.username log.info("reporter: " + reporter) return reporter.equals(currentUser.username)

REST Service Details

In this section, REST details (URL, method, headers, body, credential, etc) are filled.

  • URL: The endpoint of the REST service. You can type any dynamic parameter here using double curly braces (e.g. http://mycompany.com/api/book/{{bookId}} assuming bookId is defined in the Parameters section which is described below).

  • Method: Select Http Method (GET, POST, PUT, DELETE).

  • Headers: Define your HTTP header key/value sets on separate lines as shown in the screenshot.

  • Request Body: Type json request body. You can use dynamic parameters here as well.

{ "assignee" : "{{assignee}}", "reporter" : "{{reporter}}", "userName" : "{{userDisplayName}}", "price" : "{{price}}" }


  • Parameters: Define all of your dynamic parameters here using Groovy Script. You can add static values as well as the dynamic values of fields/custom fields in the Jira issue. Here are some sample usages:

    def parameters = [:] parameters["assignee"] = issue.assignee parameters["reporter"] = issue.reporter def userManager = ComponentAccessor.getUserManager() parameters["userDisplayName"] = userManager.getUserByName("someUserName").getDisplayName() def cf = customFieldManager.getCustomFieldObject("customfield_xxxxx") def price = issue.getCustomFieldValue(cf) parameters["price"] = (null != price) ? price : "No price defined" return parameters

    For more details about parameter assigning and utils, see the Parameters Usage Examples page.
    Also see the ComponentAccessor, UserIssue and Status API pages for all properties and methods.

  • Authorization Type: Choose authorization type - currently BASIC Authentication is supported.

  • Credential: If you choose any Authorization Type, this option becomes active and the credentials you defined in the Credentials section will be listed.

Below is an example:


Post Operations

In this section, you can configure the post function to update a custom field based on a value in the REST response.

  • First, tick the Update custom field checkbox.

  • The custom field drop-down menu will be displayed - select the one that you would like to update.

  • Type jsonpath accordingly to select the value. If the result is not json and you want to update the custom field with the body itself, just leave this text box empty.

See jsonpath examples page for jsonpath samples.


Run asynchronously?

In this section, you can enable async to execute the REST service asynchronously. There are a couple of reasons why you may want to execute asynchronously:

  1. Executing synchronously makes the user wait for the transition for longer than usual. If the host of the REST service responds late or there is a connection time out between the Jira server and the host, the user will wait for the spinner for a long time. So, it may be wise to enable async if your REST service responds slowly.

  2. Some issue parameters may wait for calculation or reindexing. Some plugins may work in the background to populate the actual result of a custom field and you may want to use that value. 

There is a scheduled task named Easy Integrations for Jira - Async Rest Caller Scheduled Task which runs every 2 minutes, checks for these REST calls and then executes. 




View of The Post Function

Please do not forget to publish the workflow to see it in action.



Order of The REST Service Caller Post Function

The order of the REST Service Caller post function is very important and depends on what you want to do. 

If the REST Service Caller post function is BEFORE the "Update change history for an issue and store the issue in the database." post function

  • $issue.getCustomFieldValue($customFieldManager.getCustomFieldObject('customfield_10123')) call returns the old value of customfield_10123

  • $transientIssue.getCustomFieldValue($customFieldManager.getCustomFieldObject('customfield_10123')) call returns the new value of customfield_10123

  • If the Update custom field is selected, the custom field(s) will be updated successfully.

If the REST Service Caller post function is AFTER the "Update change history for an issue and store the issue in the database." post function

  • $issue.getCustomFieldValue($customFieldManager.getCustomFieldObject('customfield_10123')) call returns the new value of customfield_10123

  • If the Update custom field is selected, the custom field(s) will not be updated successfully as the Update change history and store the issue in the database post function is already executed.


So, if you want to use the new value of a field (which is changed via the transition screen) and you also want to update a custom field value, you should use $transitionIssue and place the REST Caller Service post function before the Update change history and store the issue in the database post function.