# This workflow exports the built libxrpl package to the Conan remote on a
# a channel named after the pull request, and notifies the Clio repository about
# the new version so it can check for compatibility.
name: Notify Clio

# This workflow can only be triggered by other workflows.
on:
  workflow_call:
    inputs:
      conan_remote_name:
        description: "The name of the Conan remote to use."
        required: false
        type: string
        default: xrplf
      conan_remote_url:
        description: "The URL of the Conan endpoint to use."
        required: false
        type: string
        default: https://conan.ripplex.io
    secrets:
      clio_notify_token:
        description: "The GitHub token to notify Clio about new versions."
        required: true
      conan_remote_username:
        description: "The username for logging into the Conan remote."
        required: true
      conan_remote_password:
        description: "The password for logging into the Conan remote."
        required: true

concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}-clio
  cancel-in-progress: true

defaults:
  run:
    shell: bash

jobs:
  upload:
    if: ${{ github.event.pull_request.head.repo.full_name == github.repository }}
    runs-on: ubuntu-latest
    container: ghcr.io/xrplf/ci/ubuntu-noble:gcc-13-sha-5dd7158
    steps:
      - name: Checkout repository
        uses: actions/checkout@08eba0b27e820071cde6df949e0beb9ba4906955 # v4.3.0
      - name: Generate outputs
        id: generate
        env:
          PR_NUMBER: ${{ github.event.pull_request.number }}
        run: |
          echo 'Generating user and channel.'
          echo "user=clio" >> "${GITHUB_OUTPUT}"
          echo "channel=pr_${PR_NUMBER}" >> "${GITHUB_OUTPUT}"
          echo 'Extracting version.'
          echo "version=$(cat src/libxrpl/protocol/BuildInfo.cpp | grep "versionString =" | awk -F '"' '{print $2}')" >> "${GITHUB_OUTPUT}"
      - name: Calculate conan reference
        id: conan_ref
        run: |
          echo "conan_ref=${{ steps.generate.outputs.version }}@${{ steps.generate.outputs.user }}/${{ steps.generate.outputs.channel }}" >> "${GITHUB_OUTPUT}"
      - name: Set up Conan
        uses: ./.github/actions/setup-conan
        with:
          conan_remote_name: ${{ inputs.conan_remote_name }}
          conan_remote_url: ${{ inputs.conan_remote_url }}
      - name: Log into Conan remote
        env:
          CONAN_REMOTE_NAME: ${{ inputs.conan_remote_name }}
        run: conan remote login "${CONAN_REMOTE_NAME}" "${{ secrets.conan_remote_username }}" --password "${{ secrets.conan_remote_password }}"
      - name: Upload package
        env:
          CONAN_REMOTE_NAME: ${{ inputs.conan_remote_name }}
        run: |
          conan export --user=${{ steps.generate.outputs.user }} --channel=${{ steps.generate.outputs.channel }} .
          conan upload --confirm --check --remote="${CONAN_REMOTE_NAME}" xrpl/${{ steps.conan_ref.outputs.conan_ref }}
    outputs:
      conan_ref: ${{ steps.conan_ref.outputs.conan_ref }}

  notify:
    needs: upload
    runs-on: ubuntu-latest
    steps:
      - name: Notify Clio
        env:
          GH_TOKEN: ${{ secrets.clio_notify_token }}
          PR_URL: ${{ github.event.pull_request.html_url }}
        run: |
          gh api --method POST -H "Accept: application/vnd.github+json" -H "X-GitHub-Api-Version: 2022-11-28" \
          /repos/xrplf/clio/dispatches -f "event_type=check_libxrpl" \
          -F "client_payload[conan_ref]=${{ needs.upload.outputs.conan_ref }}" \
          -F "client_payload[pr_url]=${PR_URL}"
