name: Rebase on Upstream

on:
  schedule:
    - cron: '53 0 * * *'
  workflow_dispatch:

jobs:
  rebase:
    runs-on: ubuntu-latest
    outputs:
      build-commit: ${{ steps.relocate.outputs.build-commit }}
    steps:
    - name: Checkout
      uses: actions/checkout@master
      with:
        fetch-depth: 0
        token: ${{ secrets.GH_PAT }}
    - name: Locate
      shell: bash
      run: |
        echo "HEAD_NOW=$(git rev-parse HEAD)" >> "$GITHUB_ENV"
    - name: Rebase
      uses: ytdl-org/ytdl-patched-rebase-upstream-action@master
      with:
        token: ${{ secrets.GH_PAT }}
        upstream: ytdl-org/youtube-dl
    - name: Relocate
      id: relocate
      shell: bash
      run: |
        NEW_HEAD=$(git rev-parse HEAD)
        [ "${{ env.HEAD_NOW }}" = "$NEW_HEAD" ] || echo "build-commit=$NEW_HEAD" >> "$GITHUB_OUTPUT"
        
  build:
    needs: rebase
    if: ${{ needs.rebase.outputs.build-commit }}
    uses: ./.github/workflows/build.yml
    with:
      build-commit: ${{ needs.rebase.outputs.build-commit }}
