# Start job on Slab CI bot given by input command.
name: Start AWS job

on:
  workflow_call:
    inputs:
      command:
        required: true
        type: string
      user_inputs:
        required: false
        type: string
  workflow_dispatch:
    inputs:
      command:
        required: true
        type: string
      user_inputs:
        description: 'user inputs to be forwarded to the called workflow'
        required: false
        type: string

env:
  GIT_REF: ${{ github.head_ref }}

jobs:
  sl:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout concrete
        uses: actions/checkout@v3
        with:
          fetch-depth: 0

      - name: Get git ref
        # github.head_ref is only available from a Pull Request
        if: env.GIT_REF == ''
        run: |
          echo "GIT_REF=${{ github.ref_name }}" >> $GITHUB_ENV

      - name: Checkout Slab repo
        uses: actions/checkout@v3
        with:
          repository: zama-ai/slab
          path: slab
          token: ${{ secrets.CONCRETE_ACTIONS_TOKEN }}

      - name: Start AWS job in Slab
        shell: bash
        run: |
          GIT_SHA="$(git --no-pager show -s --format="%H" origin/${{ env.GIT_REF }})" || GIT_SHA=${{ github.sha }}
          echo -n '{"command": "${{ inputs.command }}", "git_ref": "${{ env.GIT_REF }}", "sha":"'${GIT_SHA}'", "user_inputs": "${{ inputs.user_inputs }}"}' > command.json
          cat command.json
          SIGNATURE="$(slab/scripts/hmac_calculator.sh command.json '${{ secrets.JOB_SECRET }}')"
          curl -v -k \
          --fail-with-body \
          -H "Content-Type: application/json" \
          -H "X-Slab-Repository: ${{ github.repository }}" \
          -H "X-Slab-Command: start_aws" \
          -H "X-Hub-Signature-256: sha256=${SIGNATURE}" \
          -d @command.json \
          ${{ secrets.SLAB_URL }}
