name: R-package

on:
  push:
    branches:
    - master
  pull_request:
    branches:
    - master
    - release/*

# automatically cancel in-progress builds if another commit is pushed
concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true

env:
  # hack to get around this:
  # https://stat.ethz.ch/pipermail/r-package-devel/2020q3/005930.html
  _R_CHECK_SYSTEM_CLOCK_: 0
  # ignore R CMD CHECK NOTE checking how long it has
  # been since the last submission
  _R_CHECK_CRAN_INCOMING_REMOTE_: 0
  # CRAN ignores the "installed size is too large" NOTE,
  # so our CI can too. Setting to a large value here just
  # to catch extreme problems
  _R_CHECK_PKG_SIZES_THRESHOLD_: 100

jobs:
  test:
    name: ${{ matrix.task }} (${{ matrix.os }}, ${{ matrix.compiler }}, R ${{ matrix.r_version }}, ${{ matrix.build_type }})
    runs-on: ${{ matrix.os }}
    container: ${{ matrix.container }}
    timeout-minutes: 60
    strategy:
      fail-fast: false
      matrix:
        include:
          ################
          # CMake builds #
          ################
          - os: ubuntu-latest
            task: r-package
            compiler: gcc
            r_version: 3.6
            build_type: cmake
            container: 'ubuntu:18.04'
          - os: ubuntu-latest
            task: r-package
            compiler: gcc
            r_version: 4.3
            build_type: cmake
            container: 'ubuntu:22.04'
          - os: ubuntu-latest
            task: r-package
            compiler: clang
            r_version: 3.6
            build_type: cmake
            container: 'ubuntu:18.04'
          - os: ubuntu-latest
            task: r-package
            compiler: clang
            r_version: 4.3
            build_type: cmake
            container: 'ubuntu:22.04'
          - os: macOS-latest
            task: r-package
            compiler: gcc
            r_version: 4.3
            build_type: cmake
            container: null
          - os: macOS-latest
            task: r-package
            compiler: clang
            r_version: 4.3
            build_type: cmake
            container: null
          - os: windows-latest
            task: r-package
            compiler: MINGW
            toolchain: MINGW
            r_version: 3.6
            build_type: cmake
            container: null
          - os: windows-latest
            task: r-package
            compiler: MINGW
            toolchain: MSYS
            r_version: 4.3
            build_type: cmake
            container: null
          # Visual Studio 2019
          - os: windows-2019
            task: r-package
            compiler: MSVC
            toolchain: MSVC
            r_version: 3.6
            build_type: cmake
            container: null
          # Visual Studio 2022
          - os: windows-2022
            task: r-package
            compiler: MSVC
            toolchain: MSVC
            r_version: 4.3
            build_type: cmake
            container: null
          ###############
          # CRAN builds #
          ###############
          - os: windows-latest
            task: r-package
            compiler: MINGW
            toolchain: MINGW
            r_version: 3.6
            build_type: cran
            container: null
          - os: windows-latest
            task: r-package
            compiler: MINGW
            toolchain: MSYS
            r_version: 4.3
            build_type: cran
            container: null
          - os: ubuntu-latest
            task: r-package
            compiler: gcc
            r_version: 4.3
            build_type: cran
            container: 'ubuntu:22.04'
          - os: macOS-latest
            task: r-package
            compiler: clang
            r_version: 4.3
            build_type: cran
            container: null
          ################
          # Other checks #
          ################
          - os: ubuntu-latest
            task: r-rchk
            compiler: gcc
            r_version: 4.3
            build_type: cran
            container: 'ubuntu:22.04'
    steps:
      - name: Prevent conversion of line endings on Windows
        if: startsWith(matrix.os, 'windows')
        shell: pwsh
        run: git config --global core.autocrlf false
      - name: Install packages used by third-party actions
        if: startsWith(matrix.os, 'ubuntu')
        shell: bash
        run: |
          apt-get update -y
          apt-get install --no-install-recommends -y \
            ca-certificates \
            dirmngr \
            gpg \
            gpg-agent \
            software-properties-common \
            sudo
          # install newest version of git
          # ref:
          #     - https://unix.stackexchange.com/a/170831/550004
          #     - https://git-scm.com/download/linux
          add-apt-repository ppa:git-core/ppa -y
          apt-get update -y
          apt-get install --no-install-recommends -y \
            git
      - name: Trust git cloning LightGBM
        if: startsWith(matrix.os, 'ubuntu')
        run: |
          git config --global --add safe.directory "${GITHUB_WORKSPACE}"
      - name: Checkout repository
        uses: actions/checkout@v3
        with:
          fetch-depth: 5
          submodules: true
      - name: Install pandoc
        uses: r-lib/actions/setup-pandoc@v2
      - name: install tinytex
        if: startsWith(matrix.os, 'windows')
        uses: r-lib/actions/setup-tinytex@v2
        env:
          CTAN_MIRROR: https://ctan.math.illinois.edu/systems/win32/miktex
          TINYTEX_INSTALLER: TinyTeX
      - name: Setup and run tests on Linux and macOS
        if: matrix.os == 'macOS-latest' || matrix.os == 'ubuntu-latest'
        shell: bash
        run: |
          export TASK="${{ matrix.task }}"
          export COMPILER="${{ matrix.compiler }}"
          export GITHUB_ACTIONS="true"
          if [[ "${{ matrix.os }}" == "macOS-latest" ]]; then
              export OS_NAME="macos"
          elif [[ "${{ matrix.os }}" == "ubuntu-latest" ]]; then
              export OS_NAME="linux"
              export IN_UBUNTU_BASE_CONTAINER="true"
              # the default version of cmake provided on Ubuntu 18.04 (v3.10.2), is not supported by LightGBM
              # see https://github.com/microsoft/LightGBM/issues/5642
              if [[ "${{ matrix.container }}" == "ubuntu:18.04" ]]; then
                export INSTALL_CMAKE_FROM_RELEASES="true"
              fi
          fi
          export BUILD_DIRECTORY="$GITHUB_WORKSPACE"
          export R_VERSION="${{ matrix.r_version }}"
          export R_BUILD_TYPE="${{ matrix.build_type }}"
          $GITHUB_WORKSPACE/.ci/setup.sh
          $GITHUB_WORKSPACE/.ci/test.sh
      - name: Setup and run tests on Windows
        if: startsWith(matrix.os, 'windows')
        shell: pwsh -command ". {0}"
        run: |
          $env:BUILD_SOURCESDIRECTORY = $env:GITHUB_WORKSPACE
          $env:LGB_VER = (Get-Content -TotalCount 1 $env:BUILD_SOURCESDIRECTORY\VERSION.txt).trim().replace('rc', '-')
          $env:TOOLCHAIN = "${{ matrix.toolchain }}"
          $env:R_VERSION = "${{ matrix.r_version }}"
          $env:R_BUILD_TYPE = "${{ matrix.build_type }}"
          $env:COMPILER = "${{ matrix.compiler }}"
          $env:GITHUB_ACTIONS = "true"
          $env:TASK = "${{ matrix.task }}"
          & "$env:GITHUB_WORKSPACE/.ci/test_windows.ps1"
  test-r-sanitizers:
    name: r-sanitizers (ubuntu-latest, R-devel, ${{ matrix.compiler }} ASAN/UBSAN)
    timeout-minutes: 60
    runs-on: ubuntu-latest
    container: wch1/r-debug
    strategy:
      fail-fast: false
      matrix:
        include:
          - r_customization: san
            compiler: gcc
          - r_customization: csan
            compiler: clang
    steps:
      - name: Trust git cloning LightGBM
        run: |
          git config --global --add safe.directory "${GITHUB_WORKSPACE}"
      - name: Checkout repository
        uses: actions/checkout@v3
        with:
          fetch-depth: 5
          submodules: true
      - name: Install packages
        shell: bash
        run: |
          RDscript${{ matrix.r_customization }} -e "install.packages(c('R6', 'data.table', 'jsonlite', 'knitr', 'markdown', 'Matrix', 'RhpcBLASctl', 'testthat'), repos = 'https://cran.rstudio.com', Ncpus = parallel::detectCores())"
          sh build-cran-package.sh --r-executable=RD${{ matrix.r_customization }}
          RD${{ matrix.r_customization }} CMD INSTALL lightgbm_*.tar.gz || exit -1
      - name: Run tests with sanitizers
        shell: bash
        run: |
          cd R-package/tests
          exit_code=0
          RDscript${{ matrix.r_customization }} testthat.R >> tests.log 2>&1 || exit_code=-1
          cat ./tests.log
          exit ${exit_code}
  test-r-debian-clang:
    name: r-package (debian, R-devel, clang-${{ matrix.clang-version }})
    timeout-minutes: 60
    strategy:
      fail-fast: false
      matrix:
        # list of versions tested in CRAN "Additional Checks":
        # https://cran.r-project.org/web/checks/check_issue_kinds.html
        clang-version:
          - 16
          - 17
          - 18
    runs-on: ubuntu-latest
    container: rhub/debian-clang-devel
    env:
      DEBIAN_FRONTEND: noninteractive
    steps:
      - name: Install Git before checkout
        shell: bash
        run: |
          apt-get update --allow-releaseinfo-change
          apt-get install --no-install-recommends -y git
      - name: Trust git cloning LightGBM
        run: |
          git config --global --add safe.directory "${GITHUB_WORKSPACE}"
      - name: Checkout repository
        uses: actions/checkout@v3
        with:
          fetch-depth: 5
          submodules: true
      - name: install clang
        run: |
          ./.ci/install-clang-devel.sh ${{ matrix.clang-version }}
      - name: Install packages and run tests
        shell: bash
        run: |
          export PATH=/opt/R-devel/bin/:${PATH}
          Rscript -e "install.packages(c('R6', 'data.table', 'jsonlite', 'knitr', 'markdown', 'Matrix', 'RhpcBLASctl', 'testthat'), repos = 'https://cran.rstudio.com', Ncpus = parallel::detectCores())"
          sh build-cran-package.sh
          R CMD check --as-cran --run-donttest lightgbm_*.tar.gz || exit -1
          echo ""
          echo "install logs:"
          echo ""
          cat lightgbm.Rcheck/00install.out
          echo ""
          if grep -q -E "NOTE|WARNING|ERROR" lightgbm.Rcheck/00check.log; then
              echo "NOTEs, WARNINGs, or ERRORs have been found by R CMD check"
              exit -1
          fi
  all-r-package-jobs-successful:
    if: always()
    runs-on: ubuntu-latest
    needs: [test, test-r-sanitizers, test-r-debian-clang]
    steps:
    - name: Note that all tests succeeded
      uses: re-actors/alls-green@v1.2.2
      with:
        jobs: ${{ toJSON(needs) }}
