# Roblox Integration Deploy
#
# Builds and uploads the project to your Roblox integration place on every pull request.
# This workflow is inactive until you complete the setup below.
#
# Setup:
#   1. Run `nevermore deploy init` to create deploy.nevermore.json (with an `integration` target)
#   2. Add ROBLOX_OPEN_CLOUD_API_KEY as a repository secret
#      (Create one at https://create.roblox.com/dashboard/credentials)
#   3. Deploys will run automatically on every pull request
#
# Docs: https://quenty.github.io/NevermoreEngine/docs/testing/integration-testing

name: integration
on: [pull_request]
jobs:
  integration:
    runs-on: ubuntu-latest
    environment: integration
    steps:
      - id: check
        name: Check config
        run: |
          if [ -n "$KEY" ]; then
            echo "enabled=true" >> "$GITHUB_OUTPUT"
          else
            echo "::notice::Integration skipped — ROBLOX_OPEN_CLOUD_API_KEY not configured. See top of this file for setup."
          fi
        env:
          KEY: $\{{ secrets.ROBLOX_OPEN_CLOUD_API_KEY }}

      - name: Checkout repository
        if: steps.check.outputs.enabled == 'true'
        uses: actions/checkout@v6

      - name: Fetch base branch for changeset diff
        if: steps.check.outputs.enabled == 'true'
        run: git fetch origin $\{{ github.base_ref }} --depth=1

      - name: Setup Aftman
        if: steps.check.outputs.enabled == 'true'
        uses: ok-nick/setup-aftman@v0.4.2
        with:
          version: 'v0.3.0'
          token: $\{{ secrets.GITHUB_TOKEN }}
          cache: true

      - name: Setup pnpm
        if: steps.check.outputs.enabled == 'true'
        uses: pnpm/action-setup@v4
        with:
          cache: true

      - name: Setup npm for GitHub Packages
        if: steps.check.outputs.enabled == 'true'
        run: |
          if [[ -n "$NPM_GITHUB_QUENTYSTUDIOS_TOKEN" ]]; then
            echo -e "\n//npm.pkg.github.com/:_authToken=$NPM_GITHUB_QUENTYSTUDIOS_TOKEN" >> .npmrc
            echo -e "\n//npm.pkg.github.com/:_authToken=$NPM_GITHUB_QUENTYSTUDIOS_TOKEN" >> ~/.npmrc
          fi

          if [[ -n "$NPM_TOKEN" ]]; then
            echo -e "\n//registry.npmjs.org/:_authToken=$NPM_TOKEN" >> .npmrc
            echo -e "\n//registry.npmjs.org/:_authToken=$NPM_TOKEN" >> ~/.npmrc
          fi
        env:
          NPM_GITHUB_QUENTYSTUDIOS_TOKEN: $\{{ secrets.NPM_GITHUB_QUENTYSTUDIOS_TOKEN }}
          NPM_TOKEN: $\{{ secrets.NPM_TOKEN }}

      - name: Link and install packages
        if: steps.check.outputs.enabled == 'true'
        run: pnpm install --frozen-lockfile

      - name: Deploy integration
        id: deploy-integration
        if: steps.check.outputs.enabled == 'true'
        run: npx @quenty/nevermore-cli batch deploy --target integration --publish --base origin/$\{{ github.base_ref }} --output integration-results.json --yes --verbose --logs
        env:
          ROBLOX_OPEN_CLOUD_API_KEY: $\{{ secrets.ROBLOX_OPEN_CLOUD_API_KEY }}
          GITHUB_TOKEN: $\{{ secrets.GITHUB_TOKEN }}

      - name: Post deploy results
        if: always() && steps.check.outputs.enabled == 'true'
        run: npx @quenty/nevermore-cli tools post-deploy-results integration-results.json --yes --run-outcome $\{{ steps.deploy-integration.outcome }}
        env:
          GITHUB_TOKEN: $\{{ secrets.GITHUB_TOKEN }}
