# Generated by `starci compile` from events.ci.star. Edit that, not this.
#
# Each job is one step of the plan: the same image, command, tools and
# packages, given only the step's inputs. A job skips its work when it has
# already passed with the same inputs.
#
# What does not carry over:
#  - The script looks at the pull request. Whether it comes from a fork carries
#    over; any other rule about it does not, and every pull request is treated
#    as one into main.
#  - The script looks at the branch. Whether it is main carries over; any other
#    rule about branch names does not, and every other branch is treated alike.
#  - A schedule's runs may overlap here, where starci would skip a firing while
#    the last run is still going; and a missed firing is never made up.
#  - Passes are remembered in this repository's Actions cache, which GitHub
#    scopes by branch and evicts when old. starci's own store has neither
#    limit.

name: CI

on:
  push:
    branches: ['**']
    tags: ['**']
  pull_request:
  schedule:
    - cron: "0 3 * * *"
  workflow_dispatch:

jobs:
  lint:
    name: "lint"
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - name: Has this already passed with these inputs?
        id: passed
        uses: actions/cache@v4
        with:
          path: ${{ runner.temp }}/starci-passed
          key: starci-passed-lint-d309b4ed71edb687-${{ hashFiles('**', '!.git/**') }}
      - name: "lint"
        if: steps.passed.outputs.cache-hit != 'true'
        env:
          STARCI_RUN: |-
            npm ci && npm run lint
        run: |
          docker run --rm \
            -v "$PWD:/workspace" \
            -w /workspace \
            node:22-bookworm-slim sh -c "$STARCI_RUN"
      - name: Remember this pass
        if: steps.passed.outputs.cache-hit != 'true'
        run: mkdir -p "$RUNNER_TEMP/starci-passed" && date > "$RUNNER_TEMP/starci-passed/at"

  test:
    name: "test"
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - name: Has this already passed with these inputs?
        id: passed
        uses: actions/cache@v4
        with:
          path: ${{ runner.temp }}/starci-passed
          key: starci-passed-test-3dce010675ea0e57-${{ hashFiles('**', '!.git/**') }}
      - name: "test"
        if: steps.passed.outputs.cache-hit != 'true'
        env:
          STARCI_RUN: |-
            npm ci && npm test
        run: |
          docker run --rm \
            -v "$PWD:/workspace" \
            -w /workspace \
            node:22-bookworm-slim sh -c "$STARCI_RUN"
      - name: Remember this pass
        if: steps.passed.outputs.cache-hit != 'true'
        run: mkdir -p "$RUNNER_TEMP/starci-passed" && date > "$RUNNER_TEMP/starci-passed/at"

  deploy-staging:
    name: "deploy staging"
    runs-on: ubuntu-latest
    needs: [lint, test]
    if: ${{ (github.event_name == 'push' && github.ref == 'refs/heads/main') }}
    steps:
      - uses: actions/checkout@v4
      - name: "deploy staging"
        env:
          STARCI_RUN: |-
            npm ci && npm run deploy:staging
        run: |
          docker run --rm \
            -v "$PWD:/workspace" \
            -w /workspace \
            node:22-bookworm-slim sh -c "$STARCI_RUN"

  publish:
    name: "publish"
    runs-on: ubuntu-latest
    needs: [lint, test]
    if: ${{ (github.event_name == 'push' && github.ref_type == 'tag') }}
    steps:
      - uses: actions/checkout@v4
      - name: "publish"
        env:
          STARCI_RUN: |-
            npm ci && npm publish
        run: |
          docker run --rm \
            -v "$PWD:/workspace" \
            -w /workspace \
            node:22-bookworm-slim sh -c "$STARCI_RUN"

  preview:
    name: "preview"
    runs-on: ubuntu-latest
    needs: [lint, test]
    if: ${{ (github.event_name == 'pull_request' && github.event.pull_request.head.repo.full_name == github.repository) }}
    steps:
      - uses: actions/checkout@v4
      - name: "preview"
        env:
          STARCI_RUN: |-
            npm ci && npm run deploy:preview
        run: |
          docker run --rm \
            -v "$PWD:/workspace" \
            -w /workspace \
            node:22-bookworm-slim sh -c "$STARCI_RUN"

  end-to-end:
    name: "end to end"
    runs-on: ubuntu-latest
    needs: [lint, test]
    if: ${{ (github.event_name == 'schedule' && github.event.schedule == '0 3 * * *') }}
    steps:
      - uses: actions/checkout@v4
      - name: Has this already passed with these inputs?
        id: passed
        uses: actions/cache@v4
        with:
          path: ${{ runner.temp }}/starci-passed
          key: starci-passed-end-to-end-c35e8a3446283be5-${{ hashFiles('**', '!.git/**') }}
      - name: "end to end"
        if: steps.passed.outputs.cache-hit != 'true'
        env:
          STARCI_RUN: |-
            npm ci && npm run test:e2e
        run: |
          docker run --rm \
            -v "$PWD:/workspace" \
            -w /workspace \
            node:22-bookworm-slim sh -c "$STARCI_RUN"
      - name: Remember this pass
        if: steps.passed.outputs.cache-hit != 'true'
        run: mkdir -p "$RUNNER_TEMP/starci-passed" && date > "$RUNNER_TEMP/starci-passed/at"

  audit:
    name: "audit"
    runs-on: ubuntu-latest
    if: ${{ (github.event_name == 'schedule' && github.event.schedule == '0 3 * * *') }}
    steps:
      - uses: actions/checkout@v4
      - name: "audit"
        env:
          STARCI_RUN: |-
            npm ci && npm audit --audit-level=high
        run: |
          docker run --rm \
            -v "$PWD:/workspace" \
            -w /workspace \
            node:22-bookworm-slim sh -c "$STARCI_RUN"
