name: Downstream Codegen Tests
on:
  repository_dispatch:
    types: [ run-codegen-command ]
  pull_request:
    paths:
    - 'pkg/codegen/**'
    - '!pkg/codegen/docs/**'
    - '.github/workflows/run-codegen-test.yml'

env:
  GITHUB_TOKEN: ${{ secrets.GITHUB_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 Downstream Codegen Tests [Here][1]

            [1]: ${{ steps.vars.outputs.run-url }}
  downstream-test:
    name: Test ${{ matrix.provider }} Downstream
    if: github.event_name == 'repository_dispatch' || github.event.pull_request.head.repo.full_name == github.repository
    runs-on: ubuntu-latest
    strategy:
      matrix:
        provider: ["aws", "gcp", "azure", "azuread", "random", "kubernetes", "azure-native"]
      fail-fast: false
    steps:
      - name: Install Go
        uses: actions/setup-go@v1
        with:
          go-version: 1.16.x
      - name: Install Node.js
        uses: actions/setup-node@v1
        with:
          node-version: '14.x'
      - name: Install Python
        uses: actions/setup-python@v1
        with:
          python-version: 3.9.x
      - 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:
          ref: ${{ env.PR_COMMIT_SHA }}
      - name: Test Downstream
        uses: pulumi/action-test-provider-downstream@releases/v7
        env:
          GOPROXY: "https://proxy.golang.org"
        with:
          replacements: github.com/pulumi/pulumi/pkg/v3=pulumi/pkg,github.com/pulumi/pulumi/sdk/v3=pulumi/sdk
          downstream-name: pulumi-${{ matrix.provider }}
          downstream-url: https://github.com/pulumi/pulumi-${{ matrix.provider }}
          pulumi-bot-token: ${{ secrets.PULUMI_BOT_TOKEN }}
          github-actions-token: ${{ secrets.GITHUB_TOKEN }}
          use-provider-dir: true
          issue-number: ${{ github.event.client_payload.github.payload.issue.number }}
