name: Static Analysis

on:
  pull_request:
  push:
    branches:
      - main

concurrency:
  group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }}
  cancel-in-progress: true

permissions:
  contents: write
  pull-requests: write
  actions: read

jobs:
  yarn-fmt-check:
    runs-on: 'ubuntu-22.04'
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with:
          node-version-file: '.nvmrc'
          cache: 'yarn'
      - run: yarn install
      - run: yarn fmt-check

  yarn-build-wasm:
    runs-on: ubuntu-22.04

    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with:
          node-version-file: '.nvmrc'
          cache: 'yarn'
      - run: yarn install
      - run: yarn build:wasm

  yarn-tsc:
    runs-on: ubuntu-22.04

    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with:
          node-version-file: '.nvmrc'
          cache: 'yarn'
      - run: yarn install
      - uses: Swatinem/rust-cache@v2
        with:
          workspaces: './src/wasm-lib'

      - run: yarn build:wasm
      - run: yarn tsc

  yarn-lint:
    runs-on: ubuntu-22.04

    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with:
          node-version-file: '.nvmrc'
          cache: 'yarn'
      - run: yarn install
      - run: yarn lint

  python-codespell:
    runs-on: ubuntu-22.04
    steps:
      - name: Checkout
        uses: actions/checkout@v4
      - name: Set up Python
        uses: actions/setup-python@v5
      - name: Install codespell
        run: |
            python -m pip install codespell
      - name: Run codespell
        run: codespell --config .codespellrc # Edit this file to tweak the typo list and other configuration.

  yarn-unit-test-kcl-samples:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - uses: actions/setup-node@v4
        with:
          node-version-file: '.nvmrc'
          cache: 'yarn'

      - run: yarn install
      - run: yarn build:wasm

      - run: yarn simpleserver:bg
        if: ${{ github.event_name != 'release' && github.event_name != 'schedule' }}

      - name: Install Chromium Browser
        if: ${{ github.event_name != 'release' && github.event_name != 'schedule' }}
        run: yarn playwright install chromium --with-deps

      - name: run unit tests for kcl samples
        if: ${{ github.event_name != 'release' && github.event_name != 'schedule' }}
        run: yarn test:unit:kcl-samples
        env:
          VITE_KC_DEV_TOKEN: ${{ secrets.KITTYCAD_API_TOKEN_DEV }}

  yarn-unit-test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - uses: actions/setup-node@v4
        with:
          node-version-file: '.nvmrc'
          cache: 'yarn'

      - run: yarn install
      - run: yarn build:wasm

      - run: yarn simpleserver:bg
        if: ${{ github.event_name != 'release' && github.event_name != 'schedule' }}

      - name: Install Chromium Browser
        if: ${{ github.event_name != 'release' && github.event_name != 'schedule' }}
        run: yarn playwright install chromium --with-deps

      - name: run unit tests
        if: ${{ github.event_name != 'release' && github.event_name != 'schedule' }}
        run: yarn test:unit
        env:
          VITE_KC_DEV_TOKEN: ${{ secrets.KITTYCAD_API_TOKEN_DEV }}

      - name: check for changes
        if: ${{ github.event_name != 'release' && github.event_name != 'schedule' }}
        id: git-check
        run: |
            git add src/lang/std/artifactMapGraphs
            if git status src/lang/std/artifactMapGraphs | grep -q "Changes to be committed"
            then echo "modified=true" >> $GITHUB_OUTPUT
            else echo "modified=false" >> $GITHUB_OUTPUT
            fi
      - name: Commit changes, if any
        if: ${{ github.event_name != 'release' && github.event_name != 'schedule' && steps.git-check.outputs.modified == 'true' }}
        run: |
          git config --local user.email "github-actions[bot]@users.noreply.github.com"
          git config --local user.name "github-actions[bot]"
          git remote set-url origin https://${{ github.actor }}:${{ secrets.GITHUB_TOKEN }}@github.com/${{ github.repository }}.git
          git fetch origin
          echo ${{ github.head_ref }}
          git checkout ${{ github.head_ref }}
          # TODO when webkit works on ubuntu remove the os part of the commit message
          git commit -am "Look at this (photo)Graph *in the voice of Nickelback*" || true
          git push
          git push origin ${{ github.head_ref }}
