Retrieve data with Jira REST API in Automation to update issue fields
Platform Notice: Cloud - This article applies to Atlassian products on the cloud platform.
There is a way to combine the capabilities of Jira REST API with Automation to perform advanced changes to Jira issues. With the growing importance of integrations between systems, you may be in a situation where issues are getting created in your Jira site because of actions taken by users in 3rd party systems or just generally outside Jira. For example, it may be a result of an action taken in Slack, but it may as well be simply an email message that should be turned into an issue. In this article, we will showcase how to set user fields (such as assignee or reporter) based on issue field data that is present in the description or summary fields.
Before we begin
In this example, we will be using Jira REST API to retrieve the Account ID of the user who is supposed to be assigned to the issue. In order to start sending web requests to Jira REST API, you need to create your API token. If you're not familiar with Jira REST APIs, make sure to read our documentation page to find out what it is and how it works.
Building the automation rule
Trigger
Let's say that issues are getting created in Jira but they are missing important Jira fields such as assignee or reporter. Let's go to our project and open Project Settings > Automation. Let's create a rule and pick as the first element of the rule the Issue created trigger.
Creating a variable
However, the information that is supposed to be set in the assignee or reporter fields (or just any other user picker fields) is stored in the description field. The information may be either the name or email address of the user we need to set. In order to retrieve this information, we can use the match function and for more information please read this article.
Let's add the first component to the rule and we're going to use the Create variable action. Type any name for your variable and we use the following regex smart value to match a user's email from the issue's description:
{{issue.description.match(".*Email: (\S+).*")}}
In our example, we'll call the variable matchedEmailAddress.
Send a web request
Now when we have the email address available as a smart variable and we can easily access it, it's time to get the Account ID of the user. As mentioned above, we can use API to search for users by name or email because this is what is supported by the REST API. In order to send a web request via Automation, first you need to add your API token as the Authorization header. For more information on how to do that, please read this Community post.
When this is done, we can now easily send your web request. As the web request URL, we will use the following address with smart value syntax:
https://<jira_instance>/rest/api/3/user/search?query={{matchedEmailAddress.urlEncode}}
The HTTP method we need is GET. Other things to check before you proceed:
- Make sure to include the entire instance URL, for example: my-instance.atlassian.net.
- Also, if you called your variable differently, make sure to change it as well.
- Don't forget to add the urlEncode parameter to your variable.
- Finally, don't forget to tick the option "Delay execution of subsequent rule actions until we've received a response for this web request". We want to make sure the request is completed before we proceed to the next step in the automation.
Update a field
If our web request succeeded, we should have the Account ID we needed. With the Account ID, we can set correctly all user picker fields in Jira. In order to do that, add the Edit issue action and as the value for your field use the following smart value:
{{webResponse.body.first.accountId}}
Summary
You can try to test the rule now. If everything went well, you should see it updates the user picker field of your choice with the right user. If the rule fails for you, you can always use instructions on this page to debug automation rules and smart values.