|
| 1 | +"""Receive ClickUp webhooks in Flyte, and see one arrive without leaving your laptop. |
| 2 | +
|
| 3 | +Two ways to run this. The second needs no ClickUp account at all: |
| 4 | +
|
| 5 | + python clickup_webhooks.py --local # replay a real sample delivery in-process |
| 6 | + python clickup_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` — a `taskCreated` 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 ClickUp at `<app-url>/webhook/clickup` |
| 14 | +from Space Settings -> Integrations -> Webhooks. |
| 15 | +
|
| 16 | +Setup for the real thing: |
| 17 | + flyte create secret CLICKUP_WEBHOOK_SECRET --value <secret> |
| 18 | +
|
| 19 | +ClickUp 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.clickup import DEFAULT_SECRET_ENV, SAMPLE_DELIVERY, ClickUpProvider, events |
| 29 | + |
| 30 | +image = flyte.Image.from_debian_base(python_version=(3, 12)).with_pip_packages("flyteplugins-clickup[app]") |
| 31 | + |
| 32 | +app_env = WebhookAppEnvironment( |
| 33 | + name="clickup-webhooks", |
| 34 | + providers=[ClickUpProvider()], |
| 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.Task.CREATED) |
| 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.Task.STATUS_UPDATED) |
| 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 `clickup-tickets.triage_task` deployed first |
| 67 | + and a Flyte backend to launch into. Wire it up with: |
| 68 | +
|
| 69 | + @app_env.on_event(events.Task.CREATED) |
| 70 | +
|
| 71 | + `idempotent_run` refuses to launch when a run carrying the same dedupe key |
| 72 | + is already live or has succeeded, so ClickUp 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="clickup-tickets.triage_task", 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(), task_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/clickup (signed with a throwaway secret)") |
| 97 | + response = client.post("/webhook/clickup", 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/clickup", 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/clickup", 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 ClickUp at {handle.endpoint}/webhook/clickup") |
0 commit comments