name: Get the latest mantle release for branch
on:
  workflow_dispatch:
  push:
    branches:
      - main
  schedule:
    - cron:  '0 7 * * 1'

jobs:
  get-mantle-release:
    strategy:
      matrix:
        branch: [main,alpha,beta,stable,lts,lts-old]
      fail-fast: false
    runs-on: ubuntu-latest
    steps:
      - name: Figure out branch
        id: figure-out-branch
        run: |
          set -euo pipefail # The line with major=$(curl | awk) requires pipefail for error handling
          skip=0
          branch=''
          if [ ${{ matrix.branch }} = "main" ]; then
            branch='main'
          elif [[ ${{ matrix.branch }} = 'lts-old' ]]; then
            curl -fsSLO --retry-delay 1 --retry 60 --retry-connrefused --retry-max-time 60 --connect-timeout 20 'https://lts.release.flatcar-linux.net/lts-info'
            if [[ $(grep -e ':supported' lts-info | wc -l) -le 1 ]]; then
              # Only one supported LTS, skip this workflow run
              # as 'lts' matrix branch will handle updating the only
              # supported LTS.
              skip=1
            else
              major=$(grep -e ':supported' lts-info | sort -V | head -n 1 | awk -F: '{print $1}')
              branch="flatcar-${major}"
              # Drop this corner case when 2605 is not supported.
              if [[ ${major} -eq 2605 ]]; then
                branch='flatcar-lts-2605'
              fi
            fi
            rm -f lts-info
          else
            major=$(curl -fsSL --retry-delay 1 --retry 60 --retry-connrefused --retry-max-time 60 --connect-timeout 20 https://${{ matrix.branch }}.release.flatcar-linux.net/amd64-usr/current/version.txt | awk -F= '/FLATCAR_BUILD=/{ print $2 }')
            branch="flatcar-${major}"
          fi
          echo "BRANCH=${branch}" >>"${GITHUB_OUTPUT}"
          echo "SKIP=${skip}" >>"${GITHUB_OUTPUT}"
      - uses: actions/checkout@v4
        if: ${{ steps.figure-out-branch.outputs.SKIP == 0 }}
        with:
          token: ${{ secrets.BOT_PR_TOKEN }}
          ref: ${{ steps.figure-out-branch.outputs.BRANCH }}
      - name: Fetch latest mantle hash
        if: ${{ steps.figure-out-branch.outputs.SKIP == 0 }}
        id: fetch-latest-mantle
        run: |
          set -euo pipefail
          commit=$(git ls-remote  https://github.com/flatcar/mantle refs/heads/flatcar-master | cut -f1)
          echo "COMMIT=${commit}" >>"${GITHUB_OUTPUT}"
      - name: Try to apply patch
        if: ${{ steps.figure-out-branch.outputs.SKIP == 0 }}
        run: |
          set -euo pipefail
          set -x
          commit=${{ steps.fetch-latest-mantle.outputs.COMMIT }}
          if ! grep -q "ghcr.io/flatcar/mantle:git-${commit}" sdk_container/.repo/manifests/mantle-container; then
            echo "ghcr.io/flatcar/mantle:git-${commit}" > sdk_container/.repo/manifests/mantle-container
            git add sdk_container/.repo/manifests/mantle-container
          fi
      - name: Create pull request for branch
        if: ${{ steps.figure-out-branch.outputs.SKIP == 0 }}
        uses: peter-evans/create-pull-request@v6
        with:
          token: ${{ secrets.BOT_PR_TOKEN }}
          base: ${{ steps.figure-out-branch.outputs.BRANCH }}
          branch: mantle-update-${{ steps.figure-out-branch.outputs.BRANCH }}
          author: Flatcar Buildbot <buildbot@flatcar-linux.org>
          committer: Flatcar Buildbot <buildbot@flatcar-linux.org>
          title: Upgrade mantle container image to latest HEAD in ${{ steps.figure-out-branch.outputs.BRANCH }}
          commit-message: Update mantle container image to latest HEAD
          delete-branch: true
