name: Pull request feedback

on:
  pull_request_target:
    types: [ opened, synchronize ]

jobs:
  test:
    runs-on: ubuntu-latest
    env:
      GITHUB_TOKEN: "${{ secrets.GITHUB_TOKEN }}"
    steps:
    - uses: actions/checkout@v2

    - name: Check for relevant changes
      uses: dorny/paths-filter@v2
      id: changed
      with:
        list-files: shell
        filters: |
          style:
            - '*.csl'
            - 'dependent/*.csl'
          locale:
            - '*.xml'

    - name: Changed files
      run: |
        echo changed: ${{ steps.changed.outputs.style_files }} ${{ steps.changed.outputs.locale_files }}

    - name: Set up Ruby
      if: steps.changed.outputs.style == 'true' || steps.changed.outputs.locale == 'true'
      uses: ruby/setup-ruby@v1
      with:
        ruby-version: 2.7.1
    - name: but use cache to speed that up
      if: steps.changed.outputs.style == 'true' || steps.changed.outputs.locale == 'true'
      uses: actions/cache@v2
      with:
        path: vendor/bundle
        key: ${{ runner.os }}-gems-${{ hashFiles('**/Gemfile.lock') }}
        restore-keys: |
          ${{ runner.os }}-gems-
    - name: Bundle install
      if: steps.changed.outputs.style == 'true' || steps.changed.outputs.locale == 'true'
      run: |
        bundle config path vendor/bundle
        bundle update sheldon --jobs 4 --retry 3

    - name: Apply the PR
      if: steps.changed.outputs.style == 'true' || steps.changed.outputs.locale == 'true'
      run: bundle exec sheldon --token=$GITHUB_TOKEN --apply

    - name: Welcome to a new PR
      if: github.event.action == 'opened' && steps.changed.outputs.style == 'true'
      run: bundle exec sheldon --token=$GITHUB_TOKEN --welcome

    - name: See if the styles/locales work
      if: steps.changed.outputs.style == 'true' || steps.changed.outputs.locale == 'true'
      run: bundle exec rake

    - name: report
      if: (failure() || success()) && steps.changed.outputs.style == 'true'
      run: bundle exec sheldon --token=$GITHUB_TOKEN --report --verbose
