name: sync-main-to-dev

# author: @ralfhandl

#
# This workflow creates PRs to update the dev branch with the latest changes from main
#

# run this on push to main
on:
  push:
    branches:
      - main

jobs:
  sync-branch:
    runs-on: ubuntu-latest
    steps:
      - name: Generate access token
        id: generate-token
        uses: actions/create-github-app-token@v2
        with:
          app-id: ${{ secrets.OAI_SPEC_PUBLISHER_APPID }}
          private-key: ${{ secrets.OAI_SPEC_PUBLISHER_PRIVATE_KEY }}

      - name: Checkout repository
        uses: actions/checkout@v5

      - name: Create pull request
        id: pull_request
        shell: bash
        run: |
          EXISTS=$(gh pr list --base $BASE --head $HEAD \
            --json number --jq '.[] | .number')
          if [ ! -z "$EXISTS" ]; then
            echo "PR #$EXISTS already wants to merge $HEAD into $BASE"
            exit 0
          fi

          gh pr create --base $BASE --head $HEAD \
            --label "Housekeeping" \
            --title "$BASE: update from $HEAD" \
            --body "Merge \`$HEAD\` into \`$BASE\`."
        env:
          GH_TOKEN: ${{ steps.generate-token.outputs.token }}
          HEAD: main
          BASE: dev
