name: Lint

on: [pull_request]

permissions:
  contents: read

jobs:
  trailing-whitespace:
    runs-on: ubuntu-latest
    steps:
    - name: Checkout
      uses: actions/checkout@v4
    - name: Check for trailing whitespace
      uses: raisedevs/find-trailing-whitespace@restrict-to-plaintext-only
    - name: Check for merge conflicts
      run: git diff --check
      shell: bash
    - name: Ensure all files have newline at EOF
      uses: Logerfo/newline-action@0.0.4
      with:
        github-token: ${{ secrets.GITHUB_TOKEN }}
  prose:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v4
      - name: Vale
        uses: errata-ai/vale-action@d89dee975228ae261d22c15adcd03578634d429c
        env:
          GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}}
  markdown:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@master
    - uses: avto-dev/markdown-lint@v1
      with:
        config: '.github/lint/markdown.json'
        args: 'docs/**/*.md'
  changedfiles:
    runs-on: ubuntu-latest
    # Map a step output to a job output
    outputs:
      all: ${{ steps.changes.outputs.all}}
      markdown: ${{ steps.changes.outputs.markdown }}
    steps:
      # Make sure we have some code to diff.
      - name: Checkout repository
        uses: actions/checkout@v4
        with:
          fetch-depth: 0
      - name: Get changed files
        id: changes
        # Set outputs using the command.
        run: |
          echo "::set-output name=all::$(git diff --name-only --diff-filter=ACMRT ${{ github.event.pull_request.base.sha }} ${{ github.event.pull_request.head.sha }} | xargs)"
          echo "::set-output name=markdown::$(git diff --name-only --diff-filter=ACMRT ${{ github.event.pull_request.base.sha }} ${{ github.event.pull_request.head.sha }} | grep .md$ | xargs)"
  changelog:
    runs-on: ubuntu-latest
    # require the first job to have ran
    needs: changedfiles
    steps:
      - name: echo changed files
        run: |
          # Filter out Gemfile.lock files from changed files
          ALL_FILES="${{needs.changedfiles.outputs.all}}"
          FILTERED_FILES=$(echo "$ALL_FILES" | tr ' ' '\n' | grep -v '^Gemfile\.lock$' | grep -v '^docs/Gemfile\.lock$' | tr '\n' ' ' | xargs)

          # Only require changelog if there are other files changed
          if [[ -n "$FILTERED_FILES" ]] && [[ ! "${{needs.changedfiles.outputs.markdown}}" == *"CHANGELOG.md"* ]]; then
            echo "::error file=CHANGELOG.md,line=1,col=1::Please make sure that you add a docs/CHANGELOG.md entry to describe the changes in this pull request."
            exit 1
          fi
  lint:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@master
    - name: Setup Ruby
      uses: ruby/setup-ruby@v1
      with:
        ruby-version: 4.0
    - uses: actions/cache@v4
      with:
        path: vendor/bundle
        key: gems-build-rails-8-ruby-3.2-${{ hashFiles('**/Gemfile.lock') }}
    - name: Lint with Rubocop and ERB Lint
      run: |
        bundle config path vendor/bundle
        bundle update
        bundle exec standardrb -r "rubocop-md"
        bundle exec erb_lint --lint-all
      env:
        RAILS_VERSION: '~> 8'
