name: Code Scanning

on:
  push:
  pull_request:
    branches: [ master ]

env:
  MAKEFLAGS: -j8

permissions:
  actions: read
  contents: read
  security-events: write

jobs:

  code-ql:
    runs-on: ubuntu-latest
    steps:
    - name: Install Dependencies
      uses: awalsh128/cache-apt-pkgs-action@v1
      with:
        packages: >
          libasound2-dev
          libbluetooth-dev
          libbsd-dev
          libdbus-1-dev
          libfdk-aac-dev
          libglib2.0-dev
          libmp3lame-dev
          libmpg123-dev
          libncurses5-dev
          libreadline-dev
          libsbc-dev
          libspandsp-dev
    - uses: actions/checkout@v3
    - name: Initialize CodeQL
      uses: github/codeql-action/init@v2
      with:
        languages: cpp
        queries: security-and-quality
    - name: Create Build Environment
      run: |
        mkdir -p ${{ github.workspace }}/{build,m4}
        autoreconf --install
    - name: Configure GNU Automake
      working-directory: ${{ github.workspace }}/build
      run: |
        ${{ github.workspace }}/configure \
          --enable-aac \
          --enable-faststream \
          --enable-mp3lame \
          --enable-mpg123 \
          --enable-msbc \
          --enable-ofono \
          --enable-upower \
          --enable-aplay \
          --enable-cli \
          --enable-rfcomm \
          --enable-a2dpconf \
          --enable-hcitop
    - name: Build
      working-directory: ${{ github.workspace }}/build
      run: make
    - name: Perform CodeQL Analysis
      uses: github/codeql-action/analyze@v2

  doc8-lint:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v3
    - name: Run reStructuredText Linter
      uses: deep-entertainment/doc8-action@v4
      with:
        ignorePaths: ${{ github.workspace }}/doc/bluealsa-api.txt
        scanPaths: ${{ github.workspace }}

  include-what-you-use:
    runs-on: ubuntu-latest
    steps:
    - name: Install Dependencies
      uses: awalsh128/cache-apt-pkgs-action@v1
      with:
        # XXX: iwyu package depends on clang-14, but is built with clang-13
        packages: >
          bear
          check
          iwyu libclang-13-dev
          jq
          libasound2-dev
          libbluetooth-dev
          libbsd-dev
          libdbus-1-dev
          libfdk-aac-dev
          libglib2.0-dev
          libmp3lame-dev
          libmpg123-dev
          libncurses5-dev
          libreadline-dev
          libsbc-dev
          libspandsp-dev
    - uses: actions/checkout@v3
    - name: Create Build Environment
      run: |
        mkdir -p ${{ github.workspace }}/{build,m4}
        autoreconf --install
    - name: Configure GNU Automake
      working-directory: ${{ github.workspace }}/build
      run: |
        ${{ github.workspace }}/configure \
          --enable-aac \
          --enable-debug \
          --enable-debug-time \
          --enable-faststream \
          --enable-mp3lame \
          --enable-mpg123 \
          --enable-msbc \
          --enable-ofono \
          --enable-upower \
          --enable-aplay \
          --enable-cli \
          --enable-rfcomm \
          --enable-a2dpconf \
          --enable-hcitop \
          --enable-test
    - name: Build
      working-directory: ${{ github.workspace }}/build
      run: bear -- make check TESTS=
    - name: Run IWYU Check
      run: |
        jq 'del(.[] | select(.file | endswith("-iface.c")))' \
          ${{ github.workspace }}/build/compile_commands.json > tmp.json \
          && mv tmp.json ${{ github.workspace }}/build/compile_commands.json
        iwyu_tool -p ${{ github.workspace }}/build -- \
          -Xiwyu --mapping_file=${{ github.workspace }}/.github/iwyu.imp \
          -Xiwyu --keep=*/config.h \
          -Xiwyu --no_fwd_decls

  markdown-lint:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v3
    - name: Run Markdown Linter
      uses: nosborn/github-action-markdown-cli@v3
      with:
        files: ${{ github.workspace }}

  shellcheck:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v3
      with:
        fetch-depth: 0
    - name: Run ShellCheck Scan
      uses: redhat-plumbers-in-action/differential-shellcheck@v4
      with:
        token: ${{ secrets.GITHUB_TOKEN }}

  spellcheck:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v3
    - name: Run Spell Check
      uses: rojopolis/spellcheck-github-actions@master
      with:
        config_path: .github/spellcheck.yaml
