name: R valgrind tests

on:
  repository_dispatch:
    types: [gha_run_r_valgrind]

jobs:
  test-r-valgrind:
    name: r-package (ubuntu-latest, R-devel, valgrind)
    timeout-minutes: 120
    runs-on: ubuntu-latest
    container: wch1/r-debug
    env:
      SECRETS_WORKFLOW: ${{ secrets.WORKFLOW }}
    steps:
      - name: Install essential software before checkout
        shell: bash
        run: |
          apt-get update
          apt-get install --no-install-recommends -y \
            curl \
            git \
            jq
      - name: Checkout repository
        uses: actions/checkout@v2.3.4
        with:
          fetch-depth: 5
          submodules: true
          repository: microsoft/LightGBM
          ref: "refs/pull/${{ github.event.client_payload.pr_number }}/merge"
      - name: Send init status
        if: ${{ always() }}
        run: |
          $GITHUB_WORKSPACE/.ci/set_commit_status.sh "${{ github.workflow }}" "pending" "${{ github.event.client_payload.pr_sha }}"
          $GITHUB_WORKSPACE/.ci/append_comment.sh \
            "${{ github.event.client_payload.comment_number }}" \
            "Workflow **${{ github.workflow }}** has been triggered! 🚀\r\n${GITHUB_SERVER_URL}/microsoft/LightGBM/actions/runs/${GITHUB_RUN_ID}"
      - name: Install packages
        shell: bash
        run: |
          RDscriptvalgrind -e "install.packages(c('R6', 'data.table', 'jsonlite', 'testthat'), repos = 'https://cran.r-project.org')"
          sh build-cran-package.sh
          RDvalgrind CMD INSTALL --preclean --install-tests lightgbm_*.tar.gz || exit -1
      - name: Run tests with valgrind
        shell: bash
        run: ./.ci/test_r_package_valgrind.sh
      - name: Send final status
        if: ${{ always() }}
        run: |
          $GITHUB_WORKSPACE/.ci/set_commit_status.sh "${{ github.workflow }}" "${{ job.status }}" "${{ github.event.client_payload.pr_sha }}"
          $GITHUB_WORKSPACE/.ci/append_comment.sh \
            "${{ github.event.client_payload.comment_number }}" \
            "Status: ${{ job.status }}."
      - name: Rerun workflow-indicator
        if: ${{ always() }}
        run: |
          bash $GITHUB_WORKSPACE/.ci/rerun_workflow.sh \
            "optional_checks.yml" \
            "${{ github.event.client_payload.pr_number }}" \
            "${{ github.event.client_payload.pr_branch }}" \
            || true
