#
# Copyright (C) 2023 Red Hat, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# SPDX-License-Identifier: Apache-2.0

name: Publish update to podman.io website

on:
  workflow_dispatch:
    inputs:
      version:
        description: 'release version like 1.1.0'
        required: true
  repository_dispatch:
    types: [ publish-to-podman_io ]

jobs:

  version:
    name: Extracting version
    runs-on: ubuntu-24.04
    outputs:
      desktopVersion: ${{ steps.VERSION.outputs.desktopVersion}}

    steps:
      - name: set version
        id: VERSION
        run: |
          version=""
          if [ "${{ github.event_name }}" == "repository_dispatch" ]
          then
            version="${{ github.event.client_payload.version }}"
          else
            version="${{ github.event.inputs.version }}"
          fi
          # strip out the prefix v if it's there
          if [[ $version == v* ]]; then
            version="${version:1}"
          fi
          echo "desktopVersion=$version" >> ${GITHUB_OUTPUT}
      - uses: actions/checkout@v4
        with:
          repository: containers/podman.io
          ref: refs/heads/main
          token: ${{ secrets.PODMAN_DESKTOP_BOT_TOKEN }}


      - name: Replace the content of the file
        run: |
          # Replace the version in static/data/global.ts file (replace the xxx value)
          # export const LATEST_DESKTOP_VERSION = 'xxx';
          sed -i "s/export const LATEST_DESKTOP_VERSION = '.*';/export const LATEST_DESKTOP_VERSION = '${{ steps.VERSION.outputs.desktopVersion }}';/g" static/data/global.ts
          # Replace the version of the blog post
          # export const LATEST_DESKTOP_DOWNLOAD_URL = 'https://podman-desktop.io/blog/podman-desktop-release-xxx';
          # need to strip out the last digit after the last . to get the blog post version (so for example 1.4.1 becomes 1.4)
          blogPostVersion=$(echo ${{ steps.VERSION.outputs.desktopVersion }} | sed 's/\.[^.]*$//')
          sed -i "s/export const LATEST_DESKTOP_DOWNLOAD_URL = '.*';/export const LATEST_DESKTOP_DOWNLOAD_URL = 'https:\/\/podman-desktop.io\/blog\/podman-desktop-release-$blogPostVersion';/g" static/data/global.ts
          echo "New content of the file:"
          cat static/data/global.ts

      - name: Create the PR to bump the version in the main branch
        run: |
          git config --local user.name ${{ github.actor }}
          git config --local user.email "fbenoit@redhat.com"
          bumpedBranchName="update-podman-desktop-io-for-v${{ steps.VERSION.outputs.desktopVersion }}"
          echo "branch name: $bumpedBranchName"
          git checkout -b $bumpedBranchName
          echo "adding the file to the commit"
          git add static/data/global.ts
          echo "commiting the file"
          git commit --signoff -m "chore: Update Podman Desktop version to v${{ steps.VERSION.outputs.desktopVersion }}"
          git remote -v
          git remote add podman-desktop-bot https://github.com/podman-desktop-bot/podman.io
          git push podman-desktop-bot "+$bumpedBranchName"
          printf "Podman Desktop v${{ steps.VERSION.outputs.desktopVersion }} has been released 🎉\nUpdate website for v${{ steps.VERSION.outputs.desktopVersion }}" > ./pr-title
          pullRequestUrl=$(gh pr create --title "chore:  Update Podman Desktop version to v${{ steps.VERSION.outputs.desktopVersion }}" --body-file ./pr-title --head "podman-desktop-bot:$bumpedBranchName" --base "main" -R "containers/podman.io")
          echo "📢 Pull request created: $pullRequestUrl"
          echo "➡️ Flag the PR as being ready for review"
          gh pr ready "$pullRequestUrl"
          echo "🔅 Mark the PR as being ok to be merged automatically"
          gh pr merge "$pullRequestUrl" --auto --rebase
        env:
          GH_TOKEN: ${{ secrets.PODMAN_DESKTOP_BOT_TOKEN }}
          GITHUB_TOKEN: ${{ secrets.PODMAN_DESKTOP_BOT_TOKEN }}
