|
| 1 | +"""Receive Jira webhooks in Flyte, and see one arrive without leaving your laptop. |
| 2 | +
|
| 3 | +Two ways to run this. The second needs no Jira account at all: |
| 4 | +
|
| 5 | + python jira_webhooks.py --local # replay a real sample delivery in-process |
| 6 | + python jira_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 `jira:issue_created` 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 Jira at `<app-url>/webhook/jira` |
| 14 | +from Jira Settings -> System -> Webhooks. |
| 15 | +
|
| 16 | +Setup for the real thing: |
| 17 | + flyte create secret JIRA_WEBHOOK_TOKEN --value <secret> |
| 18 | +
|
| 19 | +Jira does not sign webhooks, so this is a token you invent. Something in front |
| 20 | +of the app has to inject it as `X-Webhook-Token`, since Jira cannot send custom |
| 21 | +headers itself. |
| 22 | +""" |
| 23 | + |
| 24 | +import os |
| 25 | +import sys |
| 26 | + |
| 27 | +import flyte |
| 28 | +from flyte.extras.webhooks import WebhookAppEnvironment |
| 29 | + |
| 30 | +from flyteplugins.jira import DEFAULT_SECRET_ENV, SAMPLE_DELIVERY, JiraProvider, events |
| 31 | + |
| 32 | +image = flyte.Image.from_debian_base(python_version=(3, 12)).with_pip_packages("flyteplugins-jira[app]") |
| 33 | + |
| 34 | +app_env = WebhookAppEnvironment( |
| 35 | + name="jira-webhooks", |
| 36 | + providers=[JiraProvider()], |
| 37 | + image=image, |
| 38 | + secrets=[flyte.Secret(DEFAULT_SECRET_ENV, as_env_var=DEFAULT_SECRET_ENV)], |
| 39 | +) |
| 40 | + |
| 41 | + |
| 42 | +@app_env.on_event(events.Issue.CREATED) |
| 43 | +async def on_primary(event): |
| 44 | + """React to the event this plugin's sample delivery carries. |
| 45 | +
|
| 46 | + Returning a dict is enough to see the path working. To do real work, launch |
| 47 | + a deployed task instead — see `launch_a_task` below. |
| 48 | + """ |
| 49 | + return { |
| 50 | + "saw": event.qualified_type, |
| 51 | + "resource": event.resource_id, |
| 52 | + "title": event.title, |
| 53 | + # The key `idempotent_run` would dedupe on. Replaying the same delivery |
| 54 | + # produces the same key, which is what makes a redelivery a no-op. |
| 55 | + "dedupe_key": event.dedupe_key(), |
| 56 | + } |
| 57 | + |
| 58 | + |
| 59 | +@app_env.on_event(events.Comment.CREATED) |
| 60 | +async def on_secondary(event): |
| 61 | + """A second handler, to show dispatch picking the right one per event.""" |
| 62 | + return {"saw": event.qualified_type, "resource": event.resource_id} |
| 63 | + |
| 64 | + |
| 65 | +async def launch_a_task(event): |
| 66 | + """What a handler looks like once it does real work. |
| 67 | +
|
| 68 | + Not registered above, because it needs `jira-tickets.triage_issue` deployed first |
| 69 | + and a Flyte backend to launch into. Wire it up with: |
| 70 | +
|
| 71 | + @app_env.on_event(events.Issue.CREATED) |
| 72 | +
|
| 73 | + `idempotent_run` refuses to launch when a run carrying the same dedupe key |
| 74 | + is already live or has succeeded, so Jira redelivering an event — which |
| 75 | + it does on any non-2xx — never starts a second run. |
| 76 | + """ |
| 77 | + import flyte.remote as remote |
| 78 | + from flyte.extras.webhooks import DuplicateRun, idempotent_run |
| 79 | + |
| 80 | + task = remote.Task.get(name="jira-tickets.triage_issue", auto_version="latest") |
| 81 | + try: |
| 82 | + # Always `.aio`: the blocking form stalls the app's event loop, and |
| 83 | + # webhook senders time deliveries out in seconds. |
| 84 | + run = await idempotent_run.aio(task, key=event.dedupe_key(), issue_key=event.resource_id) |
| 85 | + except DuplicateRun as exc: |
| 86 | + return {"skipped": str(exc)} |
| 87 | + return {"run": run.name} |
| 88 | + |
| 89 | + |
| 90 | +def _try_locally() -> None: |
| 91 | + """Post this plugin's sample delivery to the app, in-process.""" |
| 92 | + from fastapi.testclient import TestClient |
| 93 | + |
| 94 | + secret = os.environ.setdefault(DEFAULT_SECRET_ENV, "local-trial-secret") |
| 95 | + build_headers, body = SAMPLE_DELIVERY |
| 96 | + client = TestClient(app_env.app) |
| 97 | + |
| 98 | + print("POST /webhook/jira (signed with a throwaway secret)") |
| 99 | + response = client.post("/webhook/jira", content=body, headers=build_headers(body, secret)) |
| 100 | + print(f" {response.status_code} {response.json()}\n") |
| 101 | + |
| 102 | + print("the same delivery again — note the identical dedupe_key:") |
| 103 | + again = client.post("/webhook/jira", content=body, headers=build_headers(body, secret)) |
| 104 | + print(f" {again.status_code} {again.json()}\n") |
| 105 | + |
| 106 | + print("an unsigned delivery is refused:") |
| 107 | + bad = client.post("/webhook/jira", content=body, headers={}) |
| 108 | + print(f" {bad.status_code} {bad.json()}\n") |
| 109 | + |
| 110 | + print("normalized events the app has seen:") |
| 111 | + for seen in client.get("/api/events").json(): |
| 112 | + print(f" {seen['provider']} {seen['qualified_type']} resource={seen['resource_id']}") |
| 113 | + |
| 114 | + |
| 115 | +if __name__ == "__main__": |
| 116 | + if "--local" in sys.argv: |
| 117 | + _try_locally() |
| 118 | + else: |
| 119 | + flyte.init_from_config() |
| 120 | + handle = flyte.serve(app_env) |
| 121 | + handle.activate(wait=True) |
| 122 | + print(f"Dashboard ready at {handle.endpoint}") |
| 123 | + print(f"Point Jira at {handle.endpoint}/webhook/jira") |
0 commit comments