name: Update OpenAPI
on:
  push:
    paths:
      - common/src/main/resources/META-INF/openapi.json
    branches: [ '2.6.x' ]

jobs:
  update-openapi:
    name: Update OpenAPI
    runs-on: ubuntu-22.04
    if: github.repository_owner == 'Apicurio'
    steps:
      - name: Apicurio Registry Checkout
        run: |
          mkdir registry
          cd registry
          git init
          git config --global user.name "apicurio-ci"
          git config --global user.email "apicurio.ci@gmail.com"
          git remote add origin "https://apicurio-ci:${{ secrets.ACCESS_TOKEN }}@github.com/Apicurio/apicurio-registry.git"
          git fetch
          git checkout 2.6.x
          git branch --set-upstream-to=origin/2.6.x
          git pull

      - name: Set up JDK 17
        uses: actions/setup-java@v3
        with:
          java-version: '17'
          distribution: 'temurin'

      - name: Set up Maven
        uses: stCarolas/setup-maven@v4
        with:
          maven-version: '3.6.3'

      - name: Copy openapi.json
        run: |
          cd registry
          cp ./common/src/main/resources/META-INF/openapi.json ./app/src/main/resources-unfiltered/META-INF/resources/api-specifications/registry/v2/openapi.json

      - name: Commit Release Version Change
        run: |
          cd registry
          git add ./app/src/main/resources-unfiltered/META-INF/resources/api-specifications/registry/v2/openapi.json
          git commit -m "Automatically updated the core v2 API OpenAPI definition."
          git push

  validate:
    needs: update-openapi
    uses: ./.github/workflows/validate-openapi.yaml
