name: Integration
on:
  # @moul: temporarily disabled.
  #        the test is flappy and the current notification system make it not very useful.
  #        I think that we should move the prod integration test to an external monitoring and only keep the CI/CD related integration test here.
  #schedule:
  #  - cron: '0 * * * *'
  push:
    tags:
      - v*
    branches:
      - master
    paths:
      - "go/**"
      - "!go/**.md"
      - "go.*"
      - "**.go"
      - "js/**"
      - "!js/packages/i18n/locale/*/*.json"
      - ".github/workflows/integration.yml"
  pull_request:
    paths:
      - "go/**"
      - "!go/**.md"
      - "go.*"
      - "**.go"
      - "js/**"
      - "!js/packages/i18n/locale/*/*.json"
      - ".github/workflows/integration.yml"

# FIXME:
# * test on production periodically with env=production
# * test against a fresh docker compose infra on each commit
# * store performance logs for comparison

jobs:
  integration-prod:
    if: github.event_name == 'schedule'
    name: Integration (Production)
    runs-on: ubuntu-latest
    steps:
      - name: Generate run's URL
        id: url
        run: |
          echo "::set-output name=runUrl::https://github.com/${GITHUB_REPOSITORY}/actions/runs/${GITHUB_RUN_ID}"

      - name: Checkout
        uses: actions/checkout@v4

      - name: Load variables from file
        uses: antifree/json-to-variables@v1.0.1
        with:
          filename: .github/workflows/utils/variables.json

      - name: Setup asdf
        uses: asdf-vm/actions/setup@9cd779f40fe38688dd19505ccbc4eaaf018b44e7
        with:
          asdf_version: 0.16.7

      - name: Setup go
        run: |
          asdf plugin add golang
          asdf install golang
          echo "go_version=$(asdf current golang | xargs | cut -d ' ' -f 6)" >> $GITHUB_ENV

      - name: Cache Go modules
        uses: actions/cache@v4
        with:
          path: ~/go/pkg/mod
          key: ${{ runner.os }}-go-${{ env.go_version }}-${{ env.json_cache-versions_go }}-${{ hashFiles('**/go.sum') }}
          restore-keys: ${{ runner.os }}-go-${{ env.go_version }}-${{ env.json_cache-versions_go }}-

      - name: Avoid triggering make generate
        run: touch go/gen.sum

      - name: Fetch dependencies
        run: go mod download

      - name: Compile the integration binary
        working-directory: go
        run: go install ./cmd/berty-integration
          asdf reshim golang # See: https://github.com/kennyp/asdf-golang/issues/18

      - name: Run integration test suite
        run: |
          berty-integration \
            -p2p.disc-disable-filter=true \
            -integration.testbot=`cat ./config/config.gen.json | jq -r '.berty.contacts["testbot"].link'` \
            -integration.benchmark > benchmark_result.json

      - name: Download previous benchmark data
        uses: actions/cache@v4
        with:
          path: ./cache
          key: ${{runner.os}}-integration-benchmark

      - name: Discord notification
        if: failure()
        env:
          DISCORD_WEBHOOK: ${{ secrets.DISCORD_PROD_WEBHOOK }}
        uses: Ilshidur/action-discord@0.3.2
        with:
          args: "Integration failed, see ${{ steps.url.outputs.runUrl }}"

  integration-local:
    if: github.event_name == 'push' || github.event_name == 'pull_request'
    name: Integration (Local)
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Build Docker images
        working-directory: tool/integration-compose
        run: docker compose build

      - name: Run integration test suite
        working-directory: tool/integration-compose
        run: make
      #- name: Store benchmark result
      #  uses: rhysd/github-action-benchmark@v1
      #  with:
      #    tool: 'googlecpp'
      #    output-file-path: benchmark_result.json
      #    external-data-json-path: ./cache/benchmark-data.json
      #    fail-on-alert: false
      #    comment-on-alert: true
      #    github-token: ${{secrets.GITHUB_TOKEN}}
      #    #alert-comment-cc-users: '@foobar'
      #    #auto-push: true

  doctor-production:
    name: Doctor (Production)
    runs-on: ubuntu-latest
    steps:
      - name: Generate run's URL
        id: url
        run: |
          echo "::set-output name=runUrl::https://github.com/${GITHUB_REPOSITORY}/actions/runs/${GITHUB_RUN_ID}"

      - name: Checkout
        uses: actions/checkout@v4

      - name: Load variables from file
        uses: antifree/json-to-variables@v1.0.1
        with:
          filename: .github/workflows/utils/variables.json

      - name: Setup asdf
        uses: asdf-vm/actions/setup@9cd779f40fe38688dd19505ccbc4eaaf018b44e7
        with:
          asdf_version: 0.16.7

      - name: Setup go
        run: |
          asdf plugin add golang
          asdf install golang
          echo "go_version=$(asdf current golang | xargs | cut -d ' ' -f 6)" >> $GITHUB_ENV

      - name: Cache Go modules
        uses: actions/cache@v4
        with:
          path: ~/go/pkg/mod
          key: ${{ runner.os }}-go-${{ env.go_version }}-${{ env.json_cache-versions_go }}-${{ hashFiles('**/go.sum') }}
          restore-keys: ${{ runner.os }}-go-${{ env.go_version }}-${{ env.json_cache-versions_go }}-

      - name: Avoid triggering make generate
        run: touch go/gen.sum

      - name: Fetch dependencies
        run: go mod download

      - name: Compile the doctor binary
        working-directory: go
        run: |
          go install ./cmd/berty-doctor
          asdf reshim golang # See: https://github.com/kennyp/asdf-golang/issues/18

      - name: Run doctor
        run: berty-doctor -v

      - name: Discord notification
        if: github.event_name == 'schedule' && failure()
        env:
          DISCORD_WEBHOOK: ${{ secrets.DISCORD_PROD_WEBHOOK }}
        uses: Ilshidur/action-discord@0.3.2
        with:
          args: "Doctor failed, see ${{ steps.url.outputs.runUrl }}"
