name: Code quality analysis

on:
  push:
    branches:
      - main
      - renovate/**
  schedule:
    - cron: '31 7 * * 3'
  workflow_dispatch:

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

permissions:
  contents: read
  security-events: write

jobs:
  codeql:
    name: CodeQL
    runs-on: ubuntu-latest
    steps:
    - name: Checkout repository
      uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1

    - name: Set up JDK
      uses: actions/setup-java@387ac29b308b003ca37ba93a6cab5eb57c8f5f93 # v4.0.0
      with:
        distribution: temurin
        java-version: 18

    - name: Setup Gradle caching
      uses: gradle/gradle-build-action@982da8e78c05368c70dac0351bb82647a9e9a5d2 # v2.11.1
      with:
        gradle-home-cache-cleanup: true
        cache-read-only: true

    - name: Initialize CodeQL
      uses: github/codeql-action/init@0b21cf2492b6b02c465a3e5d7c473717ad7721ba # v3.23.1
      with:
        languages: java
        tools: latest
        queries: +security-extended

    - name: Build project
      shell: bash
      run: |
        ./gradlew assembleDebug assembleRelease

    - name: Perform CodeQL Analysis
      uses: github/codeql-action/analyze@0b21cf2492b6b02c465a3e5d7c473717ad7721ba # v3.23.1
      with:
        category: "/language:java"
  mobsfscan:
    name: MobSF
    runs-on: ubuntu-latest
    steps:
      - name: Checkout repository
        uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1

      - name: Set up JDK
        uses: actions/setup-java@387ac29b308b003ca37ba93a6cab5eb57c8f5f93 # v4.0.0
        with:
          distribution: temurin
          java-version: 18

      - name: Setup Gradle caching
        uses: gradle/gradle-build-action@982da8e78c05368c70dac0351bb82647a9e9a5d2 # v2.11.1
        with:
          gradle-home-cache-cleanup: true
          cache-read-only: true

      - name: Setup python
        uses: actions/setup-python@0a5c61591373683505ea898e09a3ea4f39ef2b9c # v5
        with:
          python-version: 3.8

      - name: Run mobsfscan
        uses: MobSF/mobsfscan@e29e85c36fa1aa950d7f1b1064345d94a70b2a28
        with:
          args: . --sarif --output results.sarif || true

      - name: Upload mobsfscan report
        uses: github/codeql-action/upload-sarif@0b21cf2492b6b02c465a3e5d7c473717ad7721ba # v3.23.1
        with:
          sarif_file: results.sarif
