name: Pre-Commit formatter

on:
  push:
    branches:
      - precommit/**

env:
  JAVA_VERSION: "21"
  MAVEN_PASSWORD: ${{ secrets.NEXUS_PASSWORD }}
  MAVEN_USERNAME: ${{ secrets.NEXUS_USERNAME }}

jobs:
  format-code:
    name: "Reformat code"
    runs-on: ubuntu-latest
    if: contains(github.event.head_commit.message, '[reformat code]')
    steps:
      - uses: actions/checkout@v4
      - uses: Alfresco/alfresco-build-tools/.github/actions/get-build-info@v8.16.0
      - uses: Alfresco/alfresco-build-tools/.github/actions/free-hosted-runner-disk-space@v8.16.0
      - uses: Alfresco/alfresco-build-tools/.github/actions/setup-java-build@v8.16.0
        with:
          java-version: ${{ env.JAVA_VERSION }}
      - name: Set up Python ${{ inputs.python-version }}
        uses: actions/setup-python@42375524e23c412d93fb67b49958b491fce71c38 # v5.4.0
        with:
          python-version: "3.9"
      - uses: pre-commit/action@2c7b3805fd2a0fd8c1884dcaebf91fc102a13ecd # v3.0.1
        continue-on-error: true
        with:
          extra_args: --all-files
      - name: Update secrets baseline
        run: pip install detect-secrets && detect-secrets scan --baseline .secrets.baseline
      - uses: Alfresco/alfresco-build-tools/.github/actions/git-commit-changes@v8.16.0
        with:
          username: ${{ secrets.BOT_GITHUB_USERNAME }}
          add-options: -u
          commit-message: "Apply Pre-Commit code formatting"
          skip-if-no-changes: true
      - name: Push changes
        run: git push
