Level 2 · Lesson 8 · optional
Take online bookings
Level 2 · Lesson 8Goal of this lesson
Give the app a web trigger, a web address (URL) an online booking system can send bookings to, guarded by a booking code kept in an encrypted variable.
Behind, or starting here? Download the code as it should be before this lesson, or in the course workspace run course-catch-up level-2/after-06-diagnosis-expert 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.
This lesson is optional, and it is the most technical one: every step happens in the terminal. Skipping it changes nothing else in the course. Some cars are booked in online, through a booking form on the workshop’s website, not by a person at the desk. A web trigger gives the app a URL that booking system can call. Anyone who has the URL can call it, so the app only accepts requests that carry a secret code.
See this on the Level 2 chart, in the online bookings chapter.
Step 1: Make a booking code and lock it in the key safe
Locked until the step before it is done.
In the same terminal as every other lesson (in a codespace, the codespace terminal), in your Workshop-helper folder:
-
Make a long random code. This prints 64 random letters and digits, too many for anyone to guess:
RunTerminal openssl rand -hex 32 -
Copy the code it prints and keep it somewhere private, such as a password manager, before you go on: it is printed only this once, you need it again in step 5, and the key safe never shows it back to you.
-
Then put it in the key safe; the CLI asks for the value and hides it:
RunTerminal forge variables set --encrypt BOOKING_CODE
The booking system needs the same code to call the app, so store it in that system’s own secret settings. Never write it into its source code, where everyone who can read the code would see it. If you lose the code, make a new one and set it again the same way; the old one then stops working.
Stuck?One common problem, and its fix
I set or changed a variable
Open in the FAQVariables reach your functions with the next deploy:
RunTerminal forge variables list forge deployVariables belong to one environment. Unless you add
-efollowed by an environment name (such as-e production), both commands use development.
Step 2: Only accept bookings with the booking code
Locked until the step before it is done.
Runs in the Atlassian cloud
Web triggerin the workshop: online bookings
A webtrigger module gives the app a URL on Atlassian’s domain that outside systems can call over HTTPS. The function gets the request’s method, headers and body, and returns a status code, headers and a body. The URL works without any Atlassian login, which is why the function must check who is calling.
In the workshop Other systems send in bookings, with no person at the desk.
A new file checks the code first, then the booking, then accepts it exactly as the desk does:
- The booking is saved in the Key-Value Store.
- Its check-in number goes in the queue.
- The caller gets a receipt.
src/bookings.jsA new file: check the code, check the booking, then accept it like the desk does.
Added: import { timingSafeEqual } from 'node:crypto'; | |||
Borrow a way to compare secrets that doesn’t give away, by how long it takes, how much of a guess was right. | |||
Added: import { acceptCheckIn } from './checkins'; | |||
Added: import { validateCheckIn } from './lib/validate'; | |||
Added: | |||
Added: const reply = (statusCode, body) => ({ | |||
A helper that builds an answer: a status code and a JSON body. | |||
Added: statusCode, | |||
Added: headers: { 'Content-Type': ['application/json'] }, | |||
Added: body: JSON.stringify(body), | |||
Added: }); | |||
Added: | |||
Added: function header(request, name) { | |||
Added: const key = Object.keys(request.headers ?? {}).find((k) => k.toLowerCase() === name); | |||
Added: const value = key ? request.headers[key] : undefined; | |||
Added: return Array.isArray(value) ? value[0] : value; | |||
Added: } | |||
Added: | |||
Added: // Compare in constant time, so how long the answer takes gives nothing away. | |||
Added: function sameSecret(given, expected) { | |||
Is the code that came with the request exactly the booking code? | |||
Added: const a = Buffer.from(String(given ?? '')); | |||
Added: const b = Buffer.from(String(expected ?? '')); | |||
Added: return a.length > 0 && a.length === b.length && timingSafeEqual(a, b); | |||
Added: } | |||
Added: | |||
Added: /** | |||
Added: * Online bookings: other systems can send in a booking if they know the booking code. | |||
Added: * The URL is public, so check the code before anything else, and never log it. | |||
Added: */ | |||
Added: export async function run(request) { | |||
What runs each time something calls the app’s web address. | |||
Added: if (!sameSecret(header(request, 'x-booking-code'), process.env.BOOKING_CODE)) { | |||
No booking code, or the wrong one: refuse with 401, “not allowed”. | |||
Added: console.warn('Online bookings: refused a request without the right code.'); | |||
Added: return reply(401, { error: 'Unauthorized' }); | |||
Added: } | |||
Added: let payload; | |||
Added: try { | |||
Added: payload = JSON.parse(request.body ?? ''); | |||
Read the booking from the request’s JSON. If it isn’t JSON, answer 400. | |||
Added: } catch { | |||
Added: return reply(400, { error: 'Send the booking as JSON.' }); | |||
Added: } | |||
Added: const check = validateCheckIn(payload); | |||
Added: if (!check.ok) return reply(400, { error: check.error }); | |||
Added: const checkInId = await acceptCheckIn({ ...check.checkIn, requestedBy: 'online-bookings' }); | |||
From here it is the same as the desk: save, queue, receipt. | |||
Added: return reply(202, { checkInId }); | |||
202 means “accepted, the work happens later”, with the receipt number. | |||
Added: } | |||
Why: Web trigger · Shared secret · Environment variable
Stuck?One common problem, and its fix
I changed code in src/
Open in the FAQWith
forge tunnelrunning 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 deployThe site picks up the new version on its own. You do not need to install again.
Step 3: Open online bookings in the manifest
Locked until the step before it is done.
See this on the Level 2 chart, in the step where a booking arrives at the web trigger.
manifest.ymlChange 1 of 2A web trigger: a URL other systems can call.
type: string | |||
required: true | |||
description: The diagnosis to publish, as plain text. | |||
Added: webtrigger: | |||
A new module: a web address outside systems can call. | |||
Added: - key: online-bookings | |||
Added: function: bookings | |||
Added: response: | |||
Added: type: dynamic | |||
The function decides the answer’s status code itself. | |||
function: | |||
- key: resolver | |||
handler: index.handler | |||
manifest.ymlChange 2 of 2The function behind it.
handler: rovo.lookUpCar |
- key: publish-diagnosis-fn |
handler: rovo.publishDiagnosis |
Added: - key: bookings |
Added: handler: bookings.run |
resources: |
- key: main |
path: src/frontend/index.jsx |
Why: Web trigger
type: dynamic lets the function choose its own status code, such as 401 for a wrong code or 202 for an accepted booking.
Stuck?One common problem, and its fix
I changed manifest.yml
Open in the FAQCheck it, then deploy:
RunTerminal forge lint forge deployIf the change adds a scope or an outside host,
forge deploystops and asks for approval. See Deploy stops after a permission change.
Step 4: Deploy and get the booking address
Locked until the step before it is done.
A web trigger is a new kind of module, so this deploy makes a new major version: approve it, and upgrade the installations before you ask for the address. If the app is installed on Jira and Confluence, the upgrade lists both; run it once for each.
npm run build:ui
forge deploy --approve MAJOR_VERSION_RULE
forge install --upgrade
forge webtrigger create --functionKey online-bookingsThe last command shows one list, Select an installation, with a row for each place the app is installed: its environment, site, product and version. Move with the arrow keys to the row that says Jira on your developer site in the development environment, and press Enter. The URL appears straight away, under Copy your web trigger URL below. Treat it as private too: it is half of what a caller needs.
Stuck?2 common problems, and their fixes
Deploy stops after a permission change
Open in the FAQThat 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 --upgradeforge webtrigger createsays the web trigger “was not found in the provided installation context”Open in the FAQAdding the web trigger made a new major version, and your installations are still on the old one, which has no web trigger. Approve the new version, upgrade the installations, then ask for the URL again:
RunTerminal forge deploy --approve MAJOR_VERSION_RULE forge install --upgrade forge webtrigger create --functionKey online-bookingsIf the app is installed on Jira and Confluence, run the upgrade for both; the CLI asks which one.
Step 5: Send a booking, with and without the code
Locked until the step before it is done.
Send a booking with the code, as the booking system would, using curl, a command that sends a web request from the terminal (it comes with macOS, Linux and Windows 10 or later). Replace the placeholders with your URL and code:
curl -i -X POST '<your-web-trigger-url>' \
-H 'Content-Type: application/json' \
-H 'X-Booking-Code: <your-booking-code>' \
-d '{"car": "Volkswagen Golf", "notes": "Brakes feel soft, please check before the annual inspection.", "reportedBy": "Booking form"}'What each part of the command does:
-iprints the answer’s status line and headers, so you can see the status code.-X POSTsends the request as a POST, the kind that hands something over.- Each
-Hadds one header line: the first says the body is JSON, the second carries the booking code. -dis the body itself, the booking, written as JSON.- The
\at the end of a line means “the command goes on on the next line”.
The answer is 202 with a checkInId, and a few seconds later a repair task for the Volkswagen Golf appears on the board, written by the consumer as before, with its car details card. Send the same request without the X-Booking-Code line: the answer is 401, and nothing is saved.
Stuck?One common problem, and its fix
My check-in stays “waiting” or ends “failed”
Open in the FAQThe consumer ran into a problem. Read its log, which names the step that failed:
RunTerminal forge logs --since 15mThe usual causes are a missing
WORKSHOP_PROJECT_KEY, a Jira space you cannot create work items in, or Forge LLM not being available on the site yet.
Compare with yours: download the code after this lesson.