#
# 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 Winget

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

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 Winget
    needs: version
    runs-on: windows-2022
    defaults:
      run:
        shell: powershell
    steps:
      - name: Create winget PR
        run: |
          Invoke-WebRequest https://aka.ms/wingetcreate/latest -OutFile wingetcreate.exe
          .\wingetcreate.exe update RedHat.Podman-Desktop -u $Env:URL_X64 $Env:URL_ARM64 -v $Env:VERSION -t $Env:TOKEN --submit
        env:
          TOKEN: ${{ secrets.PODMAN_DESKTOP_BOT_TOKEN }}
          VERSION: ${{ needs.version.outputs.desktopVersion }}
          URL_X64: ${{ format('https://github.com/containers/podman-desktop/releases/download/v{0}/podman-desktop-{0}-setup-x64.exe|x64', needs.version.outputs.desktopVersion) }}
          URL_ARM64: ${{ format('https://github.com/containers/podman-desktop/releases/download/v{0}/podman-desktop-{0}-setup-arm64.exe|arm64', needs.version.outputs.desktopVersion) }}
