name: Test
on:
  push:
    branches:
      - master
  pull_request:
    branches: [ master ]
jobs:
  lint:
    name: Lint Code Base
    runs-on: ubuntu-latest
    steps:
      - name: Checkout Code
        uses: actions/checkout@v3
        with:
          fetch-depth: 0
      - name: Lint Code Base
        uses: github/super-linter@v4
        env:
          VALIDATE_ALL_CODEBASE: false
          DEFAULT_BRANCH: master
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          VALIDATE_JSCPD: false # Can not exclude specific file: https://github.com/kucherenko/jscpd/issues/215
          VALIDATE_TYPESCRIPT_STANDARD: false
          VALIDATE_MARKDOWN: false
  build:
    name: Build
    needs: lint
    runs-on: ubuntu-latest
    steps:
      - name: Checkout Code
        uses: actions/checkout@v3
        with:
          fetch-depth: 0
      - uses: pnpm/action-setup@v2
        with:
          version: latest
          run_install: true
      - name: Build
        run: pnpm run build
      - name: Upload build
        uses: actions/upload-artifact@v3
        with:
          name: build
          path: dist
  tests:
    name: Unit Tests
    runs-on: ubuntu-latest
    needs: build
    steps:
      - uses: actions/checkout@v3
        with:
          fetch-depth: 0
      - uses: pnpm/action-setup@v2
        with:
          version: latest
          run_install: |
            - recursive: false
      - name: Run Unit Tests
        run: pnpm test
      - uses: actions/upload-artifact@v3
        with:
          name: unittest-coverage
          path: coverage/lcov.info
  sonarcloud:
    name: SonarCloud
    runs-on: ubuntu-latest
    if: github.event_name == 'push'
    needs:
      - tests
    steps:
      - uses: actions/checkout@v3
        with:
          fetch-depth: 0  # Shallow clones should be disabled for a better relevancy of analysis
      - uses: actions/download-artifact@v3
        with:
          name: unittest-coverage
          path: coverage
      - name: SonarCloud Scan
        uses: SonarSource/sonarcloud-github-action@master
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}  # Needed to get PR information, if any
          SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
  release:
    name: Release
    runs-on: ubuntu-latest
    if: github.event_name == 'push'
    needs:
      - build
      - sonarcloud
      - tests
    steps:
      - uses: actions/checkout@v3
      - name: Install Node.js
        uses: actions/setup-node@v3
        with:
          node-version: 18
      - uses: pnpm/action-setup@v2
        with:
          version: latest
          run_install: |
            - recursive: false
      - name: Download build
        uses: actions/download-artifact@v3
        with:
          name: build
          path: dist
      - run: mkdir -p docs
      - run: pnpx semantic-release
        env:
          GITHUB_TOKEN: ${{ secrets.GH_TOKEN }}
          NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
          SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
