name: Sonar

on:
  workflow_run:
    workflows: [Build]
    types: [completed]

env:
  BUILD_TYPE: RelWithDebInfo

jobs:
  sonarscanner:
    runs-on: ubuntu-22.04
    if: github.event.workflow_run.conclusion == 'success'
    steps:
    - name: 'Install dependencies'
      run: |
        sudo apt-get update
        sudo apt-get install -y libboost-dev \
                                libeigen3-dev \
                                libepoxy-dev \
                                libavcodec-dev \
                                libavformat-dev \
                                libavutil-dev \
                                libswscale-dev \
                                libjpeg-dev \
                                libpng-dev \
                                libglu1-mesa-dev \
                                qtbase5-dev \
                                qtbase5-dev-tools \
                                libqt5opengl5-dev \
                                libfreetype6-dev \
                                libsdl2-dev \
                                libluajit-5.1-dev \
                                libicu-dev \
                                ninja-build \
                                gperf \
                                cmake

    - name: 'Checkout source code'
      uses: actions/checkout@v4
      with:
        submodules: true
        repository: ${{ github.event.workflow_run.head_repository.full_name }}
        ref: ${{ github.event.workflow_run.head_branch }}
        # Disabling shallow clone is recommended for improving relevancy of reporting
        fetch-depth: 0

    - name: 'Download PR details'
      if: github.event.workflow_run.event == 'pull_request'
      uses: actions/download-artifact@v4
      with:
        name: pr-details
        run-id: ${{ github.event.workflow_run.id }}
        github-token: ${{ secrets.GITHUB_TOKEN }}

    - name: 'Extract PR details'
      id: pr_details
      if: github.event.workflow_run.event == 'pull_request'
      run: |
        cat pr-details.txt >> "$GITHUB_OUTPUT"

    - name: 'Checkout base branch'
      if: github.event.workflow_run.event == 'pull_request'
      run: |
        git remote add upstream ${{ github.event.repository.clone_url }}
        git fetch upstream ${{ steps.pr_details.outputs.PR_BASE }}
        git checkout -B ${{ steps.pr_details.outputs.PR_BASE }} upstream/${{ steps.pr_details.outputs.PR_BASE }}
        git checkout ${{ github.event.workflow_run.head_branch }}
        yes no | git clean -fxd
        git reset --hard HEAD

    - name: 'Download autogenerated headers'
      uses: actions/download-artifact@v4
      with:
        name: celestia-autogenerated-headers
        run-id: ${{ github.event.workflow_run.id }}
        github-token: ${{ secrets.GITHUB_TOKEN }}

    - name: 'Unzip autogenerated headers'
      run: |
        unzip -n celestia-autogenerated-headers.zip && rm celestia-autogenerated-headers.zip

    - name: 'Install sonar'
      uses: SonarSource/sonarcloud-github-c-cpp@v3

    - name: 'Configure CMake'
      run: |
        cmake -B ${{github.workspace}}/build \
              -G Ninja                       \
              -DENABLE_GLES=ON               \
              -DENABLE_SPICE=OFF             \
              -DENABLE_TOOLS=OFF             \
              -DENABLE_TESTS=OFF             \
              -DENABLE_SDL=ON                \
              -DENABLE_GTK=OFF               \
              -DENABLE_QT5=ON                \
              -DENABLE_FFMPEG=ON             \
              -DENABLE_MINIAUDIO=ON          \
              -DUSE_ICU=ON                   \
              -DCMAKE_EXPORT_COMPILE_COMMANDS=ON

    - name: Analyze push
      if: github.event.workflow_run.event == 'push' && github.event.workflow_run.head_repository.full_name == github.event.repository.full_name
      run: |
        sonar-scanner \
          -Dsonar.scm.revision=${{ github.event.workflow_run.head_sha }} \
          -Dsonar.branch.name=${{ github.event.workflow_run.head_branch }}
      env:
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}

    - name: Analyze pull request
      if: github.event.workflow_run.event == 'pull_request'
      run: |
        sonar-scanner \
          -Dsonar.scm.revision=${{ github.event.workflow_run.head_sha }} \
          -Dsonar.pullrequest.key=${{ steps.pr_details.outputs.PR_KEY }} \
          -Dsonar.pullrequest.branch=${{ steps.pr_details.outputs.PR_BRANCH }} \
          -Dsonar.pullrequest.base=${{ steps.pr_details.outputs.PR_BASE }}
      env:
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
