name: Main workflow

on:
  pull_request:
    branches:
      - master
  push:
    branches:
      - master

jobs:
  build:
    if: contains(github.event.head_commit.message, '[skip ci]') == false
    name: Auditing Changes
    runs-on: macos-latest

    strategy:
      matrix:
        node-version: [14.16.0]

    steps:
      - name: Git checkout
        uses: actions/checkout@v2
        with:
          fetch-depth: '0'
      - name: Git pull
        run: |
          git fetch origin ${{ github.head_ref }}
          (git branch --list | grep ${{ github.head_ref }}) || (git checkout origin/${{ github.head_ref }} -b ${{ github.head_ref }})
          # git checkout origin/${{ github.head_ref }} -b ${{ github.head_ref }}
          git pull
        if: github.head_ref != ''
        env:
          GIT_AUTH_TOKEN: ${{ secrets.GIT_AUTH_TOKEN }}
      - name: Git pull (master)
        run: |
          git checkout master
          git pull origin master
        if: github.head_ref == ''
        env:
          GIT_AUTH_TOKEN: ${{ secrets.GIT_AUTH_TOKEN }}
      - name: Setting up node.js ${{ matrix.node-version }}
        uses: actions/setup-node@v1
        with:
          node-version: ${{ matrix.node-version }}
      - run: chmod +x ./.workflow/*.sh
      - run: npm install yarn@1.22.4 -g
      - run: yarn
      - name: Running tests
        run: yarn test
      - name: Auditing commit message
        if: success()
        run: ./.workflow/validate_last_commit_message.sh
      - name: Versioning when needed (master)
        if: success() && github.event_name == 'push'
        run: ./.workflow/versioning.sh
        env:
          GIT_AUTH_TOKEN: ${{ secrets.GIT_AUTH_TOKEN }}
          NPM_AUTH_TOKEN: ${{ secrets.NPM_AUTH_TOKEN }}
          GIT_PATH: ${{ github.repository }}
    env:
      CI: true
