name: Increment Build Number

on:
  pull_request:
    branches:
      - master
      - '*-develop'
    types:
      - closed
    paths:
      - 'src/**.php'

jobs:
  incremental-build-number:
    if: github.event.pull_request.merged == true
    name: Incremental Build Number
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v3
        with:
          ref: ${{ github.base_ref }}

      - name: Setup PHP
        uses: sunxyw/workflows/setup-environment@main
        with:
          php-version: 8.0
          php-extensions: swoole, posix, json
          operating-system: ubuntu-latest
          use-cache: true

      - name: Prepare Doxygen
        id: prepare-doxygen
        continue-on-error: true
        run: bin/prepare-doxygen before

      - name: Generate Doxygen
        if: steps.prepare-doxygen.outcome == 'success'
        uses: mattnotmitt/doxygen-action@v1.9.5

      - name: Finishing Doxygen
        if: steps.prepare-doxygen.outcome == 'success'
        run: bin/prepare-doxygen after

      - name: Commit API Docs
        uses: stefanzweifel/git-auto-commit-action@v4
        with:
          commit_message: update api docs
          branch: ${{ github.base_ref }}

      - name: Increment build number
        id: increment-build-number
        run: |
          sed -i -r 's/(.*)(\VERSION_ID\s=\s)([0-9]+)(.*)/echo "\1\2$((\3+1))\4"/ge' src/ZM/Framework.php
          BUILD_ID=$(cat src/ZM/Framework.php | grep "VERSION_ID = " | sed 's/[^0-9]//g')
          echo "::set-output name=build_id::$BUILD_ID"

      - name: Commit build number
        uses: stefanzweifel/git-auto-commit-action@v4
        with:
          commit_message: increment build number (build ${{ steps.increment-build-number.outputs.build_id }})
          branch: ${{ github.base_ref }}
