# This workflow will build a Java project with Gradle
# For more information see: https://help.github.com/actions/language-and-framework-guides/building-and-testing-java-with-gradle

name: Tests

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

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v2
    - name: Set up JDK 11
      uses: actions/setup-java@v1
      with:
        java-version: 11

    - name: Cache Gradle packages
      uses: actions/cache@v2
      with:
        path: |
          ~/.gradle/caches
          ~/.gradle/wrapper
        key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties') }}
        restore-keys: |
          ${{ runner.os }}-gradle-
    - name: Build
      uses: eskatos/gradle-command-action@v1
      with:
        gradle-version: 7.0
        arguments: build -x test -x ktlintKotlinScriptCheck -x ktlintTestSourceSetCheck -x ktlintMainSourceSetCheck -x detekt
    - name: Cleanup Gradle Cache
      # Remove some files from the Gradle cache, so they aren't cached by GitHub Actions.
      # Restoring these files from a GitHub Actions cache might cause problems for future builds.
      run: |
        rm -f ~/.gradle/caches/modules-2/modules-2.lock
        rm -f ~/.gradle/caches/modules-2/gc.properties

    - name: processResources
      uses: eskatos/gradle-command-action@v1
      with:
        gradle-version: 7.0
        arguments: processResources
    - name: processTestResources
      uses: eskatos/gradle-command-action@v1
      with:
        gradle-version: 7.0
        arguments: processResources
    - uses: actions/upload-artifact@v2
      with:
        name: Build
        path: build

  testSql:
    needs: build
    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v2
    - name: Set up JDK 11
      uses: actions/setup-java@v1
      with:
        java-version: 11
    - uses: actions/download-artifact@v2
      with:
        name: Build
        path: build

    - name: Composer Up
      uses: eskatos/gradle-command-action@v1
      with:
        gradle-version: 7.0
        arguments: testSqlComposeUp

    - name: TestSql
      uses: eskatos/gradle-command-action@v1
      with:
        gradle-version: 7.0
        arguments: testSql

  test:
    needs: build
    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v2

    - name: Set up JDK 11
      uses: actions/setup-java@v1
      with:
        java-version: 11

    - uses: actions/download-artifact@v2
      with:
        name: Build
        path: build

    - name: Composer Up
      uses: eskatos/gradle-command-action@v1
      with:
        gradle-version: 7.0
        arguments: testComposeUp

    - name: Test
      uses: eskatos/gradle-command-action@v1
      with:
        gradle-version: 7.0
        arguments: test

    - name: Coverage
      uses: eskatos/gradle-command-action@v1
      with:
        gradle-version: 7.0
        arguments: coveralls
      env:
        COVERALLS_REPO_TOKEN: ${{ secrets.COVERALLS_REPO_TOKEN }}

    - name: Cache SonarCloud packages
      uses: actions/cache@v1
      with:
        path: ~/.sonar/cache
        key: ${{ runner.os }}-sonar
        restore-keys: ${{ runner.os }}-sonar

    - name: Test
      uses: eskatos/gradle-command-action@v1
      with:
        gradle-version: 7.0
        arguments: test

    - name: Build and analyze
      uses: eskatos/gradle-command-action@v1
      env:
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}  # Needed to get PR information, if any
        SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
      with:
        gradle-version: 7.0
        arguments: sonarqube --info

  lint:
    needs: build
    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v2
    - name: Set up JDK 11
      uses: actions/setup-java@v1
      with:
        java-version: 11
    - uses: actions/download-artifact@v2
      with:
        name: Build
        path: build
    - name: Lint
      uses: eskatos/gradle-command-action@v1
      with:
        gradle-version: 7.0
        arguments: ktlintCheck
