on:
  issue_comment:
    types: [created]

jobs:
  check-membership:
    if: ${{ github.event.issue.pull_request && startsWith(github.event.comment.body, '/docs') }}
    uses: ./.github/workflows/check-org-membership.yml
    with:
      commenter: ${{ github.event.comment.user.login }}
    secrets:
      access_token: ${{ secrets.ORG_ACCESS_TOKEN }}

  generate-token:
    needs: check-membership
    if: ${{ needs.check-membership.outputs.is_member == 'true' }}
    runs-on: ubicloud-standard-2
    outputs:
      app_token: ${{ steps.app.outputs.token }}
    steps:
      - name: Generate an installation token
        id: app
        uses: actions/create-github-app-token@v2
        with:
          app-id: ${{ vars.INTERNAL_APP_ID }}
          private-key: ${{ secrets.INTERNAL_APP_KEY }}
          owner: windmill-labs

  trigger-docs:
    needs: [generate-token, check-membership]
    if: ${{ needs.check-membership.outputs.is_member == 'true' }}
    uses: windmill-labs/windmilldocs/.github/workflows/create-docs.yml@main
    with:
      pr_number: ${{ github.event.issue.number }}
      repo: ${{ github.event.repository.name }}
      comment_text: ${{ github.event.comment.body }}
    secrets:
      DOCS_TOKEN: ${{ needs.generate-token.outputs.app_token }}
      GOOGLE_API_KEY: ${{ secrets.GOOGLE_API_KEY }}
