#!/usr/bin/env -S deno run --check --allow-write=. --allow-read=. --lock=./tools/deno.lock.json
// Copyright 2018-2026 the Deno authors. MIT license.
import {
  conditions,
  createWorkflow,
  defineMatrix,
  job,
  step,
} from "jsr:@david/gagen@0.3.1";

const matrix = defineMatrix({
  include: [
    { os: "linux", runner: "ubuntu-latest" },
    { os: "windows", runner: "windows-latest" },
    { os: "darwin", runner: "macos-latest" },
  ],
});

const testJob = job("test", {
  runsOn: matrix.runner,
  strategy: { matrix },
  steps: [
    step({
      name: "Checkout",
      uses: "actions/checkout@v6",
      with: { submodules: true },
    }),
    step({
      name: "Setup Deno",
      uses: "denoland/setup-deno@v2",
      with: { "deno-version": "canary" },
    }),
    step({
      name: "Install Python",
      uses: "actions/setup-python@v6",
      with: { "python-version": 3.11 },
    }),
    step({
      name: "Authenticate with Google Cloud",
      uses: "google-github-actions/auth@v3",
      with: {
        project_id: "denoland",
        credentials_json: "${{ secrets.GCP_SA_KEY }}",
        export_environment_variables: true,
        create_credentials_file: true,
      },
    }),
    step({
      name: "Setup gcloud",
      uses: "google-github-actions/setup-gcloud@v3",
      with: { project_id: "denoland" },
    }),
    step({
      name: "Run tests",
      run: "deno -A tools/ecosystem_compat_tests.ts",
    }),
    step({
      name: "Upload the report to dl.deno.land",
      env: {
        AWS_ACCESS_KEY_ID: "${{ vars.S3_ACCESS_KEY_ID }}",
        AWS_SECRET_ACCESS_KEY: "${{ secrets.S3_SECRET_ACCESS_KEY }}",
        AWS_ENDPOINT_URL_S3: "${{ vars.S3_ENDPOINT }}",
        AWS_DEFAULT_REGION: "${{vars.S3_REGION }}",
      },
      run:
        "aws s3 cp tools/ecosystem_report.json s3://dl-deno-land/ecosystem-compat-test/$(date +%F)/report-${{matrix.os}}.json",
    }),
  ],
});

const workflow = createWorkflow({
  name: "ecosystem_compat_test",
  on: {
    schedule: [{ cron: "0 10 * * 1-5" }],
    workflow_dispatch: {},
  },
  jobs: [
    testJob,
    {
      id: "summary",
      runsOn: "ubuntu-latest",
      needs: [testJob],
      if: conditions.status.always(),
      steps: [
        step({
          name: "Checkout",
          uses: "actions/checkout@v6",
          with: { submodules: true },
        }),
        step({
          name: "Setup Deno",
          uses: "denoland/setup-deno@v2",
        }),
        step({
          name: "Install Python",
          uses: "actions/setup-python@v6",
          with: { "python-version": 3.11 },
        }),
        step({
          name: "Authenticate with Google Cloud",
          uses: "google-github-actions/auth@v3",
          with: {
            project_id: "denoland",
            credentials_json: "${{ secrets.GCP_SA_KEY }}",
            export_environment_variables: true,
            create_credentials_file: true,
          },
        }),
        step({
          name: "Setup gcloud",
          uses: "google-github-actions/setup-gcloud@v3",
          with: { project_id: "denoland" },
        }),
        step({
          name: "Post message to slack channel",
          run: "deno -A tools/ecosystem_compat_slack.ts",
          env: {
            SLACK_TOKEN: "${{ secrets.NODE_COMPAT_SLACK_TOKEN }}",
            SLACK_CHANNEL: "${{ secrets.NODE_COMPAT_SLACK_CHANNEL }}",
          },
        }),
      ],
    },
  ],
});

const header =
  "# GENERATED BY ./ecosystem_compat_test.ts -- DO NOT DIRECTLY EDIT";

export function generate() {
  return workflow.toYamlString({ header });
}

export const ECOSYSTEM_COMPAT_TEST_YML_URL = new URL(
  "./ecosystem_compat_test.generated.yml",
  import.meta.url,
);

if (import.meta.main) {
  workflow.writeOrLint({ filePath: ECOSYSTEM_COMPAT_TEST_YML_URL, header });
}
