Skip to content

Commit 57fc28f

Browse files
cosmicBboyclaude
andcommitted
feat(linear): add flyteplugins-linear
Receive Linear webhooks in Flyte. Exports LinearProvider, a Provider subclass with its defaults pre-wired, so wiring it up reads: WebhookAppEnvironment(providers=[LinearProvider()]) The receiver itself ships with flyte, at flyte.extras.webhooks; this package contributes only what is specific to Linear -- which environment variable holds the secret, how to verify a delivery, how to parse one into a WebhookEvent, and typed constants for every event Linear sends. examples/linear_webhooks.py runs with no Linear account at all: --local replays this plugin's own SAMPLE_DELIVERY through the app, so you can watch a delivery be verified, normalized, and dispatched before wiring anything up. The shared conformance check exercises that same sample. The package is named for the product rather than for webhooks, so client methods can land here later when they earn their place -- returning flyte.io.File instead of an inline megabyte payload, rendering into the task report, or participating in caching and fan-out. Plain API passthrough belongs in the vendor's own SDK, called directly from a task. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01VKZrTNjjWVzTZUxDFbn4Nk Signed-off-by: Niels Bantilan <niels.bantilan@gmail.com>
1 parent fbde022 commit 57fc28f

9 files changed

Lines changed: 2020 additions & 0 deletions

File tree

plugins/linear/README.md

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
# flyteplugins-linear
2+
3+
Receive Linear webhooks in Flyte.
4+
5+
```bash
6+
pip install "flyteplugins-linear[app]"
7+
```
8+
9+
## Using it
10+
11+
Hand a `LinearProvider()` to a `WebhookAppEnvironment` and register handlers with the
12+
typed constants in `events`:
13+
14+
```python
15+
import flyte
16+
from flyte.extras.webhooks import DuplicateRun, WebhookAppEnvironment, idempotent_run
17+
from flyteplugins.linear import LinearProvider, events
18+
19+
app_env = WebhookAppEnvironment(
20+
name="linear-webhooks",
21+
providers=[LinearProvider()],
22+
secrets=[flyte.Secret("LINEAR_WEBHOOK_SECRET", as_env_var="LINEAR_WEBHOOK_SECRET")],
23+
)
24+
25+
26+
@app_env.on_event(events.Issue.CREATE)
27+
async def handle(event):
28+
import flyte.remote as remote
29+
30+
task = remote.Task.get(name="my-env.my_task", auto_version="latest")
31+
try:
32+
run = await idempotent_run.aio(task, key=event.dedupe_key(), resource=event.resource_id)
33+
except DuplicateRun as exc:
34+
return {"skipped": str(exc)}
35+
return {"run": run.name}
36+
37+
38+
flyte.serve(app_env)
39+
```
40+
41+
Handlers must `await idempotent_run.aio(...)`. The blocking form stalls the
42+
app's event loop, and Linear times deliveries out in seconds.
43+
44+
One app can serve several products at once — hand it one provider per product.
45+
46+
## Try it
47+
48+
`examples/linear_webhooks.py` runs two ways. The first needs no Linear account:
49+
50+
```bash
51+
python examples/linear_webhooks.py --local # replay a real sample delivery in-process
52+
python examples/linear_webhooks.py # deploy the receiver to Flyte
53+
```
54+
55+
`--local` posts this plugin's `SAMPLE_DELIVERY` through the app with FastAPI's
56+
test client, so you see a delivery verified, normalized, and dispatched — plus
57+
an unsigned one refused with a 401, and the same delivery replayed to show the
58+
dedupe key is stable.
59+
60+
## Setup
61+
62+
1. Store the secret and mount it on the app:
63+
```bash
64+
flyte create secret LINEAR_WEBHOOK_SECRET --value <secret>
65+
```
66+
2. Point Linear at `<app-url>/webhook/linear`, from
67+
Linear Settings → API → Webhooks (it shows the signing secret on creation).
68+
69+
**Verification:** HMAC-SHA256 over the raw body (`X-Linear-Signature`).
70+
71+
Comment and reaction payloads carry the team id only on the nested issue; the parser follows it, so a `scopes` allowlist can still attribute them.
72+
73+
## Event constants
74+
75+
`events` spells every event this plugin can dispatch, as `str` enums grouped by
76+
event type, so a typo fails at import rather than by silently never matching.
77+
Raw strings still work, for events the constants do not cover yet.
78+
79+
## What this plugin does not do
80+
81+
Call the Linear API. Use `gql` — Linear ships no Python SDK, and its API is a single GraphQL endpoint directly from your tasks — see
82+
`examples/external_saas_integrations`. This plugin owns only the part that is
83+
Flyte's: authenticating an inbound delivery and turning it into a run.
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
"""Receive Linear webhooks in Flyte, and see one arrive without leaving your laptop.
2+
3+
Two ways to run this. The second needs no Linear account at all:
4+
5+
python linear_webhooks.py --local # replay a real sample delivery in-process
6+
python linear_webhooks.py # deploy the receiver to Flyte
7+
8+
`--local` runs the app through FastAPI's test client and posts this plugin's
9+
`SAMPLE_DELIVERY` — an `Issue.create` delivery — signed with a throwaway secret. You see the
10+
delivery verified, normalized, and dispatched to a handler, which is the whole
11+
path a real webhook takes.
12+
13+
To receive real events, deploy it and point Linear at `<app-url>/webhook/linear`
14+
from Linear Settings -> API -> Webhooks.
15+
16+
Setup for the real thing:
17+
flyte create secret LINEAR_WEBHOOK_SECRET --value <secret>
18+
19+
Linear shows the signing secret once, when you create the webhook.
20+
"""
21+
22+
import os
23+
import sys
24+
25+
import flyte
26+
from flyte.extras.webhooks import WebhookAppEnvironment
27+
28+
from flyteplugins.linear import DEFAULT_SECRET_ENV, SAMPLE_DELIVERY, LinearProvider, events
29+
30+
image = flyte.Image.from_debian_base(python_version=(3, 12)).with_pip_packages("flyteplugins-linear[app]")
31+
32+
app_env = WebhookAppEnvironment(
33+
name="linear-webhooks",
34+
providers=[LinearProvider()],
35+
image=image,
36+
secrets=[flyte.Secret(DEFAULT_SECRET_ENV, as_env_var=DEFAULT_SECRET_ENV)],
37+
)
38+
39+
40+
@app_env.on_event(events.Issue.CREATE)
41+
async def on_primary(event):
42+
"""React to the event this plugin's sample delivery carries.
43+
44+
Returning a dict is enough to see the path working. To do real work, launch
45+
a deployed task instead — see `launch_a_task` below.
46+
"""
47+
return {
48+
"saw": event.qualified_type,
49+
"resource": event.resource_id,
50+
"title": event.title,
51+
# The key `idempotent_run` would dedupe on. Replaying the same delivery
52+
# produces the same key, which is what makes a redelivery a no-op.
53+
"dedupe_key": event.dedupe_key(),
54+
}
55+
56+
57+
@app_env.on_event(events.Issue.UPDATE)
58+
async def on_secondary(event):
59+
"""A second handler, to show dispatch picking the right one per event."""
60+
return {"saw": event.qualified_type, "resource": event.resource_id}
61+
62+
63+
async def launch_a_task(event):
64+
"""What a handler looks like once it does real work.
65+
66+
Not registered above, because it needs `linear-triage.triage_issue` deployed first
67+
and a Flyte backend to launch into. Wire it up with:
68+
69+
@app_env.on_event(events.Issue.CREATE)
70+
71+
`idempotent_run` refuses to launch when a run carrying the same dedupe key
72+
is already live or has succeeded, so Linear redelivering an event — which
73+
it does on any non-2xx — never starts a second run.
74+
"""
75+
import flyte.remote as remote
76+
from flyte.extras.webhooks import DuplicateRun, idempotent_run
77+
78+
task = remote.Task.get(name="linear-triage.triage_issue", auto_version="latest")
79+
try:
80+
# Always `.aio`: the blocking form stalls the app's event loop, and
81+
# webhook senders time deliveries out in seconds.
82+
run = await idempotent_run.aio(task, key=event.dedupe_key(), issue_id=event.resource_id)
83+
except DuplicateRun as exc:
84+
return {"skipped": str(exc)}
85+
return {"run": run.name}
86+
87+
88+
def _try_locally() -> None:
89+
"""Post this plugin's sample delivery to the app, in-process."""
90+
from fastapi.testclient import TestClient
91+
92+
secret = os.environ.setdefault(DEFAULT_SECRET_ENV, "local-trial-secret")
93+
build_headers, body = SAMPLE_DELIVERY
94+
client = TestClient(app_env.app)
95+
96+
print("POST /webhook/linear (signed with a throwaway secret)")
97+
response = client.post("/webhook/linear", content=body, headers=build_headers(body, secret))
98+
print(f" {response.status_code} {response.json()}\n")
99+
100+
print("the same delivery again — note the identical dedupe_key:")
101+
again = client.post("/webhook/linear", content=body, headers=build_headers(body, secret))
102+
print(f" {again.status_code} {again.json()}\n")
103+
104+
print("an unsigned delivery is refused:")
105+
bad = client.post("/webhook/linear", content=body, headers={})
106+
print(f" {bad.status_code} {bad.json()}\n")
107+
108+
print("normalized events the app has seen:")
109+
for seen in client.get("/api/events").json():
110+
print(f" {seen['provider']} {seen['qualified_type']} resource={seen['resource_id']}")
111+
112+
113+
if __name__ == "__main__":
114+
if "--local" in sys.argv:
115+
_try_locally()
116+
else:
117+
flyte.init_from_config()
118+
handle = flyte.serve(app_env)
119+
handle.activate(wait=True)
120+
print(f"Dashboard ready at {handle.endpoint}")
121+
print(f"Point Linear at {handle.endpoint}/webhook/linear")

plugins/linear/pyproject.toml

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
[project]
2+
name = "flyteplugins-linear"
3+
dynamic = ["version"]
4+
description = "Receive Linear webhooks in Flyte."
5+
readme = "README.md"
6+
authors = [{ name = "Flyte Contributors" }]
7+
requires-python = ">=3.10"
8+
# The webhook receiver lives in flyte itself, at flyte.extras.webhooks.
9+
dependencies = ["flyte"]
10+
11+
[project.optional-dependencies]
12+
app = ["fastapi>=0.115", "uvicorn>=0.30"]
13+
14+
[build-system]
15+
requires = ["setuptools", "setuptools_scm"]
16+
build-backend = "setuptools.build_meta"
17+
18+
[dependency-groups]
19+
dev = [
20+
"pytest>=8.3.5",
21+
"pytest-asyncio>=0.26.0",
22+
"fastapi>=0.115",
23+
"uvicorn>=0.30",
24+
"httpx>=0.27",
25+
]
26+
27+
[tool.setuptools]
28+
include-package-data = true
29+
30+
[tool.setuptools.packages.find]
31+
where = ["src"]
32+
include = ["flyteplugins*"]
33+
34+
[tool.setuptools_scm]
35+
root = "../../"
36+
37+
[tool.pytest.ini_options]
38+
norecursedirs = []
39+
log_cli = true
40+
log_cli_level = 20
41+
markers = []
42+
asyncio_mode = "auto"
43+
asyncio_default_fixture_loop_scope = "function"
44+
45+
[tool.coverage.run]
46+
branch = true
47+
48+
[tool.ruff]
49+
line-length = 120
50+
51+
[tool.ruff.lint]
52+
select = ["E", "W", "F", "I", "PLW", "YTT", "ASYNC", "C4", "T10", "EXE", "ISC", "LOG", "PIE", "Q", "RSE", "FLY", "PGH", "PLC", "PLE", "FURB", "RUF"]
53+
ignore = ["PGH003", "PLC0415"]
54+
55+
[tool.ruff.lint.per-file-ignores]
56+
"examples/*" = ["E402"]
57+
58+
[tool.uv.sources]
59+
flyte = { path = "../../", editable = true }
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
"""Linear webhooks for Flyte.
2+
3+
Hand a `LinearProvider()` to a `WebhookAppEnvironment` and register handlers with the
4+
typed constants in `events`. Calling the Linear API is not this plugin's job —
5+
Linear's API is a single GraphQL endpoint, so use `gql` from your tasks. See
6+
`examples/external_saas_integrations`.
7+
"""
8+
9+
import hashlib
10+
import hmac
11+
12+
from . import events
13+
from ._provider import DEFAULT_SECRET_ENV, LinearProvider, parse, verify
14+
15+
__all__ = ["DEFAULT_SECRET_ENV", "SAMPLE_DELIVERY", "LinearProvider", "events", "parse", "verify"]
16+
17+
18+
def _sample_headers(body: bytes, secret: str) -> dict[str, str]:
19+
return {"X-Linear-Signature": hmac.new(secret.encode(), body, hashlib.sha256).hexdigest()}
20+
21+
22+
#: A real `Issue.create` delivery, trimmed to the fields the parser reads.
23+
SAMPLE_DELIVERY = (
24+
_sample_headers,
25+
(
26+
b'{"action": "create", "type": "Issue", "webhookId": "wh-000",'
27+
b' "createdAt": "2024-01-01T00:00:00.000Z",'
28+
b' "data": {"id": "00000000-0000-0000-0000-000000000000", "title": "A bug",'
29+
b' "teamId": "team-000", "updatedAt": "2024-01-01T00:00:00.000Z",'
30+
b' "url": "https://linear.app/acme/issue/ENG-1"}}'
31+
),
32+
)
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
"""Linear webhook verification and payload normalization."""
2+
3+
from __future__ import annotations
4+
5+
from typing import Any, Mapping
6+
7+
from flyte.extras.webhooks import (
8+
Provider,
9+
WebhookEvent,
10+
constant_time_equals,
11+
hex_hmac_sha256,
12+
json_body,
13+
lower_headers,
14+
)
15+
16+
#: Environment variable this provider reads its secret from by default.
17+
DEFAULT_SECRET_ENV = "LINEAR_WEBHOOK_SECRET"
18+
19+
20+
def verify(body: bytes, headers: Mapping[str, str], secret: str) -> bool:
21+
"""Verify the `X-Linear-Signature` HMAC over the raw body."""
22+
signature = lower_headers(headers).get("x-linear-signature")
23+
if not signature:
24+
return False
25+
return constant_time_equals(hex_hmac_sha256(secret, body), signature.strip())
26+
27+
28+
def _team_id(data: dict[str, Any]) -> str | None:
29+
"""Find the team id, which Comment and Reaction payloads nest on the issue.
30+
31+
Without these fallbacks a `scopes` allowlist drops every non-Issue event as
32+
unattributable.
33+
"""
34+
issue = data.get("issue") or {}
35+
for candidate in (
36+
data.get("teamId"),
37+
(data.get("team") or {}).get("id"),
38+
issue.get("teamId"),
39+
(issue.get("team") or {}).get("id"),
40+
):
41+
if candidate:
42+
return str(candidate)
43+
return None
44+
45+
46+
def parse(headers: Mapping[str, str], body: bytes) -> WebhookEvent:
47+
"""Normalize a Linear delivery into a `WebhookEvent`."""
48+
payload = json_body(body)
49+
data = payload.get("data") or {}
50+
return WebhookEvent(
51+
provider="linear",
52+
event_type=payload.get("type", "Unknown"),
53+
action=payload.get("action", "unknown"),
54+
delivery_id=str(payload.get("webhookId") or ""),
55+
resource_id=data.get("id"),
56+
# `updatedAt` is on the entity; `createdAt` is the delivery time and the
57+
# only timestamp on payloads whose entity carries none.
58+
occurred_at=data.get("updatedAt") or payload.get("createdAt"),
59+
scope=_team_id(data),
60+
title=data.get("title"),
61+
url=data.get("url") or payload.get("url"),
62+
actor=(data.get("creator") or {}).get("name"),
63+
payload=payload,
64+
)
65+
66+
67+
class LinearProvider(Provider):
68+
"""Linear's webhook provider, with its defaults pre-wired.
69+
70+
```python
71+
from flyte.extras.webhooks import WebhookAppEnvironment
72+
from flyteplugins.linear import LinearProvider
73+
74+
app_env = WebhookAppEnvironment(name="webhooks", providers=[LinearProvider()])
75+
```
76+
77+
Args:
78+
secret_env: Environment variable holding the secret, mounted from a
79+
`flyte.Secret`. Override only if you store it under a non-standard
80+
name; the default is what the docs and examples assume.
81+
"""
82+
83+
def __init__(self, *, secret_env: str = DEFAULT_SECRET_ENV) -> None:
84+
super().__init__(
85+
name="linear",
86+
secret_env=secret_env,
87+
verify=verify,
88+
parse=parse,
89+
setup_hint="Linear Settings -> API -> Webhooks",
90+
)

0 commit comments

Comments
 (0)