name: Test Docs Generation

on:
  workflow_call:
    inputs:
      ref:
        required: true
        description: "GitHub ref to use"
        type: string
      pull_request_number:
        required: true
        description: "Pull request number"
        type: string
      pull_request_author:
        required: true
        description: "Pull request author, as a login name"
        type: string

env:
  GITHUB_TOKEN: ${{ secrets.PULUMI_BOT_TOKEN }}
  DOTNET_ROLL_FORWARD: "Major"

permissions:
  contents: read

jobs:
  matrix:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
        with:
          ref: ${{ inputs.ref }}
      - name: build matrix
        id: matrix
        run: |
          echo "::group::Version set variable"
          VERSION_SET=$(./scripts/get-job-matrix.py \
            generate-version-set \
            --version-set current
          )
          echo "::endgroup::"

          echo "::group::Version set"
          echo "$VERSION_SET" | yq -P '.'
          echo "::endgroup::"

          echo "::group::Set outputs"
          ./.github/scripts/set-output version-set "${VERSION_SET}"
          echo "::endgroup::"
    outputs:
      version-set: "${{ fromJson(steps.matrix.outputs.version-set) }}"

  aws:
    name: Resource Docs
    needs: [matrix]
    # Verify that the event is not triggered by a fork since forks cannot
    # access secrets other than the default GITHUB_TOKEN. Specifically,
    # this workflow relies on the secret PULUMI_BOT_TOKEN to create a
    # draft PR in the docs repo.
    env:
      GOPATH: ${{ github.workspace }}
    runs-on: ubuntu-latest
    steps:
      - name: Install Go
        uses: actions/setup-go@v2
        with:
          go-version: ${{ fromJson(needs.matrix.outputs.version-set).go }}
      - name: Install Node.js
        uses: actions/setup-node@v1
        with:
          node-version: ${{ fromJson(needs.matrix.outputs.version-set).nodejs }}
      - name: Install Python
        uses: actions/setup-python@v3
        with:
          python-version: ${{ fromJson(needs.matrix.outputs.version-set).python }}
      - name: Set up DotNet
        uses: actions/setup-dotnet@v4
        with:
          dotnet-version: ${{ fromJson(needs.matrix.outputs.version-set).dotnet }}
          dotnet-quality: ga
      - name: Install Pulumi CLI
        uses: pulumi/action-install-pulumi-cli@v1.0.1
      - name: Check out source code
        uses: actions/checkout@v2
        with:
          path: pulumi
          ref: ${{ inputs.ref }}
          token: ${{ secrets.PULUMI_BOT_TOKEN }}
      - name: Check out pulumi-aws
        uses: actions/checkout@v2
        with:
          repository: pulumi/pulumi-aws
          path: pulumi-aws
          token: ${{ secrets.PULUMI_BOT_TOKEN }}
      - name: Check out pulumi-kubernetes
        uses: actions/checkout@v2
        with:
          repository: pulumi/pulumi-kubernetes
          path: pulumi-kubernetes
          token: ${{ secrets.PULUMI_BOT_TOKEN }}
      - name: Check out docs
        uses: actions/checkout@v2
        with:
          # Use the PAT and not the default GITHUB_TOKEN since we want to create a branch
          # in this workflow and push it to a remote that is NOT the current repo, i.e. pulumi/pulumi.
          token: ${{ secrets.PULUMI_BOT_TOKEN }}
          repository: pulumi/docs
          path: docs
      - name: Check out registry
        uses: actions/checkout@v2
        with:
          repository: pulumi/registry
          path: registry


      - name: Regenerate resource docs
        id: regenerate-resource-docs
        run: |
          PR_NUMBER=$(jq --raw-output .pull_request.number "$GITHUB_EVENT_PATH")
          BRANCH_NAME="${GITHUB_ACTOR}/${PR_NUMBER}-test-generator-changes"

          pushd registry/tools/resourcedocsgen

          go mod edit -replace github.com/pulumi/pulumi/pkg/v3=../../../pulumi/pkg
          go mod edit -replace github.com/pulumi/pulumi/sdk/v3=../../../pulumi/sdk

          go mod tidy

          if [ -z "${GOPATH:-}" ]; then
            echo "GOPATH is empty. Defaulting to ${HOME}/go"
            GOPATH="${HOME}/go"
          fi
          go build -o "${GOPATH}/bin/resourcedocsgen" .
          export PATH="${GOPATH}/bin/":$PATH

          popd

          # If generating docs for more providers here, be sure to update
          # the description of the draft PR that is opened in the next step.
          pushd docs

          ./scripts/gen_resource_docs.sh aws true v5.42.0
          ./scripts/gen_resource_docs.sh kubernetes true

          popd

          echo "branchName=${BRANCH_NAME}" >> "${GITHUB_OUTPUT}"
          echo "prNumber=${PR_NUMBER}" >> "${GITHUB_OUTPUT}"

      - name: Create draft docs PR
        uses: peter-evans/create-pull-request@v3
        with:
          draft: true
          # We use a repo:public scoped PAT instead of the implicitly provided GITHUB_TOKEN secret here
          # because we want the creation of the docs PR to trigger the `on: pull_request` workflow in the
          # docs repo. Using a fork repo to raise the PR would also cause the `on: pull_request` workflow
          # to trigger, but currently Pulumify in the docs repo does not run for forks, but we want it to
          # generate the preview link.
          token: ${{ secrets.PULUMI_BOT_TOKEN }}
          path: docs
          committer: Pulumi Bot <bot@pulumi.com>
          author: Pulumi Bot <bot@pulumi.com>
          commit-message: Regenerate resource docs
          title: Preview resource docs changes for pulumi/pulumi#${{ steps.regenerate-resource-docs.outputs.prNumber }}
          body: |
            This PR was auto-generated from pulumi/pulumi#${{ steps.regenerate-resource-docs.outputs.prNumber }}.
            By default, this PR contains regenerated docs for AWS and Kubernetes only.
            After review, this PR should be manually closed.
          # Assign the draft PR to the author of the current PR.
          assignees: ${{ inputs.pull_request_author }}
          branch: ${{ steps.regenerate-resource-docs.outputs.branchName }}