name: Tag Registry Examples
on:
  workflow_dispatch:
    inputs:
      release-version:
        description: 'Registry Release Version'
        required: true
      snapshot-version:
        description: 'Next snapshot version'
        required: true
  workflow_call:
    inputs:
      release-version:
        required: true
        type: string
      snapshot-version:
        required: true
        type: string


jobs:
  tag-registry-examples:
    runs-on: ubuntu-22.04
    if: false # Registry Examples repository is archived, since we moved them to the Registry repository. This workflow can be later removed.
    steps:
      - name: Set Versions
        run: |
          set -x
          if [ -z "${{inputs.release-version}}" ] || [ -z "${{inputs.snapshot-version}}" ]
          then
            echo "Workflow Triggered via workflow_dispatch event. Setting Versions as ENV variables..."
            echo "RELEASE_VERSION=${{ github.event.inputs.release-version }}" >> $GITHUB_ENV
            echo "SNAPSHOT_VERSION=${{ github.event.inputs.snapshot-version }}" >> $GITHUB_ENV
          else
            echo "Workflow Triggered via workflow_call event. Setting Versions as ENV variables..."
            echo "RELEASE_VERSION=${{ inputs.release-version }}" >> $GITHUB_ENV
            echo "SNAPSHOT_VERSION=${{ inputs.snapshot-version }}" >> $GITHUB_ENV
          fi

      - name: Print Env variables
        run: |
          echo "Release Version: $RELEASE_VERSION"
          echo "Snapshot Version: $SNAPSHOT_VERSION"

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

      - name: Configure Git
        run: |
          git config --global user.name "apicurio-ci"
          git config --global user.email "apicurio.ci@gmail.com"

      - name: Checkout Apicurio Registry Tag ${{ env.RELEASE_VERSION }}
        uses: actions/checkout@v3
        with:
           ref: ${{ env.RELEASE_VERSION }}
           path: apicurio-registry

      - name: Build Apicurio Registry
        env:
          SKIP_TESTS: "true"
        run: cd apicurio-registry && make build-all

      - name: Checkout Apicurio Registry Examples
        uses: actions/checkout@v3
        with:
           repository: Apicurio/apicurio-registry-examples
           path: apicurio-registry-examples
           token: ${{ secrets.ACCESS_TOKEN }}

      - name: list files
        run: ls -lrt

      - name: Update Release Version ${{ env.RELEASE_VERSION }}
        run: |
          cd apicurio-registry-examples
          mvn versions:set -DnewVersion=${{ env.RELEASE_VERSION }} -DgenerateBackupPoms=false -DprocessAllModules=true
          mvn versions:set-property -Dproperty=apicurio-registry.version -DgenerateBackupPoms=false -DnewVersion=${{ env.RELEASE_VERSION }}

      - name: Build Registry Examples
        run: cd apicurio-registry-examples && ./mvnw clean install

      - name: Commit Release Version Changes
        run: |
          cd apicurio-registry-examples
          git add .
          git commit -m "Automated update to Release Version:: ${{ env.RELEASE_VERSION }}"
          git push origin main

      - name: Tag Examples
        run: |
          cd apicurio-registry-examples
          git tag -a -m "Tagging release ${{ env.RELEASE_VERSION }}" ${{ env.RELEASE_VERSION }}
          git push origin ${{ env.RELEASE_VERSION }}

      - name: Update Snapshot Version ${{ env.SNAPSHOT_VERSION }}
        run: |
          cd apicurio-registry-examples
          mvn versions:set -DnewVersion=${{ env.SNAPSHOT_VERSION }} -DgenerateBackupPoms=false -DprocessAllModules=true

      - name: Commit Snapshot Version Changes
        run: |
          cd apicurio-registry-examples
          git add .
          git commit -m "Automated update to next Snapshot Version: ${{ env.SNAPSHOT_VERSION }}"
          git push origin main

      - name: Google Chat Notification
        if: ${{ failure() }}
        uses: Co-qn/google-chat-notification@b9227d9daa4638c9782a5bd16c4abb86268127a1
        with:
          name: ${{ github.workflow }}
          url: ${{ secrets.GOOGLE_CHAT_WEBHOOK }}
          status: ${{ job.status }}
