name: CI

on: [push, pull_request]

jobs:
  build:
    runs-on: ubuntu-latest

    strategy:
      matrix:
        node-version: [18.x]

    outputs:
      version: ${{ steps.properties.outputs.version }}

    steps:
      - uses: actions/checkout@v4

      - name: Use Node.js ${{ matrix.node-version }}
        uses: actions/setup-node@v4
        with:
          node-version: ${{ matrix.node-version }}

      - name: Install dependencies
        run: npm install --legacy-peer-deps

      # Set environment variables
      - name: Export Properties
        id: properties
        shell: bash
        run: |
          VERSION="$(cat package.json | grep -o '"version": *"[0-9.]*"' package.json | awk -F '"' '{print $4}' | cut -f2- -d ' ')"

      - name: Compile
        run: npm run build

      - name: Compile GUI
        run: cd gui-sidebar && npm install && npm run build

      - name: Run unit tests
        run: npm run test

      - name: 'Report Coverage'
        # Set if: always() to also generate the report if tests are failing
        # Only works if you set `reportOnFailure: true` in your vite config as specified above
        if: always()
        uses: davelosert/vitest-coverage-report-action@v2

      - name: Upload coverage reports to Codecov
        uses: codecov/codecov-action@v4.0.1
        with:
          token: ${{ secrets.CODECOV_TOKEN }}
          slug: unit-mesh/auto-dev-vscode

      - name: Install VSCE
        run: npm install -g @vscode/vsce

      - name: Package VSIX
        run: vsce package

     # Prepare plugin archive content for creating artifact
      - name: Prepare Plugin Artifact
        id: artifact
        shell: bash
        run: |
          FILENAME=`ls *.vsix`
          echo "filename=${FILENAME}" >> $GITHUB_OUTPUT

      # Store already-built plugin as an artifact for downloading
      - name: Upload artifact
        uses: actions/upload-artifact@v3
        with:
          name: ${{ steps.artifact.outputs.filename }}
          path: ${{ steps.artifact.outputs.filename }}


  # Prepare a draft release for GitHub Releases page for the manual verification
  # If accepted and published, release workflow would be triggered
  releaseDraft:
    name: Release Draft
    if: github.event_name != 'pull_request'
    needs: build
    runs-on: ubuntu-latest
    permissions:
      contents: write
    steps:

      # Check out current repository
      - name: Fetch Sources
        uses: actions/checkout@v3

      # Remove old release drafts by using the curl request for the available releases with a draft flag
      - name: Remove Old Release Drafts
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          gh api repos/{owner}/{repo}/releases \
            --jq '.[] | select(.draft == true) | .id' \
            | xargs -I '{}' gh api -X DELETE repos/{owner}/{repo}/releases/{}

      # Create a new release draft which is not publicly visible and requires manual acceptance
      - name: Create Release Draft
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          gh release create v${{ needs.build.outputs.version }} \
            --draft \
            --title "v${{ needs.build.outputs.version }}" \
