name: Load Test Manual Action

permissions:
  contents: read

on:
  workflow_dispatch:
    inputs:
      network:
        description: 'Load test network'
        required: true
        type: choice
        options:
          - devnet
          - sepolia
        default: 'devnet'
      file:
        description: 'Load test filename'
        required: true
        type: string
        default: 'money-transfer.json'
      private_key_overwrite:
        description: 'Optional private key overwrite'
        required: false
        type: string

concurrency:
  group: load-test-${{ github.event.inputs.network }}-${{ github.ref }}
  cancel-in-progress: false

jobs:
  run-load-test:
    runs-on: gha-runner-scale-set-ubuntu-22.04-amd64-small
    name: Run Load Test
    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - uses: actions/setup-java@8df1039502a15bceb9433410b1a100fbe190c53b #v4.5.0
        with:
          distribution: temurin
          java-version: 17

      - name: Setup Gradle
        uses: gradle/actions/setup-gradle@8379f6a1328ee0e06e2bb424dadb7b159856a326 # 4.4.0

      - name: Hide sensitive inputs
        uses: levibostian/action-hide-sensitive-inputs@1.0.0
        with:
          exclude_inputs: network, file

      - name: Determine Private Key
        id: set_private_key
        run: |
          if [ -n "${{ github.event.inputs.private_key_overwrite }}" ]; then
            echo "Using provided private key."
            echo "PRIVATE_KEY=${{ github.event.inputs.private_key_overwrite }}" >> $GITHUB_ENV
          elif [ "${{ github.event.inputs.network }}" == "devnet" ]; then
            echo "Using devnet private key from secrets."
            echo "PRIVATE_KEY=${{ secrets.DEVNET_LOAD_TEST_PRIVATE_KEY }}" >> $GITHUB_ENV
          elif [ "${{ github.event.inputs.network }}" == "sepolia" ]; then
            echo "Using sepolia private key from secrets."
            echo "PRIVATE_KEY=${{ secrets.SEPOLIA_LOAD_TEST_PRIVATE_KEY }}" >> $GITHUB_ENV
          fi

      - name: Load Test
        run: |
          echo "Network to execute load test on: ${{ github.event.inputs.network }}"
          ./gradlew :testing-tools:app:run --args="-request ${{ github.event.inputs.network }}/${{ github.event.inputs.file }} -pk $PRIVATE_KEY"
