name: Downstream Resource Docs Test
on:
  repository_dispatch:
    types: [ run-docs-gen-command ]
  pull_request:
    paths:
      - 'pkg/codegen/docs/**'
      - 'pkg/codegen/docs/docs.go'
      - 'pkg/codegen/docs/docs_test.go'
      - '.github/workflows/run-docs-generation.yml'

env:
  GITHUB_TOKEN: ${{ secrets.PULUMI_BOT_TOKEN }}
  PR_COMMIT_SHA: ${{ github.event.client_payload.pull_request.head.sha }}

jobs:
  comment-notification:
    if: github.event_name == 'repository_dispatch'
    runs-on: ubuntu-latest
    steps:
      - name: Create URL to the run output
        id: vars
        run: echo ::set-output name=run-url::https://github.com/$GITHUB_REPOSITORY/actions/runs/$GITHUB_RUN_ID
      - name: Update with Result
        uses: peter-evans/create-or-update-comment@v1
        with:
          token: ${{ secrets.GITHUB_TOKEN }}
          repository: ${{ github.event.client_payload.github.payload.repository.full_name }}
          issue-number: ${{ github.event.client_payload.github.payload.issue.number }}
          body: |
            Please view the results of the Docs Generation Tests [Here][1]

            [1]: ${{ steps.vars.outputs.run-url }}
  aws:
    name: Resource Docs
    # 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_GH_PAT_DOCS to create a
    # draft PR in the docs repo.
    if: github.event_name == 'repository_dispatch' || github.event.pull_request.head.repo.full_name == github.repository
    env:
      GOPATH: ${{ github.workspace }}
    runs-on: ubuntu-latest
    strategy:
      matrix:
        go-version: [ 1.16.x ]
        node-version: [ 14.x ]
    steps:
      - name: Install Go
        uses: actions/setup-go@v1
        with:
          go-version: ${{ matrix.go-version }}
      - name: Add GOBIN to PATH
        run: |
          mkdir -p "$(go env GOPATH)/bin"
          echo "$(go env GOPATH)/bin" >> $GITHUB_PATH
      - name: Install Node.js
        uses: actions/setup-node@v1
        with:
          node-version: ${{ matrix.node-version }}
      - name: Install Pulumi CLI
        uses: pulumi/action-install-pulumi-cli@v1.0.1
      - name: Install pulumictl
        uses: jaxxstorm/action-install-gh-release@v1.2.0
        with:
          repo: pulumi/pulumictl
      - name: Check out source code
        uses: actions/checkout@v2
        with:
          path: pulumi
          ref: ${{ env.PR_COMMIT_SHA }}
      - name: Check out pulumi-aws
        uses: actions/checkout@v2
        with:
          repository: pulumi/pulumi-aws
          path: pulumi-aws
      - name: Check out pulumi-kubernetes
        uses: actions/checkout@v2
        with:
          repository: pulumi/pulumi-kubernetes
          path: pulumi-kubernetes
      - 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_GH_PAT_DOCS }}
          repository: pulumi/docs
          path: docs

      - 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"

          # 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
          ./scripts/gen_resource_docs.sh kubernetes true
          popd

          echo "::set-output name=branchName::${BRANCH_NAME}"
          echo "::set-output name=prNumber::${PR_NUMBER}"

      - 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_GH_PAT_DOCS }}
          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.
          # Assign the draft PR to the author of the current PR.
          assignees: ${{ github.event.pull_request.user.login }}
          branch: ${{ steps.regenerate-resource-docs.outputs.branchName }}
