name: Ecosystem CI

on:
  workflow_dispatch:
  pull_request:
    types: [opened, synchronize]
    paths:
      - 'crates/oxc_parser/**'
      - 'crates/oxc_cli/**'
      - 'crates/oxc_linter/**'
      - '!.github/workflows/ecosystem.yml'
  push:
    branches:
      - main
    paths:
      - 'crates/oxc_parser/**'
      - 'crates/oxc_cli/**'
      - 'crates/oxc_linter/**'
      - '!.github/workflows/ecosystem.yml'

concurrency:
  group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.sha }}
  cancel-in-progress: ${{ github.ref_name != 'main' }}

jobs:
  build:
    name: Build
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - name: Install Rust
        uses: ./.github/actions/rustup
        with:
          shared-key: oxlint
          save-cache: ${{ github.ref_name == 'main' }}

      - name: Build oxlint
        run: cargo build --release -p oxc_cli --bin oxlint

      - name: Upload Binary
        uses: actions/upload-artifact@v4
        with:
          if-no-files-found: error
          name: oxlint
          path: ./target/release/oxlint

  test:
    name: Test
    runs-on: ubuntu-latest
    needs: build
    strategy:
      fail-fast: false
      matrix:
        include:
          - repository: microsoft/vscode
            ref: main
          - repository: DefinitelyTyped/DefinitelyTyped
            ref: master
    steps:
      - name: Clone ${{ matrix.repository }}
        uses: actions/checkout@v4
        with:
          show-progress: false
          repository: ${{ matrix.repository }}
          ref: ${{ matrix.ref }}
          path: ./e2e

      - name: Download Artifacts
        uses: actions/download-artifact@v4
        with:
          name: oxlint
          path: .

      - name: Run oxlint and pipe output to a temp file
        shell: bash
        run: |
          chmod +x ./oxlint
          ./oxlint --quiet -D all ./e2e | tee log || true

      - name: Using grep to check if oxlint panic
        shell: bash
        run: |
          if grep --quiet --ignore-case panic log; then
            exit 1
          fi
