name: Post-Release PR

permissions:
  # To create a PR
  contents: write
  pull-requests: write

on:
  workflow_call:
    inputs:
      ref:
        required: true
        description: "GitHub ref to use"
        type: string
      version:
        required: true
        description: "Version to produce"
        type: string
      branch_from_ref:
        required: false
        description: "Commit to branch from, if not the tag"
        type: string
      release-notes:
        required: true
        description: "Release notes to publish"
        type: string
      queue-merge:
        required: false
        default: false
        description: "Whether to queue the release for immediate merge"
        type: boolean

jobs:
  version-bump:
    name: version bump
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
        with:
          ref: ${{ inputs.ref }}
      - uses: actions/setup-go@v5
        with:
          go-version: '>=1.19.0' # decoupled from version sets, only used by changelog tool
      - name: Create PR
        env:
          PULUMI_VERSION: ${{ inputs.version }}
          RELEASE_NOTES: ${{ inputs.release-notes }}
          QUEUE_MERGE: ${{ inputs.queue-merge }}
          GITHUB_TOKEN: ${{ secrets.PULUMI_BOT_TOKEN }}
        run: |
          set -euo pipefail
          git switch --create "automation/release-v${PULUMI_VERSION}"

          echo -en "# Changelog\n\n${RELEASE_NOTES}\n\n$(tail -n+3 CHANGELOG.md)" > ./CHANGELOG.md

          git config user.name github-actions
          git config user.email github-actions@github.com

          rm ./changelog/pending/*.yaml || true
          git add -A

          git commit -m "chore: changelog for v${PULUMI_VERSION}"

          # Update go module dependencies
          (
            cd pkg
            go get -u "github.com/pulumi/pulumi/sdk/v3@v${PULUMI_VERSION}"
          )
          make tidy
          git add -A
          git commit -m "chore: post-release go.mod updates"

          git push -u origin HEAD

          # Note that the title of the PR is used in the on-push workflow.  If the title needs to
          # change here, please also check on-push.yml
          PR=$(gh pr create --title "Changelog and go.mod updates for v${PULUMI_VERSION}" --body "")

          if [ "${QUEUE_MERGE}" = "true" ]; then
            gh pr merge --auto --squash "${PR}"
          fi