iterate.com / cloud

git clone https://git.iterate.com/repos/cloud

cloud / internal-packages / cloud-clients / scripts / synthetic-delivery.tsmain at aacbc74

// scripts/synthetic-delivery.ts — A SIGNED DELIVERY ABOUT NOBODY, for the production Cloud: the
// check that a webhook address works before a provider is pointed at it, and that the Cloud
// passes a delivery on to os.iterate.com. It signs with the app's own secret, read from
// production's Doppler config in the environment, and sends:
//
//   github   a `ping`, which names no installation
//   slack    an event of a workspace id that does not exist
//   slack-interactivity   a pressed button of that workspace, on Slack's second address
//
// So no deployment gets it, and os.iterate.com answers it `200 { ok, ignored }` and writes
// nothing. It prints the Cloud's answer and the delivery's id; the Cloud's log line
// `cloud.passed-on { url, status }` says what os.iterate.com answered (`wrangler tail`).
//
//   doppler run --project os --config prd --only-secrets ITERATE -- \
//     node internal-packages/cloud-clients/scripts/synthetic-delivery.ts https://cloud.iterate.com github|slack|slack-interactivity
import { createHmac, randomUUID } from "node:crypto";

const [cloud, provider] = process.argv.slice(2);
if (!cloud || !["github", "slack", "slack-interactivity"].includes(provider || ""))
  throw new Error("usage: synthetic-delivery.ts <cloud origin> github|slack|slack-interactivity");
const { integrations } = JSON.parse(process.env.ITERATE!);
const id = randomUUID();
const hmac = (secret: string, text: string) =>
  createHmac("sha256", secret).update(text).digest("hex");

const sent =
  provider === "github"
    ? (() => {
        const body = JSON.stringify({ zen: "A delivery about nobody.", hook_id: 0 });
        return {
          body,
          headers: {
            "content-type": "application/json",
            "user-agent": "GitHub-Hookshot/iterate-synthetic",
            "x-github-event": "ping",
            "x-github-delivery": id,
            "x-hub-signature-256": `sha256=${hmac(integrations.github.webhookSecret, body)}`,
          },
        };
      })()
    : (() => {
        const eventId = `Ev${id.replaceAll("-", "").slice(0, 12).toUpperCase()}`;
        const team = "T00000000";
        // interactivity is a form with one field, `payload`; an event is JSON
        const body =
          provider === "slack"
            ? JSON.stringify({
                type: "event_callback",
                team_id: team,
                event_id: eventId,
                event: { type: "app_home_opened" },
              })
            : `payload=${encodeURIComponent(JSON.stringify({ type: "block_actions", team: { id: team }, trigger_id: eventId }))}`;
        const timestamp = String(Math.floor(Date.now() / 1000));
        return {
          body,
          headers: {
            "content-type":
              provider === "slack" ? "application/json" : "application/x-www-form-urlencoded",
            "x-slack-request-timestamp": timestamp,
            "x-slack-signature": `v0=${hmac(integrations.slack.webhookSigningSecret, `v0:${timestamp}:${body}`)}`,
          },
        };
      })();

const route =
  provider === "slack-interactivity" ? "slack/interactivity-webhook" : `${provider}/webhook`;
const answer = await fetch(`${cloud}/clients/${route}`, { method: "POST", ...sent });
console.log(
  JSON.stringify({ provider, delivery: id, status: answer.status, answer: await answer.text() }),
);