name: 00-Check-Code-Convention
on:
  push:
    branches-ignore:
      - main
      - "tests/**"
  pull_request:
    branches:
      - main
      - develop
      - "release/**"
  workflow_dispatch:
    inputs:
      branch:
        description: 'Branch to test'
        type: string
        default: 'dev'

jobs:
  job1:
    name: Check coding convention
    runs-on: ubuntu-20.04
    steps:
    - name: Trigger
      run: echo "Triggered by ${{github.event_name}} event"
    - name: Check Branch Input
      run: |
            if [ "${{ github.event_name }}" == "workflow_dispatch" ]; then
              if [ -z "${{ github.event.inputs.branch }}" ]; then
                echo "Branch input is required for manual trigger."
                exit 1
              fi
            fi
    - name: Checkout
      uses: actions/checkout@v4.1.7
      with:
        ref: "${{ github.event_name == 'workflow_dispatch' && github.event.inputs.branch || github.ref }}"
    - name: Log Current Branch and Commit
      run: |
          echo "Current branch: $(git rev-parse --abbrev-ref HEAD)"
          echo "Current commit: $(git rev-parse HEAD)"
    - name: Install python3.11
      if: always()
      run: |
        sudo apt-get update
        sudo apt-get upgrade -y
        sudo apt-get install --no-install-recommends -y \
          software-properties-common \
          gpg \
          gpg-agent \
          curl
        sudo add-apt-repository ppa:deadsnakes/ppa -y
        sudo apt update
        sudo apt-get install --no-install-recommends -y python3.11
        python3.11 --version
        which python3.11
        sudo apt install python3.11-full
        curl -sS https://bootstrap.pypa.io/get-pip.py | sudo python3.11
    - name: Install commit check tools
      run: |
          echo "Installing pre-commit ..."
          python3.11 -m pip install pre-commit
          echo "Installing uncrustify 0.64 from source code  ..."
          sudo apt-get install --no-install-recommends -y\
            binutils ca-certificates git cmake make \
            gcc g++ binutils libc6-dev
          echo "Cloning Uncrustify repository..."
          git clone -b uncrustify-0.64 --single-branch https://github.com/uncrustify/uncrustify.git
          echo "Building and installing Uncrustify..."
          mkdir ./uncrustify/build && cd ./uncrustify/build
          cmake -D CMAKE_INSTALL_PREFIX=/usr/local -D CMAKE_BUILD_TYPE=RelWithDebInfo ../
          sudo make -j "$(nproc)"
          sudo make install
          echo "Uncrustify has been installed successfully!"
          cd ../../
          sudo cp ./tools/uncrustify.cfg ./uncrustify/uncrustify.cfg
          echo "Install clang-tidy and cppcheck ..."
          sudo apt install clang-tidy cppcheck
    - name: Run test
      run: |
        pre-commit install
        pre-commit run --all-files 2>&1 | tee CodingConventionTool.txt

    - name: Upload Result
      if: always()
      uses: actions/upload-artifact@v4.3.4
      with:
        name: CodingConventionResult
        path: CodingConventionTool.txt
        retention-days: 90
    - name: Check log file to set status of the job
      run: |
        keywords=("Failed")
        for keyword in "${keywords[@]}"; do
          if grep -q "$keyword" CodingConventionTool.txt; then
            echo "Keyword '$keyword' found in the file."
            exit 1
          else
            echo "Keyword '$keyword' not found in the file."
          fi
        done