name: Update Website
on:
  workflow_dispatch: {}
  release:
    types: [released]

jobs:
  update-website:
    if: github.repository_owner == 'Apicurio'
    runs-on: ubuntu-22.04
    steps:

      - name: Check Prerequisites
        if: github.event_name != 'workflow_dispatch' && !startsWith(github.event.release.tag_name, '2.6.')
        run: |
          exit 1

      - name: Apicurio Website Checkout
        run: |
          mkdir website
          cd website
          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.github.io.git"
          git fetch
          git checkout main
          git branch --set-upstream-to=origin/main
          git pull

      - name: Fetch required Details
        run: echo "RELEASE_VERSION=$(curl https://api.github.com/repos/apicurio/apicurio-registry/releases/latest | jq '.name' | sed 's/"//g')" >> $GITHUB_ENV

      - name: Updating Project Website for Release ${{ env.RELEASE_VERSION }}
        run: |
          cd website
          echo "********** Replacing Existing Latest Release JSON File **********"
          cd _data/registry-v2
          rm latestRelease.json
          touch latestRelease.json && curl https://api.github.com/repos/apicurio/apicurio-registry/releases/latest > latestRelease.json

          echo "********** Saving a copy in the archive **********"
          cp latestRelease.json releases/${RELEASE_VERSION}.json

      - name: Commit Project Website Changes
        run: |
          cd website
          git add .
          git commit -m "Automated Update For Apicurio Registry Release Version: ${{ env.RELEASE_VERSION }}"
          git push

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