name: Codestyle-Check

on:
  pull_request:
    branches:
      - develop
      - 'release/*'

jobs:
  pre-commit:
    name: Pre Commit
    if: ${{ github.repository_owner == 'PaddlePaddle' }}
    runs-on: ubuntu-latest
    env:
      PR_ID: ${{ github.event.pull_request.number }}
      BRANCH: ${{ github.event.pull_request.base.ref }}

    steps:
      - name: Cleanup
        run: |
          rm -rf * .[^.]*

      - name: Checkout base repo
        uses: actions/checkout@v4
        with:
          ref: ${{ github.event.pull_request.base.ref }}
          fetch-depth: 1000

      - name: Merge PR to test branch
        run: |
          git fetch origin pull/${PR_ID}/merge
          git checkout -b test FETCH_HEAD

      - name: Setup python3.10
        uses: actions/setup-python@v5
        with:
          python-version: '3.10'
          cache: 'pip'

      - name: Install dependencies
        run: |
          pip install pre-commit==4.2.0 cpplint==1.6.0 clang-format==13.0.0

      - name: Check pre-commit
        env:
          SKIP_CLANG_TIDY_CHECK: "ON"
        run: |
          set +e
          bash -x tools/codestyle/pre_commit.sh;EXCODE=$?
          exit $EXCODE
