name: ci
concurrency:
  group: "${{github.workflow}}-${{github.ref}}"
  cancel-in-progress: true
on:
  workflow_dispatch:
  schedule:
    - cron: "0 8 * * 3" # At 08:00 on Wednesday # https://crontab.guru/#0_8_*_*_3
  push:
    branches:
      - main
      - v*.*.x
    tags:
      - v*.*.*
  pull_request:
    types: [opened, synchronize]
    branches:
      - '*'

jobs:
  rubocop:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: ruby/setup-ruby@v1
        with:
          ruby-version: "3.1"
          bundler-cache: true
      - run: bundle exec rake rubocop

  test:
    needs: ["rubocop"]
    strategy:
      fail-fast: false
      matrix:
        ruby: ["2.5", "2.6", "2.7", "3.0", "3.1", "3.2", "3.3", "3.4", "head", "truffleruby-head", "jruby-9.4"]
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: ruby/setup-ruby@v1
        with:
          ruby-version: ${{matrix.ruby}}
          bundler-cache: true
          rubygems: 3.3.26 # for ruby 2.5
      - run: bundle exec rake test

  # borrowed from nokogiri/.github/workflows/downstream.yml
  downstream:
    needs: ["rubocop"]
    name: downstream-${{matrix.name}}
    strategy:
      fail-fast: false
      matrix:
        include:
          - url: https://github.com/rails/rails-html-sanitizer
            name: rails-html-sanitizer
            command: "bundle exec rake test"
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: ruby/setup-ruby@v1
        with:
          ruby-version: "3.2"
          bundler-cache: true
          rubygems: latest
      - run: |
          git clone --depth=1 ${{matrix.url}} ${{matrix.name}}
          cd ${{matrix.name}}
          bundle add loofah --path ".."
          bundle install --local || bundle install
          ${{matrix.command}}
