name: Sonarqube nightly

on:
  schedule:
    - cron: '0 20 * * *'

# Enrich gradle.properties for CI/CD
env:
  CI_GRADLE_ARG_PROPERTIES: >
    -Porg.gradle.jvmargs=-Xmx4g
    -Porg.gradle.parallel=false
jobs:
  codecov-units:
    name: Unit tests with code coverage
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - uses: actions/setup-java@v3
        with: 
          distribution: 'adopt'
          java-version: '11'
      - uses: actions/cache@v3
        with:
          path: |
            ~/.gradle/caches
            ~/.gradle/wrapper
          key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties') }}
          restore-keys: |
            ${{ runner.os }}-gradle-
      - run: ./gradlew allCodeCoverageReport $CI_GRADLE_ARG_PROPERTIES
      - name: Upload Codecov data
        uses: actions/upload-artifact@v3
        if: always()
        with:
          name: codecov-xml
          path: |
            build/reports/jacoco/allCodeCoverageReport/allCodeCoverageReport.xml

  sonarqube:
    name: Sonarqube upload
    runs-on: ubuntu-latest
    needs:
      - codecov-units
    steps:
      - uses: actions/checkout@v3
      - uses: actions/setup-java@v3
        with:
          distribution: 'adopt'
          java-version: '11'
      - uses: actions/cache@v3
        with:
          path: |
            ~/.gradle/caches
            ~/.gradle/wrapper
          key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties') }}
          restore-keys: |
            ${{ runner.os }}-gradle-
      - uses: actions/download-artifact@v3
        with:
          name: codecov-xml # will restore to allCodeCoverageReport.xml by default; we restore to the same location in following tasks
      - run: mkdir -p build/reports/jacoco/allCodeCoverageReport/
      - run: mv allCodeCoverageReport.xml build/reports/jacoco/allCodeCoverageReport/
      - run: ./gradlew sonarqube $CI_GRADLE_ARG_PROPERTIES
        env:
          ORG_GRADLE_PROJECT_SONAR_LOGIN: ${{ secrets.SONAR_TOKEN }}

# Notify the channel about sonarqube failures
  notify:
    name: Notify matrix
    runs-on: ubuntu-latest
    needs:
      - sonarqube
      - codecov-units
    if: always() && (needs.sonarqube.result != 'success' || needs.codecov-units.result != 'success') 
    steps:
      - uses: michaelkaye/matrix-hookshot-action@v1.0.0
        with:
          github_token: ${{ secrets.GITHUB_TOKEN }}
          hookshot_url: ${{ secrets.ELEMENT_ANDROID_HOOKSHOT_URL }}
          text_template: "Sonarqube run (on ${{ github.ref }}): {{#each job_statuses }}{{#with this }}{{#if completed }}  {{name}} {{conclusion}} at {{completed_at}}, {{/if}}{{/with}}{{/each}}"
          html_template: "Sonarqube run (on ${{ github.ref }}): {{#each job_statuses }}{{#with this }}{{#if completed }}<br />{{icon conclusion}} {{name}} <font color='{{color conclusion}}'>{{conclusion}} at {{completed_at}} <a href=\"{{html_url}}\">[details]</a></font>{{/if}}{{/with}}{{/each}}"
