name: ci

on:
  # Trigger the workflow on push to master or develop, except tag creation
  push:
    branches:
      - 'master'
      - 'develop'
    tags-ignore:
      - '**'

  # Trigger the workflow on pull request
  pull_request: ~

  # Trigger the workflow manually
  workflow_dispatch: ~

  # Trigger after public PR approved for CI
  pull_request_target:
    types: [labeled]

jobs:
  # Run CI including downstream packages on self-hosted runners
  downstream-ci:
    name: downstream-ci
    if: ${{ !github.event.pull_request.head.repo.fork && github.event.action != 'labeled' || github.event.label.name == 'approved-for-ci' }}
    uses: ecmwf/downstream-ci/.github/workflows/downstream-ci.yml@main
    with:
      metkit: ecmwf/metkit@${{ github.event.pull_request.head.sha || github.sha }}
      codecov_upload: true
      clang_format: true
    secrets: inherit

  # Run Rust CI (fmt + clippy + test) on the rust/ workspace
  ci-rust:
    name: ci-rust
    if: ${{ !github.event.pull_request.head.repo.fork && github.event.action != 'labeled' || github.event.label.name == 'approved-for-ci' }}
    uses: ecmwf/reusable-workflows/.github/workflows/ci-rust.yml@main
    with:
      manifest-path: rust/Cargo.toml
      features: --features vendored
      run-doc: false
    secrets:
      private_repos_token: ${{ secrets.GH_REPO_READ_TOKEN }}

  # Run CI of private downstream packages on self-hosted runners
  private-downstream-ci:
    name: private-downstream-ci
    needs: [downstream-ci]
    if: ${{ (success() || failure()) && (!github.event.pull_request.head.repo.fork && github.event.action != 'labeled' || github.event.label.name == 'approved-for-ci') }}
    runs-on: ubuntu-latest
    permissions:
      pull-requests: write
    steps:
      - name: Dispatch private downstream CI
        uses: ecmwf/dispatch-private-downstream-ci@v1
        with:
          token: ${{ secrets.GH_REPO_READ_TOKEN }}
          owner: ecmwf
          repository: private-downstream-ci
          event_type: downstream-ci
          payload: '{"metkit": "ecmwf/metkit@${{ github.event.pull_request.head.sha || github.sha }}"}'

  # Build downstream packages on HPC
  downstream-ci-hpc:
    name: downstream-ci-hpc
    if: ${{ !github.event.pull_request.head.repo.fork && github.event.action != 'labeled' || github.event.label.name == 'approved-for-ci' }}
    uses: ecmwf/downstream-ci/.github/workflows/downstream-ci-hpc.yml@main
    with:
      metkit: ecmwf/metkit@${{ github.event.pull_request.head.sha || github.sha }}
    secrets: inherit

  # Run CI of private downstream packages on HPC
  private-downstream-ci-hpc:
    name: private-downstream-ci-hpc
    needs: [downstream-ci-hpc]
    if: ${{ (success() || failure()) && (!github.event.pull_request.head.repo.fork && github.event.action != 'labeled' || github.event.label.name == 'approved-for-ci') }}
    runs-on: ubuntu-latest
    permissions:
      pull-requests: write
    steps:
      - name: Dispatch private downstream CI
        uses: ecmwf/dispatch-private-downstream-ci@v1
        with:
          token: ${{ secrets.GH_REPO_READ_TOKEN }}
          owner: ecmwf
          repository: private-downstream-ci
          event_type: downstream-ci-hpc
          payload: '{"metkit": "ecmwf/metkit@${{ github.event.pull_request.head.sha || github.sha }}"}'

  notify:
    runs-on: ubuntu-latest
    needs:
      - downstream-ci
      - ci-rust
      - private-downstream-ci
      - downstream-ci-hpc
      - private-downstream-ci-hpc
    if: ${{ always() && !github.event.pull_request.head.repo.fork && (github.event.action != 'labeled' || github.event.label.name == 'approved-for-ci') }}
    steps:
      - name: Trigger Teams notification
        uses: ecmwf/notify-teams@v1
        with:
          incoming_webhook: ${{ secrets.MS_TEAMS_INCOMING_WEBHOOK }}
          needs_context: ${{ toJSON(needs) }}
