# Report this pipeline's result to id3as/ci-workflows for the aggregated product
# CI dashboard (one live Discord message showing the whole product x pipeline
# grid). Replaces the old per-run pony post: the dashboard renders the pony now,
# from the payload this action dispatches.
#
# GITHUB_TOKEN can't repository_dispatch another repo, so the caller passes a
# CI_DISPATCH_TOKEN (a fine-grained PAT scoped to id3as/ci-workflows, Contents:
# write — what the dispatch API requires). If it's absent the step no-ops with a
# warning rather than failing the build, so a repo can adopt this before the
# token is provisioned.
name: CI status dispatch
description: Dispatch this pipeline's result to the id3as/ci-workflows product dashboard.

inputs:
  token:
    description: "PAT with Contents:write on id3as/ci-workflows. Pass secrets.CI_DISPATCH_TOKEN."
    required: true
  product:
    description: "Product key: commentary | playout | probe | studio | funke | reuters."
    required: true
  pipeline:
    description: "Pipeline key: build-image | integration | checks | nightly | dev-kit | smoke | docs."
    required: true
  status:
    description: "success or failure."
    required: true
  detail:
    description: "Optional extra line shown under the cell (e.g. a nightly bump summary)."
    required: false
    default: ""

runs:
  using: composite
  steps:
    - name: Dispatch ci-status to id3as/ci-workflows
      shell: bash
      env:
        TOKEN: ${{ inputs.token }}
        PRODUCT: ${{ inputs.product }}
        PIPELINE: ${{ inputs.pipeline }}
        STATUS: ${{ inputs.status }}
        DETAIL: ${{ inputs.detail }}
        SHA: ${{ github.sha }}
        RUN_URL: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}
      run: |
        set -euo pipefail
        if [ -z "$TOKEN" ]; then
          echo "::warning::CI_DISPATCH_TOKEN not set — skipping product-dashboard dispatch"
          exit 0
        fi
        ts="$(date -u +%Y-%m-%dT%H:%M:%SZ)"
        # jq assembles the payload so a bump summary with quotes/newlines can't
        # break the JSON; detail is included only when non-empty.
        payload="$(jq -cn \
          --arg product "$PRODUCT" --arg pipeline "$PIPELINE" --arg conclusion "$STATUS" \
          --arg sha "$SHA" --arg runUrl "$RUN_URL" --arg ts "$ts" --arg detail "$DETAIL" \
          '{event_type: "ci-status", client_payload: (
             {product: $product, pipeline: $pipeline, conclusion: $conclusion, sha: $sha, runUrl: $runUrl, ts: $ts}
             + (if $detail == "" then {} else {detail: $detail} end)
           )}')"
        code="$(curl -sS -o /tmp/dispatch-resp -w '%{http_code}' -X POST \
          -H "authorization: Bearer $TOKEN" \
          -H "accept: application/vnd.github+json" \
          -H "x-github-api-version: 2022-11-28" \
          "https://api.github.com/repos/id3as/ci-workflows/dispatches" \
          -d "$payload")"
        echo "dashboard dispatch: HTTP $code ($PRODUCT/$PIPELINE=$STATUS)"
        if [ "$code" != "204" ]; then
          echo "::error::dashboard dispatch failed (HTTP $code)"; cat /tmp/dispatch-resp || true; exit 1
        fi
