name: 'Run the provided command in an environment configured to monitor tmpnet networks'
description: 'Run the provided command in an environment configured to monitor tmpnet networks'

inputs:
  run:
    description: "the bash command to run"
    required: true
  filter_by_owner:
    default: ''
  prometheus_id:
    required: true
  prometheus_password:
    required: true
  loki_id:
    required: true
  loki_password:
    required: true
  # The following inputs need never be provided by the caller. They
  # default to context values that the action's steps are unable to
  # acccess directly.
  repository_owner:
    default: ${{ github.repository_owner }}
  repository_name:
    default: ${{ github.event.repository.name }}
  workflow:
    default: ${{ github.workflow }}
  run_id:
    default: ${{ github.run_id }}
  run_number:
    default: ${{ github.run_number }}
  run_attempt:
    default: ${{ github.run_attempt }}
  job:
    default: ${{ github.job }}

runs:
  using: composite
  steps:
    - name: Start prometheus
      # Only run for the original repo; a forked repo won't have access to the monitoring credentials
      if: (inputs.prometheus_id != '')
      shell: bash
      run: bash -x ./scripts/run_prometheus.sh
      env:
        PROMETHEUS_ID: ${{ inputs.prometheus_id }}
        PROMETHEUS_PASSWORD: ${{ inputs.prometheus_password }}
    - name: Start promtail
      if: (inputs.prometheus_id != '')
      shell: bash
      run: bash -x ./scripts/run_promtail.sh
      env:
        LOKI_ID: ${{ inputs.loki_id }}
        LOKI_PASSWORD: ${{ inputs.loki_password }}
    - name: Notify of metrics availability
      if: (inputs.prometheus_id != '')
      shell: bash
      run: ${{ github.action_path }}/notify-metrics-availability.sh
      env:
        GRAFANA_URL: https://grafana-poc.lux-dev.network/d/kBQpRdWnk/lux-main-dashboard?orgId=1&refresh=10s&var-filter=is_ephemeral_node%7C%3D%7Cfalse&var-filter=gh_repo%7C%3D%7C${{ inputs.repository_owner }}%2F${{ inputs.repository_name }}&var-filter=gh_run_id%7C%3D%7C${{ inputs.run_id }}&var-filter=gh_run_attempt%7C%3D%7C${{ inputs.run_attempt }}
        GH_JOB_ID: ${{ inputs.job }}
        FILTER_BY_OWNER: ${{ inputs.filter_by_owner }}
    - name: Run command
      shell: bash
      run: ${{ inputs.run }}
      env:
        GH_REPO: ${{ inputs.repository_owner }}/${{ inputs.repository_name }}
        GH_WORKFLOW: ${{ inputs.workflow }}
        GH_RUN_ID: ${{ inputs.run_id }}
        GH_RUN_NUMBER: ${{ inputs.run_number }}
        GH_RUN_ATTEMPT: ${{ inputs.run_attempt }}
        GH_JOB_ID: ${{ inputs.job }}
