name: Autobump

on:
  workflow_dispatch:
  schedule:
    - cron:  '0 20 * * *'
jobs:
  bump:
    runs-on: ubuntu-latest
    env:
      GIT_AUTHOR_NAME: "ci-robbot"
      GIT_AUTHOR_EMAIL: robot@c3os.io
      GIT_COMMITTER_NAME: "ci-robbot"
      GIT_COMMITTER_EMAIL: robot@c3os.io
      WORK_BRANCH: bumps
      AUTO_GIT: true
      GIT_SIGNOFF: true
      LUET_NOLOCK: true
      REVBUMP_CHAR: "-"
      LUET_YES: true
      TOKEN: ${{ secrets.PAT_TOKEN }}
      GITHUB_TOKEN: ${{ secrets.PAT_TOKEN }}
      GITHUB_PRIVATE_KEY: "${{ secrets.BOT_PRIVATE_KEY }}"
      GITHUB_PUBLIC_KEY: "${{ secrets.BOT_PUBLIC_KEY }}"
    strategy:
      matrix:
        include:
          - upstream_branch: "main"
            fork: "git@github.com:ci-forks/packages.git"
            upstream_repo: "https://github.com/kairos-io/packages"
            name: "packages"
    steps:
      - env:
          HUB_ARGS: "-b ${{ matrix.upstream_branch }}"
          FORK_REPO: ${{ matrix.fork }}
          UPSTREAM_REPO: ${{ matrix.upstream_repo }}
          RESET_BRANCH: ${{ matrix.upstream_branch }}
        name: Autobump ${{matrix.name}}
        run: |
          sudo rm -rf /usr/local/bin/yq
          curl https://get.mocaccino.org/luet/get_luet_root.sh | sudo sh
          sudo luet install -y repository/mocaccino-extra
          sudo luet install -y system/luet-extensions system/luet-devkit utils/jq utils/yq vcs/hub utils/dasel
          mkdir -p $HOME/.ssh
          mkdir -p $HOME/.config
          echo "$GITHUB_PRIVATE_KEY" > $HOME/.ssh/id_rsa
          echo "$GITHUB_PUBLIC_KEY" > $HOME/.ssh/id_rsa.pub
          chmod 700 ~/.ssh
          chmod 600 ~/.ssh/id_rsa
          chmod 600 ~/.ssh/id_rsa.pub

          git clone $FORK_REPO fork
          cd fork
          git remote add upstream $UPSTREAM_REPO
          git fetch --all
          git reset --hard upstream/${RESET_BRANCH}
          git push -fv
          echo "Removing working branch if present"
          git branch -D $WORK_BRANCH || true

          git checkout -b $WORK_BRANCH
          git reset --hard upstream/${RESET_BRANCH}
          git push -fv -u origin $WORK_BRANCH
          export TREE_DIR=$PWD/packages
          
          luet autobump-github
