#
# Copyright (C) 2022 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 Chocolatey

on:
  workflow_dispatch:
    inputs:
      version:
        description: 'release version like 0.0.6'
        required: true
      force:
        description: 'Force the push of the package'
        required: false
        default: 'false'
  repository_dispatch:
    types: [ publish-to-chocolatey ]

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}

  winget-bump:
    name: Update Chocolatey
    needs: version
    runs-on: windows-2022
    defaults:
      run:
        shell: powershell
    steps:
      - uses: actions/checkout@v4
      - name: Install the Chocolatey Automatic Package Updater Module
        run: choco install au -y
      - name: Build the Podman Desktop chocolatey Package by making sure we're using the latest release
        working-directory: ./.chocolatey/podman-desktop
        run: |
          if ($env:automaticUpdateForce -eq 'true') {
              $au_Force = $true; ./update.ps1
          } else {
              ./update.ps1
          }
          cat podman-desktop.nuspec
        env:
          au_Push: ${{ github.event.inputs.force }}
          automaticUpdateForce: ${{ github.event.inputs.force }}
          VERSION: ${{ needs.version.outputs.desktopVersion }}
      - 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-chocolatey-for-${{ needs.version.outputs.desktopVersion }}"
          git checkout -b $bumpedBranchName
          git add .chocolatey
          git commit --signoff -m "chore: Update chocolatey packages for ${{ needs.version.outputs.desktopVersion }}"
          git push origin "$bumpedBranchName"
          New-Item -Path . -Name "pr-title" -ItemType "file" -Value "${{ needs.version.outputs.desktopVersion }} has been released`n`nUpdate Chocolatey packages for ${{ needs.version.outputs.desktopVersion }}"
          $pullRequestUrl = gh pr create --title "chore: Update Chocolatey package to ${{ needs.version.outputs.desktopVersion }}" --body-file ./pr-title --head "$bumpedBranchName" --base "main" -R "containers/podman-desktop"
          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 }}
      - name: Publish Package to Chocolatey
        run: |
          if ($env:automaticUpdateForce -eq 'true') {
              $au_Force = $true; Push-Package
          } else {
              Push-Package
          }
        working-directory: ./.chocolatey/podman-desktop
        env:
          api_key: ${{ secrets.CHOCOLATEY_API_KEY }}
          automaticUpdateForce: ${{ github.event.inputs.force }}
