# CI: runs on every push. Install, lint, check, test, build (dev), e2e.
when:
    - event: pull_request
    - event: push
      branch: main
    - event: tag
    - event: manual

steps:
    - name: install
      image: node:22-bookworm-slim
      commands:
          - corepack enable
          - corepack prepare pnpm@latest --activate
          - pnpm install --frozen-lockfile

    - name: lint
      image: node:22-bookworm-slim
      commands:
          - corepack enable && corepack prepare pnpm@latest --activate
          - pnpm run lint
      depends_on:
          - install

    - name: Send Lint Status Notification (failure)
      image: curlimages/curl
      environment:
          MATTERMOST_BOT_ACCESS_TOKEN:
              from_secret: mattermost_bot_access_token
          MATTERMOST_CHANNEL_ID:
              from_secret: mattermost_tests_channel_id
          MATTERMOST_POST_API_URL:
              from_secret: mattermost_post_api_url
      commands:
          - |
              BODY=$(printf '{"channel_id":"%s","message":"[%s - Build #%s] Lint failure 💩"}' "$MATTERMOST_CHANNEL_ID" "$CI_REPO" "$CI_PIPELINE_NUMBER")
              curl -sS -X POST -H "Content-Type: application/json" -d "$BODY" -H "Authorization: Bearer $MATTERMOST_BOT_ACCESS_TOKEN" $MATTERMOST_POST_API_URL
      depends_on:
          - lint
      when:
          - status: [failure]

    - name: check
      image: node:22-bookworm-slim
      commands:
          - corepack enable && corepack prepare pnpm@latest --activate
          - pnpm run check
      depends_on:
          - lint

    - name: Send Svelte Check Status Notification (failure)
      image: curlimages/curl
      environment:
          MATTERMOST_BOT_ACCESS_TOKEN:
              from_secret: mattermost_bot_access_token
          MATTERMOST_CHANNEL_ID:
              from_secret: mattermost_tests_channel_id
          MATTERMOST_POST_API_URL:
              from_secret: mattermost_post_api_url
      commands:
          - |
              BODY=$(printf '{"channel_id":"%s","message":"[%s - Build #%s] Svelte Check failure 💩"}' "$MATTERMOST_CHANNEL_ID" "$CI_REPO" "$CI_PIPELINE_NUMBER")
              curl -sS -X POST -H "Content-Type: application/json" -d "$BODY" -H "Authorization: Bearer $MATTERMOST_BOT_ACCESS_TOKEN" $MATTERMOST_POST_API_URL
      depends_on:
          - check
      when:
          - status: [failure]

    - name: Unit Tests
      image: node:22-bookworm-slim
      commands:
          - corepack enable && corepack prepare pnpm@latest --activate
          - pnpm run test:run
      depends_on:
          - check

    - name: Send Test Status Notification (failure)
      image: curlimages/curl
      environment:
          MATTERMOST_BOT_ACCESS_TOKEN:
              from_secret: mattermost_bot_access_token
          MATTERMOST_CHANNEL_ID:
              from_secret: mattermost_tests_channel_id
          MATTERMOST_POST_API_URL:
              from_secret: mattermost_post_api_url
      commands:
          - |
              BODY=$(printf '{"channel_id":"%s","message":"[%s - Build #%s] Test failure 💩"}' "$MATTERMOST_CHANNEL_ID" "$CI_REPO" "$CI_PIPELINE_NUMBER")
              curl -sS -X POST -H "Content-Type: application/json" -d "$BODY" -H "Authorization: Bearer $MATTERMOST_BOT_ACCESS_TOKEN" $MATTERMOST_POST_API_URL
      depends_on:
          - Unit Tests
      when:
          - status: [failure]

    - name: build
      image: node:22-bookworm-slim
      commands:
          - corepack enable && corepack prepare pnpm@latest --activate
          - pnpm run build
      depends_on:
          - Unit Tests

    - name: Send Build Status Notification (failure)
      image: curlimages/curl
      environment:
          MATTERMOST_BOT_ACCESS_TOKEN:
              from_secret: mattermost_bot_access_token
          MATTERMOST_CHANNEL_ID:
              from_secret: mattermost_tests_channel_id
          MATTERMOST_POST_API_URL:
              from_secret: mattermost_post_api_url
      commands:
          - |
              BODY=$(printf '{"channel_id":"%s","message":"[%s - Build #%s] Build failure 💩"}' "$MATTERMOST_CHANNEL_ID" "$CI_REPO" "$CI_PIPELINE_NUMBER")
              curl -sS -X POST -H "Content-Type: application/json" -d "$BODY" -H "Authorization: Bearer $MATTERMOST_BOT_ACCESS_TOKEN" $MATTERMOST_POST_API_URL
      depends_on:
          - build
      when:
          - status: [failure]

    - name: build-full
      image: node:22-bookworm-slim
      commands:
          - apt-get update
          - apt-get install -y --no-install-recommends ca-certificates libasound2 libatk-bridge2.0-0 libatk1.0-0 libcups2 libdrm2 libgbm1 libgtk-3-0 libnss3 libxcomposite1 libxdamage1 libxfixes3 libxkbcommon0 libxrandr2
          - rm -rf /var/lib/apt/lists/*
          - corepack enable && corepack prepare pnpm@latest --activate
          - pnpm run critical-css:install
          - pnpm run build:full
      depends_on:
          - build

    - name: Send Build Full Status Notification (failure)
      image: curlimages/curl
      environment:
          MATTERMOST_BOT_ACCESS_TOKEN:
              from_secret: mattermost_bot_access_token
          MATTERMOST_CHANNEL_ID:
              from_secret: mattermost_tests_channel_id
          MATTERMOST_POST_API_URL:
              from_secret: mattermost_post_api_url
      commands:
          - |
              BODY=$(printf '{"channel_id":"%s","message":"[%s - Build #%s] Build Full failure 💩"}' "$MATTERMOST_CHANNEL_ID" "$CI_REPO" "$CI_PIPELINE_NUMBER")
              curl -sS -X POST -H "Content-Type: application/json" -d "$BODY" -H "Authorization: Bearer $MATTERMOST_BOT_ACCESS_TOKEN" $MATTERMOST_POST_API_URL
      depends_on:
          - build-full
      when:
          - status: [failure]

    - name: E2E Tests
      image: node:22-bookworm-slim
      commands:
          - corepack enable && corepack prepare pnpm@latest --activate
          - pnpm exec playwright install chromium --with-deps
          - pnpm run test:e2e
      depends_on:
          - build

    - name: Send E2E Status Notification (failure)
      image: curlimages/curl
      environment:
          MATTERMOST_BOT_ACCESS_TOKEN:
              from_secret: mattermost_bot_access_token
          MATTERMOST_CHANNEL_ID:
              from_secret: mattermost_tests_channel_id
          MATTERMOST_POST_API_URL:
              from_secret: mattermost_post_api_url
      commands:
          - |
              BODY=$(printf '{"channel_id":"%s","message":"[%s - Build #%s] E2E failure 💩"}' "$MATTERMOST_CHANNEL_ID" "$CI_REPO" "$CI_PIPELINE_NUMBER")
              curl -sS -X POST -H "Content-Type: application/json" -d "$BODY" -H "Authorization: Bearer $MATTERMOST_BOT_ACCESS_TOKEN" $MATTERMOST_POST_API_URL
      depends_on:
          - E2E Tests
      when:
          - status: [failure]

    - name: Send CI Pipeline Status Notification (success)
      image: curlimages/curl
      environment:
          MATTERMOST_BOT_ACCESS_TOKEN:
              from_secret: mattermost_bot_access_token
          MATTERMOST_CHANNEL_ID:
              from_secret: mattermost_tests_channel_id
          MATTERMOST_POST_API_URL:
              from_secret: mattermost_post_api_url
      commands:
          - |
              BODY=$(printf '{"channel_id":"%s","message":"[%s - Build #%s] CI pipeline success 🎉"}' "$MATTERMOST_CHANNEL_ID" "$CI_REPO" "$CI_PIPELINE_NUMBER")
              curl -sS -X POST -H "Content-Type: application/json" -d "$BODY" -H "Authorization: Bearer $MATTERMOST_BOT_ACCESS_TOKEN" $MATTERMOST_POST_API_URL
      depends_on:
          - install
          - lint
          - check
          - Unit Tests
          - build
          - build-full
          - E2E Tests
      when:
          - status: [success]
