When a test turns flaky, file a ticket for its code owners and tell their team in Slack
A flaky test gets fixed fastest when the team that owns it hears about it right away. Trunk already knows each test’s code owners from your CODEOWNERS file. This recipe uses that to route every newly flaky test to its owners: it files a ticket in your tracker, assigns it to the team, and posts in the team’s Slack channel.It’s a small service you run yourself, built from one webhook and two API calls:
If you only need tickets, and not per-team routing or Slack messages, automatic ticketing does it with no code.
Before you begin
You need:
A ticketing integration set as the test collection’s default. Connect Jira, Linear, or Asana, and select it in the collection’s ticketing settings. Without a default, creating a ticket fails with TICKETING_NOT_CONFIGURED.
An API key with the Create tickets permission. See Create an API key. Reading tests needs no permission.
A Slack bot token with the chat:write scope, and the bot invited to each team’s channel.
A webhook endpoint subscribed to v2.test_case.status_changed, pointed at the service you build below. See Webhooks to add one, and copy its signing secret.
1. Map code owners to teams
Trunk returns each test’s owners exactly as they appear in your CODEOWNERS file, such as @acme/payments. Keep a map from those owners to where each team wants to hear about flaky tests:
type Team = { slackChannel: string; // channel ID slackGroup?: string; // Slack user group ID, to @-mention the team assignee?: unknown; // the tracker's assignee value; see step 4};const TEAMS: Record<string, Team> = { "@acme/payments": { slackChannel: "C0PAYMENTS", slackGroup: "S0PAYONCALL", assignee: { accountId: "5b10ac8d82e05b22cc7d4ef5" }, // Jira }, "@acme/web": { slackChannel: "C0WEB", slackGroup: "S0WEBONCALL", assignee: "0a8e4852-8514-4f48-a7e6-266b756e0343", // Linear },};// Tests with no owner, or an owner missing from the map, go here.const FALLBACK: Team = { slackChannel: "C0CIHEALTH" };
Mentioning the team’s Slack user group is what makes the message an assignment and not just a notification.
2. Receive the webhook
Verify the signature, then act only when a test moves into the flaky state. Respond quickly and do the work in the background, so a slow ticket call doesn’t time out the delivery:
Fetch the test before acting. Webhooks can arrive late or be retried, so this confirms the test is still flaky, gets its owners, and shows whether a ticket is already linked. Adding include=metrics returns its recent failure rate for the Slack message:
const API = "https://api.trunk.io/v2";const headers = { Authorization: `Bearer ${process.env.TRUNK_API_KEY}`, "Content-Type": "application/json",};const getTest = async (testId: string) => { const res = await fetch( `${API}/tests/${testId}?include=metrics&period=7d`, { headers }, ); if (!res.ok) throw new Error(`Get a test: ${res.status}`); return res.json();};
Call Create a ticket for a test. Trunk files the ticket in the collection’s default ticketing integration, writes the title and description, and links the ticket to the test. The description includes the test’s location, failure rate, owners, and most common failure reasons.Set the assignee through fields, using the shape your tracker expects. Anything you pass is merged over the default fields saved on the integration:
const createTicket = async (testId: string, assignee?: unknown) => { const res = await fetch(`${API}/tests/${testId}/create-ticket`, { method: "POST", headers, body: JSON.stringify({ fields: assignee ? { assignee } : {} }), }); if (res.status === 201) return res.json(); // The ticket may have been filed even though the call timed out. if (res.status === 504) return (await getTest(testId)).ticket; // Another delivery already filed one. const { error } = await res.json(); if (error?.code === "TEST_ALREADY_LINKED") return (await getTest(testId)).ticket; throw new Error(`Create a ticket: ${res.status} ${error?.code}`);};
The response is the linked ticket, including its key, url, status, and assignee.
Don’t retry a 504 blindly. The ticket can be filed after the call times out, so read the test first: if its ticket is set, the ticket exists.
5. Tell the owning team
Post to the team’s channel with the test, why it was flagged, and the ticket:
const notifyTeam = async (team: Team, owner: string, test: any, ticket: any) => { const mention = team.slackGroup ? `<!subteam^${team.slackGroup}> this test is yours (\`${owner}\` in CODEOWNERS).` : "This test has no owner. Claim it and add it to CODEOWNERS."; const m = test.metrics ?? {}; const monitors = test.activeMonitors.map((a: any) => a.monitorType).join(", "); await fetch("https://slack.com/api/chat.postMessage", { method: "POST", headers: { Authorization: `Bearer ${process.env.SLACK_BOT_TOKEN}`, "Content-Type": "application/json", }, body: JSON.stringify({ channel: team.slackChannel, text: [ `:warning: *Newly flaky:* \`${test.name}\` (${test.file})`, mention, `• Last 7 days: ${m.failureCount} failures in ${m.runCount} runs (${((m.failureRate ?? 0) * 100).toFixed(1)}%), flagged by \`${monitors}\``, `• Quarantined: ${test.quarantineState === "quarantined" ? "yes" : "no"}`, ticket ? `• Ticket: <${ticket.url}|${ticket.key}>` : "• Ticket: not created", `• <${test.url}|View in Trunk>`, ].join("\n"), }), });};
Put it together
const routeFlakyTest = async (testId: string) => { const test = await getTest(testId); if (test.status !== "flaky") return; const owner = test.codeowners.find((o: string) => o in TEAMS); const team = owner ? TEAMS[owner] : FALLBACK; let ticket = test.ticket; if (!ticket) { try { ticket = await createTicket(testId, team.assignee); } catch (err) { console.error(err); // still tell the team } } await notifyTeam(team, owner ?? test.codeowners[0] ?? "none", test, ticket);};
A test can list several owners. This picks the first one found in your map. To notify every owning team instead, post once per matching owner and keep the single ticket, since a test carries at most one.
Test it
You don’t have to wait for a test to turn flaky. Find a flaky test with List and search tests, then call routeFlakyTest with its id: