name: release-updates
on:
  release:
    types:
      - published

permissions:
  contents: write
  pull-requests: write

env:
  LABEL_VERSION: "v2"

jobs:
  release_updates:
    runs-on: ubuntu-24.04
    if: contains(github.ref, 'v2.')
    environment: release-updates-static-web-server
    steps:
      - name: Checkout
        uses: actions/checkout@v6
        with:
          ref: master

      - name: Prepare
        shell: bash
        run: |
          echo "SERVER_VERSION=${GITHUB_REF#refs/tags/}" >> $GITHUB_ENV
          echo "version is: ${{ env.SERVER_VERSION }}"
          echo "PR_BRANCH_NAME=release-docs-${{ env.SERVER_VERSION }}-$(date -u '+%d-%m-%Y')" >> $GITHUB_ENV

      - name: Dispatch docs update event
        uses: peter-evans/repository-dispatch@v4
        with:
          token: ${{ secrets.DOCS_TOKEN }}
          repository: static-web-server/docs
          event-type: event-docs-v2-update
          client-payload: '{"ref": "${{ github.ref }}", "sha": "${{ github.sha }}"}'

      - name: Post release installer updates
        shell: bash
        env:
          UPLOAD_CHECKSUM: true
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          scripts/ci/post_release_updates.sh

      - name: Commit post release installer updates
        shell: bash
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          git config --global user.name 'github-actions[bot]'
          git config --global user.email '41898282+github-actions[bot]@users.noreply.github.com'

          git add scripts/installer.sh

          git commit --verbose -m "docs: ${{ env.SERVER_VERSION }} [skip ci]"
          git checkout -b ${{ env.PR_BRANCH_NAME }}
          git push --set-upstream origin --verbose ${{ env.PR_BRANCH_NAME }}

      - name: Create pull request for release installer updates
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          gh pr create \
            --title "Post release installer updates ${{ env.SERVER_VERSION }}" \
            --body "An automated PR containing documentation and script installer updates corresponding to the current release \`${{ env.SERVER_VERSION }}\`.<br>See https://static-web-server.net/download-and-install/ for more details." \
            --base "2.x" \
            --head "${{ env.PR_BRANCH_NAME }}" \
            --label "${{ env.LABEL_VERSION }}" \
            --label "enhancement" \
            --label "documentation" \
            --label "automated" \
            --draft
