When talking about automation and Jira cloud, the first thing that comes to mind is Automation for Jira (A4J), and rightfully so; it can solve quite a number of use cases. Automating your repetitive tasks using a no-code rule builder, that's the dream. However, there are instances wherein a built-in action is not available and a custom app would best fit e.g., creating a new board, or creating a new sprint – this is where Forge comes in as you can perform these actions by using REST APIs invoked from a Forge app.
But wait, why not use Forge all the way? Yes, for most issue-triggered use cases, there are Forge Jira events that you can use to trigger an action but currently, Sprint-related triggers aren’t one of them.
That said, we can integrate A4J and Forge to unlock new automation use cases.
In this article, I’ll demonstrate how to set up a trigger listening for a Jira Sofware event (e.g., Sprint completed) in A4J and call a Forge app via a web trigger URL.
We’ll use the following to make this happen:
Web trigger module to expose a URL that the automation can call
Jira Software REST APIs to make the desired changes
At the end of this article, you’ll be able to trigger your Forge app from an event received by A4J.
Open your CLI and call forge create
Name your app e.g., forge-web-trigger-example
Select the category Triggers and Validators
Select webtrigger as the template
In your CLI, go to your project directory and call forge deploy, followed by forge install and choosing the Jira cloud instance that you want the automation to run on.
The web trigger URL will be used as a parameter for your A4J action. To secure this, instructions can be found in this link.
Get the app’s installation ID by calling forge install list in your project directory
Call forge webtrigger <installationId> then select the web trigger you want to build the URL for; this should only be one if there were no modifications to the code
Copy the generated web trigger URL
By default, the URLs provided by forge webtrigger have no built-in authentication. As such, anyone can use the URL (and, by extension, invoke its related function) without providing an authentication token. You should keep these URLs secure.
Alternatively, you can also implement authentication inside the trigger itself. For example, you can add a check for an Authorization header in the request and validate any provided token.
Create a trigger e.g., Sprint completed
Add the action to Send web request
In the Web request URL, key in the web trigger URL that you got for your Forge app
You can send additional information that you would need in your Forge app to make a REST API call. In this screenshot, I am passing sprintId, and sprintName while using the sprint smart values
Optional but recommended Add the Authorization header that contains a unique token (feel free to generate one) that you will later validate within your Forge app - this will make your security team happy 🙂
Publish the rule
After this step, you can already trigger your Forge app once you close a sprint. Not so hard, is it? 🙂
You can perform any action once the Forge app is triggered, but in this article, let’s do a quick Jira Software REST API call as an example.
Install the API package
In your CLI, go to your project directory
Install the API package by running npm install @Forge/api
Modify index.jsx
Add the necessary import statement to call a REST API (see code below)
Refactor buildOutput() to remove unnecessary items in it i.e., X-Request-Id, and change the statusCode and statusText to 204 and No Content, respectively
Make the run() function async since we are to call the async requestJira function in it
Inside run(), call the REST API
For simplicity and illustration purposes, I only used the Get sprint API. However, you can call any supported Jira, JSM, and JSW REST API depending on your use case e.g., if you prefer to automatically create a new sprint, and set a sprint property every time a sprint is closed, then that is also possible.
Once done, index.jsx should look something like this:
// Necessary import for calling Jira REST APIs
import api, { route } from "@forge/api";
const buildOutput = () => ({
headers: {
'Content-Type': ['application/json']
},
statusCode: 204,
statusText: 'No Content'
});
export const run = async (req) => {
// This is the request from A4J
console.log(req);
// Parses the request body from A4J as it is a string when we got it
const parsedBody = JSON.parse(req.body);
// Gets the Authorization header
// Yes, the header keys are received in lower case
const authToken = req.headers['authorization'];
// If you decided to add Authorization header in the A4J request
// This is a basic handling of that token
if(authToken != 'Bearer mySecureToken' ) {
console.log('Invalid token');
return {
statusCode: 401,
statusText: 'Unauthorized'
};
}
// console.log('Correct token');
// Calling the GET sprint API passing a parameter from the A4J action
// You can change the API depending on your use case
const response = await api.asApp()
.requestJira(route`/rest/agile/1.0/sprint/${parsedBody.sprintId}`);
const data = await response.json();
// console.log(data);
const result = buildOutput();
return result;
};
Before calling forge deploy, make sure to add the necessary permissions in the manifest.yml file depending on the REST API resource you are calling, and in this case, this is the permission added.
permissions:
scopes:
- read:sprint:jira-software
Since the manifest file has been modified be sure to call forge install --upgrade after a successful deployment.
In these few steps, we can further expand the use cases that Jira Cloud family (including JSM and JSW) can support making our everyday lives run smoother with the help of automation.
Let us know in the comments below if you have any Automation use cases you want to share.
Ian Ragudo
Developer Advocate
Atlassian
1 accepted answer
1 comment