This document discusses using triggers to automate actions in Forge apps. It begins with an overview of triggers and then discusses:
- Product triggers that are triggered by events in Atlassian products like Jira, Confluence etc.
- Web triggers that are triggered by HTTP requests to a Forge function.
- How to authenticate and make requests to external services like Opsgenie from Forge functions in response to triggers.
- Demos of building a Forge app that responds to Jira issue creation by assigning the issue and notifying Opsgenie.
The document provides details on the event payload formats, making authenticated requests, and deploying/managing the Forge app lif
13. APP FEATURES
• Tickets should be automatically assigned to a person
currently on-call.
• If the ticket is about a problem with a service and
severity is critical, then a person on-call should be
alerted.
• When an on-call person acknowledges an alert, ticket
should be commented
33. Opsgenie - making a request
// Use environment variables to get API TOKEN, never hardcode such
things.
const scheduleId = getEnvVariable("OPSGENIE_SCHEDULE_ID");
const opsGenieApiKey = getEnvVariable("OPSGENIE_API_KEY");
console.log(`Looking for on-call person for schedule ${scheduleId}
`);
const onCallEmail = await fetchOnCall(scheduleId, opsGenieApiKey);
console.log(`Determined on-call person email to be ${onCallEmail}`);
// If we did not get an email from opsgenie then too bad; What can
we do?
if (onCallEmail === null) {
return {};
}
34. async function fetchOnCall(scheduleId: string, apiKey: string) {
const url = `https://api.opsgenie.com/v2/schedules/${scheduleId}/
on-calls?flat=true`;
let response = await api.fetch(url, {
method: "GET",
headers: {
"Authorization": `GenieKey ${apiKey}`
}
});
if (!response.ok) {
throw `Cannot get opsgenie schedule. Status: $
{response.status}, ${response.statusText}`;
}
const responseBody = await response.json();
[…]
}
Opsgenie - making a request
35. Jira - making a request
export async function assignJiraIssue(issueId: string, userId: string) {
const url = `/rest/api/3/issue/${issueId}/assignee`;
const body = {
"accountId": userId
};
let response = await api
.asApp()
.requestJira(url, {
method: "PUT",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify(body)
});
[…]
}
36. Agenda
A case for automation
Anatomy of a Forge App
Under the covers
Demo
Product triggers
Web triggers
40. Example function
export async function run(request) {
return {
headers: {
"Content-Type": ["text/plain"]
},
statusCode: 200,
body: `Hello from web trigger.n ${request.body}`
}
}