Course menuForge Cloud

Level 1 · Lesson 7

Hang the bell

Goal of this lesson

Run code when a repair task is created, with nobody looking at a card, and read what it did in the workshop log.

About 20 min4 steps

Behind, or starting here? Download the code as it should be before this lesson, or in the course workspace run course-catch-up level-1/after-06-read-the-repair-task in the folder above your app. It offers to bring your own app folder to this point and keeps its app id (the id: line in manifest.yml, which links the code to your app on the site), so it stays the same app. Your old files are kept in a copy next to it.

So far the app only works when someone opens a card. Some work should happen by itself: when a new repair task goes up, the workshop should check that the car data service knows the car on it. Jira can tell your app about events like that.

See this on the Level 1 chart, in the step where a new work item sets off the product trigger.

Step 1: Write what happens when it rings

Runs in the Atlassian cloud

Product triggerin the workshop: the bell

A trigger module runs a function when something happens in the product, such as avi:jira:created:issue when a work item is created. The function gets an event with the details instead of a click, and nobody sees its answer. It is a separate function from the resolver, with its own key and handler.

In the workshop A bell that rings in the workshop whenever a new repair task goes up.

Further reading, optional: Atlassian docs on Product events (opens in a new tab)

You already know this idea from Jira automation. A rule that starts with the Work item created trigger runs its actions each time someone creates a work item, without anyone clicking anything. A Forge trigger is the same, except that the “actions” are your own code. The details Jira sends along, such as which work item was created, are called the event, like the smart values a rule can read.

A new repair taskcreated in JiraThe bellyour trigger functionThe workshop logforge logsa cardthe eventkey and summarywhat it foundnobody sees its answer

Create src/bell.js. It reuses the summary helper and the car data file, and writes what it finds to the log, because there is no card to show it on:

New filesrc/bell.js

A new file: look up the car on the new repair task and write the result to the log.

Added: import { findCar } from './cars/apiNinjas';

Borrow the car data file. ./ means “starting from this file’s own folder”, src.

Added: import { splitSummary } from './cars/summary';
Added:
Added: // Runs when a work item is created (avi:jira:created:issue).
Added: // No one is looking at a card here, so the result goes to the log.
Added: // Only the Workshop space: Jira rings the bell for every new work item the app can see.
Added: const WORKSHOP_SPACE = 'WS';

The key of your Workshop space. Change it if you gave the space another key.

Added:
Added: export async function run(event) {

What runs when the bell rings. event holds what Jira sent: the new work item.

Added: const { key, fields } = event.issue;

Take the work item’s key and fields out of the event.

Added: if (fields?.project?.key !== WORKSHOP_SPACE) return;

Stop here unless the work item is in the Workshop space, like the JQL condition project = WS on an automation rule. Jira rings the bell for every new work item the app can see, in every space, and the rest of this function sends part of the summary to API Ninjas.

Added: const summary = fields?.summary ?? '';

Its summary, or empty text if there is none.

Added: console.log(`Bell: ${key} "${summary}" went up.`);

Write a line to the logs. Nobody looks at a card here, so the logs are the only place to see what happened.

Added: const { name, make, model } = splitSummary(summary);
Added: try {
Added: const car = model ? await findCar(make, model, process.env.API_NINJAS_KEY) : null;

Look the car up only if the summary names a model.

Added: console.log(car ? `Bell: the car data service knows the ${name}.` : `Bell: no car data for "${name}".`);

Write whether the service knows the car.

Added: } catch (error) {
Added: console.error('Bell: car lookup failed:', error.message);
Added: }
Added: }

Why: Product trigger · forge logs

The event carries the new work item, including its key, its space and its summary, so the function does not need to ask Jira for them. Jira rings the bell for every new work item the app can see, in every space on the site, so the function first checks that the work item is in the Workshop space and stops otherwise. Without that line, the summaries of other teams’ work items would go to API Ninjas too.

Stuck?One common problem, and its fix
  • I changed code in src/

    With forge tunnel running on your own computer, save the file and reload the Jira page: the tunnel serves your laptop’s code. Without the tunnel, or in a codespace in your browser, deploy:

    RunTerminal
    forge deploy

    The site picks up the new version on its own. You do not need to install again.

    Open in the FAQ

Step 2: Hang the bell in the manifest

See this on the Level 1 chart, in the step where Jira’s event reaches the product trigger.

Add two things to the manifest: the trigger, which says which event to listen for and which function to run, and the function itself, which points at the run export in src/bell.js. An export is a piece of code a file offers to the outside; export async function run in src/bell.js is what makes run reachable from the manifest. The handler line names it as file, then a dot, then export: bell.run.

Editmanifest.ymlChange 1 of 2

A trigger that fires when a work item is created.

render: native
title: Car details
icon: https://developer.atlassian.com/platform/forge/images/icons/issue-panel-icon.svg
Added: trigger:
Added: - key: new-repair-task-bell

The trigger’s own name in the manifest.

Added: function: bell

Which function to run: the one whose key is bell, a few lines below.

Added: events:
Added: - avi:jira:created:issue

The event to listen for: a work item was created.

function:
- key: resolver
handler: index.handler
Editmanifest.ymlChange 2 of 2

The function it runs.

function:
- key: resolver
handler: index.handler
Added: - key: bell
Added: handler: bell.run

Where its code is: the run export in src/bell.js.

resources:
- key: main
path: src/frontend/index.jsx

Why: Product trigger · Function

The scope from lesson 6, read:jira-work, also covers receiving work item events, so this needs no new permission.

Stuck?2 common problems, and their fixes
  • I changed manifest.yml

    Check it, then deploy:

    RunTerminal
    forge lint
    forge deploy

    If the change adds a scope or an outside host, forge deploy stops and asks for approval. See Deploy stops after a permission change.

    Open in the FAQ
  • After changing manifest.yml, forge lint or forge deploy reports an error in the manifest

    Most often the spaces at the start of the lines are off. In manifest.yml those spaces matter: they show what belongs inside what, the way an outline does. Every line you add must start with exactly as many spaces as the line it sits next to in the lesson’s change, and it must use spaces, not tabs. Compare your file with the change, line up the new lines, save, and run:

    RunTerminal
    forge lint

    If you are still stuck, download the code after the lesson (the link at the end of each lesson) and compare its manifest.yml with yours.

    Open in the FAQ

Step 3: Deploy the bell

A new module with no new permissions is a minor version: deploy, and the site picks it up by itself.

RunTerminal
forge deploy
Stuck?2 common problems, and their fixes
  • The tunnel is running while I deploy or upgrade

    Stop it with Ctrl+C first, then deploy or upgrade, then start it again. While the tunnel runs, Jira runs the code on your laptop instead of your last deploy, so if you deploy with it running you cannot tell which version you are testing. Changes to the manifest only reach the site through a deploy, never through the tunnel.

    Open in the FAQ
  • Deploy stops after a permission change

    That is expected, not an error. A new scope or outside host makes a new major version, which you approve. Then move each installation onto it. On your own developer site you are the admin, so you accept it yourself:

    RunTerminal
    forge deploy --approve MAJOR_VERSION_RULE
    forge install --upgrade
    Open in the FAQ

Step 4: Ring it, and read the workshop log

Lives on your laptop

forge logsin the workshop: the workshop log

forge logs shows what your functions wrote with console.log and console.error, and errors Forge caught, for the development environment by default. Lines can take a minute to arrive. With forge tunnel running, the same lines print in the tunnel’s terminal instead, as they happen.

In the workshop The log where the workshop writes down what it did and what went wrong.

Further reading, optional: Atlassian docs on Forge CLI reference (opens in a new tab)

See this on the Level 1 chart, in the step where the trigger’s function writes to the logs.

Give the deploy a minute to reach your site first: a work item created in the first seconds after it may not ring the bell. Then create a repair task in the Workshop space, such as Ford Focus – tyre change. Wait a minute, then read the log:

RunTerminal
forge logs --since 15m

--since 15m means “only the last fifteen minutes”, so older lines from earlier lessons don’t get in the way.

You should seeExampleYour keys, cars and timestamps will differ.
$ forge logs --since 15m
INFO  Bell: WS-4 "Ford Focus – tyre change" went up.
INFO  Bell: the car data service knows the Ford Focus.
Stuck?One common problem, and its fix
  • I changed code in src/

    With forge tunnel running on your own computer, save the file and reload the Jira page: the tunnel serves your laptop’s code. Without the tunnel, or in a codespace in your browser, deploy:

    RunTerminal
    forge deploy

    The site picks up the new version on its own. You do not need to install again.

    Open in the FAQ

Compare with yours: download the code after this lesson.

What you built

One app, in one folder: a car details card on every repair task that reads the repair task as the person looking at it, looks up the car with an outside data service and a key nobody can see, and a bell that checks every new repair task by itself. Level 2 adds a check-in desk to the same app, and the work behind it.

Check what you know

Eight questions on Level 1. Each answer explains itself.

  1. Question 1 of 8

    Where does src/resolvers/index.js run?

  2. Question 2 of 8

    The card calls invoke('getCar'). What has to exist for that to work?

  3. Question 3 of 8

    Where does the API Ninjas key live?

  4. Question 4 of 8

    You added api.api-ninjas.com to the manifest and ran forge deploy. It stops. What now?

  5. Question 5 of 8

    With forge tunnel running, you change the manifest. When does Jira see the change?

  6. Question 6 of 8

    How does getCar know which work item the card is on?

  7. Question 7 of 8

    readSummary calls api.asUser().requestJira(…). Which scope does the app need?

  8. Question 8 of 8

    Who sees what the bell’s function returns?

Type at least two letters.