on: [push, pull_request]

name: build

permissions:
  contents: write
  deployments: write
  statuses: write

jobs:
  build-r:
    runs-on: ${{ matrix.config.os }}

    name: 'R: ${{ matrix.config.r }} (${{ matrix.config.os }})'

    strategy:
      fail-fast: false
      matrix:
        config:
          - { os: macOS-latest, r: 'release' }
          - { os: windows-latest, r: 'release' }
          - { os: windows-latest, r: 'oldrel' }
          - { os: ubuntu-20.04, r: 'devel', http-user-agent: 'release' }
          - { os: ubuntu-20.04, r: 'release', release: true }
          - { os: ubuntu-20.04, r: 'oldrel-1' }
          - { os: ubuntu-20.04, r: 'oldrel-2' }
          - { os: ubuntu-20.04, r: 'oldrel-3' }
          - { os: ubuntu-20.04, r: 'oldrel-4' }

    env:
      GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}
      R_KEEP_PKG_SOURCE: yes

    steps:
      - uses: actions/checkout@v3

      - uses: r-lib/actions/setup-pandoc@v2

      - uses: r-lib/actions/setup-r@v2
        with:
          r-version: ${{ matrix.config.r }}
          http-user-agent: ${{ matrix.config.http-user-agent }}
          use-public-rspm: true

      - uses: r-lib/actions/setup-r-dependencies@v2
        with:
          # Temporarily install htmlwidgets >= 1.6.0 for tests
          extra-packages: |
            any::rcmdcheck
            htmlwidgets?source

      - uses: r-lib/actions/check-r-package@v2

      - name: Test coverage
        if: matrix.config.release
        run: covr::codecov(quiet = FALSE)
        shell: Rscript {0}

      - name: Install pkgdown dependencies
        if: matrix.config.release
        uses: r-lib/actions/setup-r-dependencies@v2
        with:
          extra-packages: any::pkgdown, local::.

      - name: Build pkgdown site
        if: matrix.config.release
        run: Rscript pkgdown/build-docs.R

      - name: Set Netlify alias
        if: matrix.config.release
        run: |
          version="v$(cat DESCRIPTION | grep Version: | cut -d' ' -f2)"
          # Netlify aliases can't have periods, so replace them with dashes
          version_alias=${version//./-}
          if [ "$GITHUB_REF" = "refs/heads/$version" ]; then
            echo "NETLIFY_ALIAS=$version_alias" >> $GITHUB_ENV
          else
            github_sha_short=$(echo $GITHUB_SHA | cut -c 1-7)
            echo "NETLIFY_ALIAS=${version_alias}-${github_sha_short}" >> $GITHUB_ENV
          fi

      - name: Deploy pkgdown site to Netlify
        if: matrix.config.release
        uses: nwtgck/actions-netlify@v1.2
        with:
          publish-dir: './docs'
          github-token: ${{ secrets.GITHUB_TOKEN }}
          alias: ${{ env.NETLIFY_ALIAS }}
          deploy-message: 'Deploy from GitHub Actions: ${{ github.ref }} (${{ github.sha }})'
          enable-pull-request-comment: false
          enable-commit-comment: false
        env:
          NETLIFY_AUTH_TOKEN: ${{ secrets.NETLIFY_AUTH_TOKEN }}
          NETLIFY_SITE_ID: ${{ secrets.NETLIFY_SITE_ID }}
        timeout-minutes: 1

      - name: Deploy pkgdown site to GitHub Pages
        if: matrix.config.release && github.ref == 'refs/heads/main'
        uses: JamesIves/github-pages-deploy-action@v4.3.3
        with:
          branch: gh-pages
          folder: docs

  build-node:
    runs-on: ubuntu-latest

    name: Node.js

    steps:
      - uses: actions/checkout@v3

      - uses: actions/setup-node@v3
        with:
          node-version: '16'
          cache: 'npm'

      - name: Install dependencies
        run: npm install

      - name: Test
        run: npm run test:cover

      - name: Lint
        run: npm run lint

      - name: Upload coverage to Codecov
        uses: codecov/codecov-action@v3
