apiVersion: argoproj.io/v1alpha1
kind: WorkflowTemplate
metadata:
  name: pagerduty-trigger
  labels:
    dagweave.io/catalog-item: pagerduty-trigger
  annotations:
    # The catalog semver this manifest was authored at. The dagweave app records
    # (catalog-item-id, semver) -> content-hash -> base-vN when it mints the tenant
    # cluster name via ResolveOrMint. Keep in sync with metadata.yaml `version`.
    dagweave.io/catalog-version: "1.0.0"
    dagweave.io/catalog-category: "Notifications"
spec:
  entrypoint: trigger
  templates:
    - name: trigger
      inputs:
        parameters:
          # Human-readable summary of the alert (the incident title).
          - name: summary
          # Perceived severity: one of critical, error, warning, info.
          - name: severity
            default: critical
          # The affected system, component or service the alert originates from.
          - name: source
          # Name/key of the Kubernetes Secret in the run namespace holding the
          # Events API v2 integration routing key. Defaults let a user leave them
          # blank in the Inspector.
          - name: routing-key-secret-name
            default: pagerduty-routing-key
          - name: routing-key-secret-key
            default: routing-key
      container:
        # Pinned by digest per the catalog convention: an image rebuild can never
        # silently change the behaviour of an already-released item version.
        # python:3.15-alpine — the Events API v2 expects a JSON body, so the payload
        # is built with json.dumps (which correctly escapes any quotes, newlines or
        # control characters) rather than shell string assembly. urllib performs the
        # POST, so no extra image is needed.
        image: python@sha256:20a82dbc2783112ddb91d1534603f802d222a3e63e593cd002eb2ac534e70211
        command: [python3, -c]
        # Values arrive as env vars (below), never interpolated into the program
        # source, so a summary/source with any content is safe.
        args:
          - |
            import json, os, urllib.request
            body = {
                "routing_key": os.environ["ROUTING_KEY"],
                "event_action": "trigger",
                "payload": {
                    "summary": os.environ["SUMMARY"],
                    "severity": os.environ["SEVERITY"],
                    "source": os.environ["SOURCE"],
                },
            }
            req = urllib.request.Request(
                "https://events.pagerduty.com/v2/enqueue",
                data=json.dumps(body).encode("utf-8"),
                headers={"Content-Type": "application/json"},
                method="POST",
            )
            with urllib.request.urlopen(req) as resp:
                print(resp.read().decode("utf-8"))
        env:
          - name: SUMMARY
            value: "{{inputs.parameters.summary}}"
          - name: SEVERITY
            value: "{{inputs.parameters.severity}}"
          - name: SOURCE
            value: "{{inputs.parameters.source}}"
          - name: ROUTING_KEY
            valueFrom:
              secretKeyRef:
                name: "{{inputs.parameters.routing-key-secret-name}}"
                key: "{{inputs.parameters.routing-key-secret-key}}"
