name: Pagarmejs pipeline

on:
  push:
    branches:
      - master
  pull_request:

concurrency: 
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true

jobs:
  lint_and_test:
    name: Lint and tests
    runs-on: ubuntu-latest
    strategy:
      matrix:
        node-version: [10.x, 12.x]
    steps:
    - 
      name: Project checkout
      uses: actions/checkout@v3
    - 
      name: Cache node modules
      uses: actions/cache@v3
      with:
        path: node_modules
        key: dependencies-{{ checksum "yarn.lock" }}
    - 
      name: Run yarn
      run: yarn
    -
      name: Run lint
      run: yarn lint
    -
      name: Run test
      run: yarn test
    - 
      name: Upload Coverage
      uses: actions/upload-artifact@v3
      with:
        name: pagarmejs-coverage
        path: coverage/
        retention-days: 1

  sonar:
    name: Sonar
    needs: lint_and_test
    runs-on: ubuntu-latest
    steps:
    -
      name: Checkout
      uses: actions/checkout@v3
      with:
        fetch-depth: 0
    -
      name: Download coverage
      uses: actions/download-artifact@v2
      with:
        name: pagarmejs-coverage
        path: coverage/
    -
      name: Run Sonar
      uses: sonarsource/sonarcloud-github-action@master
      env:
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
