## Updates demo site (https://github.com/zetxek/adritian-demo)
## taken from https://stackoverflow.com/a/68213855/570087

name: PR demo site (on external PR)

permissions:
  contents: read
  id-token: write
  pages: write
  pull-requests: write
  checks: write
  statuses: write
  actions: write

## This will open a PR which will open a vercel preview URL in the demo site
on:
  pull_request_target:
    types: [labeled]
  workflow_dispatch:

jobs:
  update-demo:
    env:
      SOURCE_BRANCH_NAME: ${{ github.head_ref || github.ref_name }} # PR branch name
      PR_NUMBER: ${{ github.event.number }}
    runs-on: ubuntu-latest
    if: contains(github.event.pull_request.labels.*.name, 'safe to test')
    steps:
      - name: Checkout source repository (theme)
        uses: actions/checkout@v4
        with:
          path: theme-source
          ref: ${{ github.head_ref }}
          token: ${{ secrets.PRIVATE_TOKEN_GITHUB }}
      
      - name: Checkout demo repository
        uses: actions/checkout@v4
        with: 
          repository: zetxek/adritian-demo
          path: demo-repo
          token: ${{ secrets.PRIVATE_TOKEN_GITHUB }}
      
      - name: Dump github context
        run:   echo "$GITHUB_CONTEXT"
        shell: bash
        env:
          GITHUB_CONTEXT: ${{ toJson(github) }}

      - name: Send pull-request
        run: |
          LATEST_TAG=$(cd theme-source && git describe --tags --always)  
          LATEST_COMMIT=$(cd theme-source && git rev-parse HEAD)
          SOURCE_REPOSITORY="zetxek/adritian-free-hugo-theme"
          REPOSITORY="zetxek/adritian-demo"
          REPO_NAME=${{ github.event.pull_request.head.repo.full_name }}
          FOLDER="bin/$REPOSITORY"
          PR_URL="https://github.com/$SOURCE_REPOSITORY/pull/$PR_NUMBER"
          BRANCH_NAME="theme-update/update-theme-to-$LATEST_TAG"
          BASE_BRANCH="main"
          ASSIGNEE="zetxek"
          
          echo "Latest tag: $LATEST_TAG"
          echo "Latest commit: $LATEST_COMMIT"
          echo "PR URL: $PR_URL"

          git config --global --add --bool push.autoSetupRemote true

          # Clone the remote repository and change working directory to the
          # folder it was cloned to.
          git clone \
            --depth=1 \
            --branch=main \
            https://some-user:${{ secrets.PRIVATE_TOKEN_GITHUB }}@github.com/$REPOSITORY \
            $FOLDER

          cd $FOLDER

          # Setup the committers identity.
          git config user.email "actions@github.com"
          git config user.name "GitHub Actions - update theme module version"

          # Create a new feature branch for the changes.
          echo "Working branch: $BRANCH_NAME"
          git checkout -b $BRANCH_NAME
          
          # Commit the changes and push the feature branch to origin
          git config --global --add --bool push.autoSetupRemote true
          echo "Committing all changes."
          git add --all

          COMMIT_MSG_THEME='chore: update theme module version to `'"$LATEST_TAG"'`'
          echo 'Updating theme module: '"$COMMIT_MSG_THEME"
          git commit -am "$COMMIT_MSG_THEME" && git push --force || echo "No changes to theme"

          # Copy content from the checked-out theme source
          cp ../../theme-source/exampleSite/hugo.toml hugo.toml
          # Update URL
          sed -i -e "s/\"https\:\/\/www\.adrianmoreno\.info\"/\"https\:\/\/adritian-demo\.vercel\.app\/\"/g" hugo.toml

          COMMIT_MSG_CONFIG='chore: update config/content to `'"$LATEST_TAG"'` from https://github.com/zetxek/adritian-free-hugo-theme'
          echo "Committing content/config: $COMMIT_MSG_CONFIG"
          git commit -am "$COMMIT_MSG_CONFIG"  && git push --force || echo "No changes to config"

          echo "Pushing branch: $BRANCH_NAME"
          git push origin $BRANCH_NAME --force

          # Store the PAT in a file that can be accessed by the
          # GitHub CLI.
          echo "${{ secrets.PRIVATE_TOKEN_GITHUB }}" > token.txt

          # Authorize GitHub CLI for the current repository and
          # create a pull-requests containing the updates.
          echo "Logging in to GitHub CLI."
          gh auth login --with-token < token.txt

          # Check if the PR already exists - if there's no "number" returned, we default to empty string
          PR_EXISTS=$(gh pr list --state open --base $BASE_BRANCH --head $BRANCH_NAME --json number | jq '.[0].number // empty')
          echo "PR_EXISTS: $PR_EXISTS"
          # Check if there's a PR number. If the PR exists, update it (empty = it doesn't exist)
          if [ -n "$PR_EXISTS" ]; then
            echo "PR Exists. Updating pull-request..."
            gh pr view
            
            echo "✅ Pull-request created - done! "
            
          # Else, we create it
          else
            echo "✨PR Does not exist yet. Creating pull-request..."
            PR_TITLE='preview: update theme to `'$SOURCE_BRANCH_NAME'`'
            echo 'PR title: '$PR_TITLE
            PR_BODY="⚠️ The source PR is not merged yet - this is a preview PR.
            🤖 This automated PR updates the theme version to a PR in the source repo: $PR_URL.
            🔗 Triggered by https://github.com/zetxek/adritian-free-hugo-theme/actions/workflows/update-demo-pr.yml"
            echo "PR body: "$PR_BODY

            gh pr create \
              --title "$PR_TITLE" \
              --body "$PR_BODY" \
              --head $BRANCH_NAME \
              --base $BASE_BRANCH \
              --assignee $ASSIGNEE \
              --label preview

            echo "✅ Pull-request created - done! "
          fi

          