# This action is centrally managed in https://github.com/asyncapi/.github/
# Don't make changes to this file in this repo as they will be overwritten with changes made to the same file in above mentioned repo

# It does magic only if there is package.json file in the root of the project
name: PR testing - if Node project

on:
  pull_request:
    types: [opened, reopened, synchronize, ready_for_review]

jobs:
  test-nodejs-pr:
    name: Test NodeJS PR - ${{ matrix.os }}
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        os: [ubuntu-latest, macos-latest, windows-latest]
    steps:
      - if: "github.event.pull_request.draft == false &&!((github.actor == 'asyncapi-bot' && startsWith(github.event.pull_request.title, 'ci: update global workflows')) || (github.actor == 'asyncapi-bot' && startsWith(github.event.pull_request.title, 'chore(release):')) || (github.actor == 'allcontributors' && startsWith(github.event.pull_request.title, 'docs: add')))"
        id: should_run
        name: Should Run
        run: echo "::set-output name=shouldrun::true"
      - if: steps.should_run.outputs.shouldrun == 'true' 
        name: Set git to use LF #to once and for all finish neverending fight between Unix and Windows
        run: |
          git config --global core.autocrlf false
          git config --global core.eol lf
      - if: steps.should_run.outputs.shouldrun == 'true' 
        name: Checkout repository
        uses: actions/checkout@v2
      - if: steps.should_run.outputs.shouldrun == 'true' 
        name: Check if Node.js project and has package.json
        id: packagejson
        run: test -e ./package.json && echo "::set-output name=exists::true" || echo "::set-output name=exists::false"
        shell: bash
      - if: steps.packagejson.outputs.exists == 'true'
        name: Setup Node.js
        uses: actions/setup-node@v2
        with:
          node-version: 14
          cache: 'npm'
          cache-dependency-path: '**/package-lock.json'
      - if: steps.packagejson.outputs.exists == 'true'
        name: Install dependencies
        id: first-installation
        run: npm install --loglevel verbose
        continue-on-error: true
      - if: steps.first-installation.outputs.status == 'failure' && steps.packagejson.outputs.exists == 'true'
        name: Clear NPM cache and install deps again
        run: | 
          npm cache clean --force
          npm install --loglevel verbose
      - if: steps.packagejson.outputs.exists == 'true'
        name: Test
        run: npm test
      - if: steps.packagejson.outputs.exists == 'true'
        name: Run linter
        run: npm run lint
      - if: steps.packagejson.outputs.exists == 'true'
        name: Run release assets generation to make sure PR does not break it
        run: npm run generate:assets
