# REF: https://github.com/JamesRobionyRogers/GitHub-to-GitBucket-Action/blob/47a44e9/.github/workflows/push-to-gitbucket.yml
name: Mirror Repository

on:
  workflow_dispatch:
  push:
    branches:
      - master

concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true

jobs:
  mirror:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4
        with:
          fetch-depth: 0
          fetch-tags: true

      - name: Fetch all branches
        run: |
          git fetch --all
          git fetch --tags

      - name: List branches
        run: |
          git branch -a

      # Enables tracking of remote branches ensuring all branches are pushed to mirror repo
      - name: Track remote branches
        run: |
          for branch in $(git branch -r | grep -v '\->'); do
            local_branch=${branch#origin/}
            git branch --track "$local_branch" "$branch" || true
          done

      - name: Push to mirror repo (GitLab)
        env:
          MIRROR_URL: "gitlab.com/null2264/yokai.git"
          MIRROR_USERNAME: ${{ secrets.MIRROR_USERNAME }}
          MIRROR_AUTH: ${{ secrets.MIRROR_AUTH }}
        run: |
          git config --global user.name "GitHub Actions"
          git config --global user.email "actions@github.com"

          git remote add mirror https://$MIRROR_USERNAME:$MIRROR_AUTH@$MIRROR_URL
          git push --mirror -v mirror || true
