|
1 | 1 | # Linear Webhooks Example |
2 | 2 |
|
3 | | -This example demonstrates how to set up a webhook handler for Linear events using the Codegen SDK and Modal. The webhook handler can process events from Linear such as issue creation, updates, comments, and more. |
| 3 | +This example demonstrates how to handle Linear webhook events using the Codegen SDK and Modal. It provides a simple webhook handler that processes different types of Linear events, such as Issue and Comment events. |
| 4 | + |
| 5 | +## Features |
| 6 | + |
| 7 | +- Receive and process Linear webhook events |
| 8 | +- Handle specific event types (Issues, Comments) |
| 9 | +- Deploy as a serverless application using Modal |
| 10 | +- Secure webhook verification using Linear's signing secret |
4 | 11 |
|
5 | 12 | ## Prerequisites |
6 | 13 |
|
7 | | -- [Modal](https://modal.com/) account |
8 | | -- [Linear](https://linear.app/) account with admin access |
9 | 14 | - Python 3.13 or higher |
| 15 | +- A Linear account with admin access |
| 16 | +- A Modal account (for deployment) |
| 17 | +- Codegen SDK (version 0.26.3 or higher) |
10 | 18 |
|
11 | 19 | ## Setup |
12 | 20 |
|
13 | | -### 1. Install Dependencies |
| 21 | +### 1. Clone the Repository |
14 | 22 |
|
15 | 23 | ```bash |
16 | | -# Clone the repository |
17 | | -git clone https://github.com/Zeeeepa/codegen.git |
18 | | -cd codegen/codegen-examples/examples/linear_webhooks |
19 | | - |
20 | | -# Install dependencies |
21 | | -pip install -e . |
| 24 | +git clone https://github.com/your-username/your-repo.git |
| 25 | +cd your-repo/examples/linear_webhooks |
22 | 26 | ``` |
23 | 27 |
|
24 | | -### 2. Configure Environment Variables |
25 | | - |
26 | | -Copy the `.env.template` file to `.env` and fill in your credentials: |
| 28 | +### 2. Create a Virtual Environment |
27 | 29 |
|
28 | 30 | ```bash |
29 | | -cp .env.template .env |
| 31 | +python -m venv venv |
| 32 | +source venv/bin/activate # On Windows: venv\Scripts\activate |
30 | 33 | ``` |
31 | 34 |
|
32 | | -Edit the `.env` file with your Linear API credentials: |
| 35 | +### 3. Install Dependencies |
33 | 36 |
|
34 | | -``` |
35 | | -LINEAR_ACCESS_TOKEN="your_linear_api_token" |
36 | | -LINEAR_SIGNING_SECRET="your_linear_webhook_signing_secret" |
37 | | -LINEAR_TEAM_ID="your_linear_team_id" |
38 | | -MODAL_API_KEY="your_modal_api_key" # Optional |
| 37 | +```bash |
| 38 | +pip install -e ".[dev]" |
39 | 39 | ``` |
40 | 40 |
|
41 | | -To get these credentials: |
| 41 | +### 4. Set Up Environment Variables |
42 | 42 |
|
43 | | -- **LINEAR_ACCESS_TOKEN**: Go to Linear → Settings → API → Create Key |
44 | | -- **LINEAR_SIGNING_SECRET**: Created when you set up a webhook in Linear |
45 | | -- **LINEAR_TEAM_ID**: Found in Linear team settings or via the API |
46 | | -- **MODAL_API_KEY**: Your Modal API key (if not using Modal CLI authentication) |
| 43 | +Create a `.env` file in the project root with the following variables: |
47 | 44 |
|
48 | | -### 3. Authenticate with Modal |
49 | | - |
50 | | -```bash |
51 | | -modal token new |
| 45 | +``` |
| 46 | +LINEAR_API_KEY=your_linear_api_key |
| 47 | +LINEAR_SIGNING_SECRET=your_linear_webhook_signing_secret |
| 48 | +MODAL_API_KEY=your_modal_api_key |
52 | 49 | ``` |
53 | 50 |
|
54 | | -## Deployment Commands |
| 51 | +You can get your Linear API key from the Linear dashboard under Settings > API > Personal API Keys. |
55 | 52 |
|
56 | | -### Deploy to Modal |
| 53 | +## Local Development |
| 54 | + |
| 55 | +For local development and testing, you can run the webhook handler locally: |
57 | 56 |
|
58 | 57 | ```bash |
59 | | -# Deploy the webhook handler to Modal |
60 | 58 | python webhooks.py |
61 | 59 | ``` |
62 | 60 |
|
63 | | -This will deploy the webhook handler to Modal and provide you with a URL that you can use to configure the webhook in Linear. |
| 61 | +This will start a local server that you can use for testing. However, for Linear to send webhooks to your local machine, you'll need to use a tool like ngrok to expose your local server to the internet. |
64 | 62 |
|
65 | | -### Get Deployment Status |
| 63 | +## Deployment |
66 | 64 |
|
67 | | -```bash |
68 | | -# Check the status of your Modal deployment |
69 | | -modal app status linear-webhooks |
70 | | -``` |
| 65 | +### Deploy to Modal |
71 | 66 |
|
72 | | -### View Logs |
| 67 | +To deploy the webhook handler to Modal, run: |
73 | 68 |
|
74 | 69 | ```bash |
75 | | -# View logs from your Modal deployment |
76 | | -modal app logs linear-webhooks |
| 70 | +modal deploy webhooks.py |
77 | 71 | ``` |
78 | 72 |
|
79 | | -### Update Deployment |
| 73 | +This will deploy the webhook handler to Modal and provide you with a URL that you can use to configure the Linear webhook. |
| 74 | + |
| 75 | +### Update the Deployment |
| 76 | + |
| 77 | +To update an existing deployment: |
80 | 78 |
|
81 | 79 | ```bash |
82 | | -# Update your Modal deployment after making changes |
83 | | -python webhooks.py |
| 80 | +modal deploy webhooks.py |
84 | 81 | ``` |
85 | 82 |
|
86 | | -### Stop Deployment |
| 83 | +### Stop the Deployment |
| 84 | + |
| 85 | +To stop the deployment: |
87 | 86 |
|
88 | 87 | ```bash |
89 | | -# Stop your Modal deployment |
90 | 88 | modal app stop linear-webhooks |
91 | 89 | ``` |
92 | 90 |
|
93 | 91 | ## Configuring Linear Webhooks |
94 | 92 |
|
95 | 93 | 1. Go to Linear → Settings → API → Webhooks |
96 | | -2. Click "New Webhook" |
97 | | -3. Enter the URL provided by Modal when you deployed the webhook handler |
98 | | -4. Select the events you want to receive (e.g., Issues, Comments) |
99 | | -5. Copy the signing secret and add it to your `.env` file as `LINEAR_SIGNING_SECRET` |
100 | | -6. Click "Create Webhook" |
| 94 | +1. Click "New Webhook" |
| 95 | +1. Enter the URL provided by Modal when you deployed the webhook handler |
| 96 | +1. Select the events you want to receive (e.g., Issues, Comments) |
| 97 | +1. Copy the signing secret and add it to your `.env` file as `LINEAR_SIGNING_SECRET` |
| 98 | +1. Click "Create Webhook" |
101 | 99 |
|
102 | 100 | ## Customizing Event Handlers |
103 | 101 |
|
104 | | -The example includes handlers for Issue and Comment events. You can customize these handlers or add new ones by modifying the `webhooks.py` file: |
| 102 | +You can customize the event handlers in `webhooks.py` to handle different types of Linear events. The example includes handlers for Issue and Comment events, but you can add handlers for other event types as well. |
| 103 | + |
| 104 | +To add a new event handler, add a new method to the `LinearEventHandlers` class and decorate it with `@app.linear.event("EventType")`: |
105 | 105 |
|
106 | 106 | ```python |
107 | | -@modal.web_endpoint(method="POST") |
108 | | -@app.linear.event("YourEventType") |
109 | | -def handle_your_event(self, event: LinearEvent): |
| 107 | +@app.linear.event("Project") |
| 108 | +def handle_project(self, event: LinearEvent): |
| 109 | + """Handle Linear Project events""" |
| 110 | + logger.info(f"Received Linear Project event: {event.action}") |
110 | 111 | # Process the event |
111 | | - return {"status": "success"} |
| 112 | + return {"status": "success", "message": f"Processed Linear Project event: {event.action}"} |
112 | 113 | ``` |
113 | 114 |
|
114 | | -## Available Event Types |
115 | | - |
116 | | -Linear supports the following event types: |
117 | | - |
118 | | -- `Issue` |
119 | | -- `Comment` |
120 | | -- `Project` |
121 | | -- `Cycle` |
122 | | -- `Reaction` |
123 | | -- And more... |
| 115 | +## Event Types |
124 | 116 |
|
125 | | -Refer to the [Linear API documentation](https://developers.linear.app/docs/graphql/webhooks) for a complete list of event types. |
| 117 | +Linear supports various event types, including: |
126 | 118 |
|
127 | | -## Troubleshooting |
| 119 | +- Issue |
| 120 | +- Comment |
| 121 | +- Project |
| 122 | +- Cycle |
| 123 | +- Team |
| 124 | +- User |
| 125 | +- Attachment |
| 126 | +- Reaction |
| 127 | +- Label |
| 128 | +- State |
128 | 129 |
|
129 | | -- **Webhook not receiving events**: Verify that your Linear webhook is configured correctly and that the URL is accessible. |
130 | | -- **Authentication errors**: Check that your LINEAR_ACCESS_TOKEN and LINEAR_SIGNING_SECRET are correct. |
131 | | -- **Modal deployment issues**: Run `modal app logs linear-webhooks` to view logs and diagnose issues. |
| 130 | +Refer to the [Linear API documentation](https://developers.linear.app/docs/graphql/webhooks) for a complete list of event types and their payloads. |
132 | 131 |
|
133 | | -## Additional Resources |
| 132 | +## Resources |
134 | 133 |
|
135 | 134 | - [Codegen Documentation](https://docs.codegen.sh/) |
136 | 135 | - [Modal Documentation](https://modal.com/docs) |
|
0 commit comments