| description | Learn how to automatically create Linear issues with Flaky Tests webhooks |
|---|
Trunk allows you to automate Linear Issue creation through webhooks. This will allow you to create Linear issues and auto-assign according to CODEOWNERS.
This guide will walk you through integrating Trunk Flaky Tests with Linear Issues through webhooks. You will be able to automatically generate Linear issues for new flaky tests found in your repo. This guide should take 15 minutes to complete.
Trunk also has a built-in Linear integration for ticket creation. You only need to use webhooks if you want to automate ticket creation or need additional customization.
Before you can create a webhook to automate GitHub Issue creation, you need to create an API token to authorize your requests.
- In the Linear app, navigate to settings by holding
Gand pressingS, or by clicking on your profile on the top left and clicking Settings. - Under Account > Security & Access > Personal API Keys, Click New API Key to create a new access token.
- Copy the new API key and save it in a secure location. You'll need to use this later.
Trunk uses Svix to integrate with other services, such as creating Linear Issues through webhooks.
You can create a new endpoint by:
-
Login to Trunk Flaky Tests
-
From your profile on the top right, navigate to Settings
-
Under Organization > Webhooks, click Automate Linear Issues Creation.

-
Paste the Linear GraphQL API endpoint into Endpoint URL, which is:
https://api.linear.app/graphql. -
Review the transformation code automatically generated for Linear issues, you can customize this transformation at any time. Learn more about customizing transformations.
-
Create the new endpoint. You will be redirected to the endpoint configuration view.
If you're having trouble adding a new webhook endpoint with Svix, please see the Adding Endpoint docs from Svix.
The Linear GraphQL API requires some custom headers. You can configure custom headers in the endpoint configuration:
- You can add custom headers under Webhooks > Advanced > Custom Headers.
- Fill in the Key and Value referencing the table below, and click the + button to add each header.
You'll need to configure the following headers.
| Key | Value |
|---|---|
Authorization |
<YOUR_API_TOKEN> |
You need to find your Linear team, project, and label IDs to create issues with the appropriate labeling. You can do this by querying your Linear project using cURL.
First, you'll need to find your team ID so you can create Linear issues under the correct team. You can make a request in your terminal using cURL, or a similar tool.
You'll need your Linear API key from step 1.
curl \
-X POST \
-H "Content-Type: application/json" \
-H "Authorization: <LINEAR_API_KEY>" \
--data '{
"query": "query Teams { teams { nodes { id name } }}"
}' \
https://api.linear.app/graphqlYou will receive a response that contains your team UID, for example:
{
"data": {
"teams": {
"nodes": [
{
"id": "9bd0672b-7766-4a7c-3233-8ce37fdbb790",
"name": "Your Linear Team"
}
]
}
}
}If you want to create issues under a specific project, you'll need to find its project ID. You can use a query like this:
curl \
-X POST \
-H "Content-Type: application/json" \
-H "Authorization: lin_api_vw3gMdb2NJN9TQ66JCgBKLqNSNY6I8cH5qxwM6EW" \
--data '{
"query": "query Projects { projects { nodes { id name } }}"
}' \
https://api.linear.app/graphqlYou'll receive a response that contains your projects and their IDs, for example:
{
"data": {
"projects": {
"nodes": [
{
"id": "ef19b35e-ce4f-4132-9705-811d4d6c8c08",
"name": "Flaky Tests"
}
]
}
}
}If you want to create issues with a specific label, you'll need to find its label ID. You can use a query like this:
curl \
-X POST \
-H "Content-Type: application/json" \
-H "Authorization: lin_api_vw3gMdb2NJN9TQ66JCgBKLqNSNY6I8cH5qxwM6EW" \
--data '{
"query": "query OrgLabels { organization { labels { nodes { id name } } }}"
}' \
https://api.linear.app/graphqlYou'll receive a response that contains your labels and their IDs, for example:
{
"data": {
"organization": {
"labels": {
"nodes": [
{
"id": "e0e9f98e-c90c-40cd-939e-06ff7bd57b45",
"name": "Feature"
},
{
"id": "ce07d3bd-dee8-4bf6-979e-778dd94f15af",
"name": "Bug"
},
{
"id": "536dd774-dc33-4e70-aecc-8b00d1f04a9d",
"name": "Improvement"
},
...
]
}
}
}
}Transformations are custom code snippets you can write to customize the Linear issues created by the webhook. A working template transformation will be added automatically for your webhook, but you can further customize the behavior of this webhook.
- In the endpoint configuration view, navigate to the Advanced tab. Under Transformation, toggle the Enabled switch.
- Click Edit transformation to update your transformation code, and click Save to update the transformation.
- You can test the transformation by selecting the
v2.test_case.status_changedpayload and clicking Run Test. This will test the transformation but not send a message. You will learn to send a test message in step 6.
The generated webhook template contains several configurable constants out of the box:
| Constant | Description |
|---|---|
LINEAR_TEAM_ID | (Required) Your Linear team ID. Learn about finding your team ID. |
LINEAR_PROJECT_ID | (Optional) The Linear project ID assigned to new issues. Learn more about finding your project ID. |
LINEAR_LABEL_IDS | (Optional) Array of label IDs assigned to new issues. Learn about finding your label IDs. |
Here is the provided transformation for context. You can customize your Linear Issues integration by following the Linear API and Svix transformations documentation.
{% hint style="info" %}
The default transformation only creates issues when new_status === "FLAKY". If you also want to create issues for tests marked as Broken (consistently failing at a high rate), update the filter condition. For example, change new_status !== "FLAKY" to new_status !== "FLAKY" && new_status !== "BROKEN" to handle both statuses.
{% endhint %}
/**
* @param webhook the webhook object
* @param webhook.method destination method. Allowed values: "POST", "PUT"
* @param webhook.url current destination address
* @param webhook.eventType current webhook Event Type
* @param webhook.payload JSON payload
* @param webhook.cancel whether to cancel dispatch of the given webhook
*/
// Your Linear Team ID from step 3 above. This is required!
const LINEAR_TEAM_ID = "";
// The Linear project ID you want issues assigned to from step 3 above. Optional.
const LINEAR_PROJECT_ID = "";
// IDs of any labels you want added to the linear issue. Optional.
const LINEAR_LABEL_IDS = [];
function handler(webhook) {
const new_status = webhook.payload.new_status;
const resolvedProjectId = LINEAR_PROJECT_ID ? `"${LINEAR_PROJECT_ID}"` : undefined;
const resolvedLinearLabels = LINEAR_LABEL_IDS.map((id) => `"${id}"`).join(",");
// Filter for only tests that transitioned to flaky
if (new_status !== "FLAKY") {
webhook.payload = "canceled";
webhook.cancel = true;
return webhook;
}
const description = summarizeTestCase(webhook.payload);
// modify the webhook object...
webhook.payload = {query: `mutation IssueCreate {
issueCreate(
input: {
title: "Flaky Test: ${webhook.payload.test_case.name}"
description: """${description}"""
teamId: "${LINEAR_TEAM_ID}"
projectId: ${resolvedProjectId}
labelIds: [${resolvedLinearLabels}]
}
) {
success
issue {
id
title
}
}
} ` };
return webhook;
}
function summarizeTestCase(payload) {
const {
previous_status,
new_status,
timestamp,
repository,
test_case: {
name,
file_path,
quarantined,
codeowners,
html_url
}
} = payload;
// Construct a comprehensive issue body with key details
const issueBody = `See all details on the [Trunk Test Detail page](${html_url})
Transition: ${previous_status} → ${new_status}
Transition time: ${timestamp}
File path: ${file_path || 'N/A'}
Quarantined: ${quarantined ? 'Yes' : 'No'}
Ownership: this test is owned by ${(codeowners && codeowners.length ? codeowners : ['@unassigned']).join(', ')}
Repository: ${repository.html_url}
View the full stack trace on the [Test Detail page](${html_url})
`
return issueBody
}If you have CODEOWNERS configured in your repo, it will be reported by Trunk in the webhook payload. You can use this to map different CODEOWNERS to Linear assignees. You can access CODEOWNERS in the payload like this: webhook.payload.test_case.codeowners.
Since the way your owners map to your Linear user is unique to your team, you'll need to provide your own mapping to convert code owners to their Linear ID.
You can modify your issue create payload like this to include an assignee:
webhook.payload = {query: `mutation IssueCreate {
issueCreate(
input: {
title: "Flaky Test: ${webhook.payload.test_case.name}"
description: """${description}"""
teamId: "<YOUR_TEAM_ID>"
projectId: "<YOUR_PROJECT_ID>"
labelIds: ["<YOUR_LABEL_ID>"]
// Add you assignee here:
assigneeId: "<ASSIGNEE_ID>"
}
) {
success
issue {
id
title
}
}
} ` };You can create test issues by delivering a mock webhook. You can do this by:
- In the endpoint configuration view, navigate to the Testing tab and select a Send event
- Under Subscribed events, select
v2.test_case.status_changedas the event type to send - Click Send Example to test your webhook
You can monitor the events and the webhook's delivery logs in the Overview tab of an endpoint configuration view.
You can see an overview of how many webhook deliveries have been attempted, how many are successful, how many are in flight, and how many fail in the Attempt Delivery Status modal.
You can see a list of past delivery attempts in the Message Attempts modal. You can filter this list by Succeeded and Failed status, and you can click on each message to see the Message content, response code, and error message of each attempt. You can learn more about replaying messages and filtering logs in the Svix docs.
A Linear Issue will now be created when a test's health status changes to flaky. You can further modify your transformation script to customize your issues.
See the Trunk webhook event catalog

