name: Continuous Integration

on:
  push:
    branches:
      - main
  pull_request:

jobs:
  pre_ci:
    name: Prepare CI environment
    runs-on: ubuntu-latest
    steps:
      - name: Checkout Project
        uses: actions/checkout@v2
        with:
          fetch-depth: 2
      - name: '[Push] Get commit message'
        if: github.event_name == 'push'
        id: push_get_commit_message
        run: echo ::set-output name=push_commit_message::$(git log --format=%B -n 1 HEAD)
      - name: '[Pull Request] Get commit message'
        if: github.event_name == 'pull_request'
        id: pr_get_commit_message
        run: echo ::set-output name=pr_commit_message::$(git log --format=%B -n 1 HEAD^2)
      - name: Add problem matchers
        run: |
          echo "::add-matcher::.github/problemMatchers/tsc.json"
          echo "::add-matcher::.github/problemMatchers/eslint-stylish.json"
    outputs:
      commit_message: $( [ -z "${{ steps.pr_get_commit_message.outputs.pr_commit_message }}" ] && echo "${{ steps.push_get_commit_message.outputs.push_commit_message }}" || echo "${{ steps.pr_get_commit_message.outputs.pr_commit_message }}" )

  Linting:
    name: Linting
    runs-on: ubuntu-latest
    if: "!contains(needs.pre_ci.outputs.commit_message, '[skip ci]')"
    needs: pre_ci
    steps:
      - name: Checkout Project
        uses: actions/checkout@v2
      - name: Use Node.js 14
        uses: actions/setup-node@v2
        with:
          node-version: 14
      - name: Restore CI Cache
        uses: actions/cache@v2.1.4
        with:
          path: node_modules
          key: ${{ runner.os }}-14-${{ hashFiles('**/yarn.lock') }}
      - name: Install Dependencies
        run: yarn --ignore-scripts --frozen-lockfile
      - name: Build ESLint and Prettier configs
        run: yarn tsc -b packages/eslint-config packages/prettier-config
      - name: Run ESLint
        run: yarn lint --fix=false

  Testing:
    name: Unit Tests
    runs-on: ubuntu-latest
    if: "!contains(needs.pre_ci.outputs.commit_message, '[skip ci]')"
    needs: pre_ci
    steps:
      - name: Checkout Project
        uses: actions/checkout@v2
      - name: Use Node.js v14
        uses: actions/setup-node@v2
        with:
          node-version: 14
      - name: Restore CI Cache
        uses: actions/cache@v2.1.4
        with:
          path: node_modules
          key: ${{ runner.os }}-14-${{ hashFiles('**/yarn.lock') }}
      - name: Install Dependencies
        run: yarn --frozen-lockfile
      - name: Build Utilities
        run: yarn workspace @sapphire/utilities build
      - name: Run tests
        run: yarn test --coverage
      - name: Store code coverage report
        uses: actions/upload-artifact@v2
        with:
          name: coverage
          path: coverage/

  Building:
    name: Compile source code
    runs-on: ubuntu-latest
    if: "!contains(needs.pre_ci.outputs.commit_message, '[skip ci]')"
    needs: pre_ci
    steps:
      - name: Checkout Project
        uses: actions/checkout@v2
      - name: Use Node.js 14
        uses: actions/setup-node@v2
        with:
          node-version: 14
      - name: Restore CI Cache
        uses: actions/cache@v2.1.4
        with:
          path: node_modules
          key: ${{ runner.os }}-14-${{ hashFiles('**/yarn.lock') }}
      - name: Install Dependencies
        run: yarn --ignore-scripts --frozen-lockfile
      - name: Compile Projects
        run: yarn build

  Upload_Coverage_Report:
    name: Upload coverage report to coveralls
    needs: [Testing]
    runs-on: ubuntu-latest
    steps:
      - name: Checkout Project
        uses: actions/checkout@v2
      - name: Download Coverage report
        uses: actions/download-artifact@v2
        with:
          name: coverage
          path: coverage/
      - name: Coveralls Upload
        uses: coverallsapp/github-action@master
        with:
          github-token: ${{ secrets.GITHUB_TOKEN }}
