name: "Check validity of .editorconfig"

permissions: read-all

on:
  pull_request:

jobs:
  check-editorconfig:
    runs-on: ubuntu-latest
    if: "!contains(github.event.pull_request.title, '[skip ci]')"
    steps:
      - name: Get list of changed files from PR
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          gh api \
            repos/notashelf/nvf/pulls/${{github.event.number}}/files --paginate \
            | jq '.[] | select(.status != "removed") | .filename' \
            > "$HOME/changed_files"

      - name: Print list of changed files
        run: |
          cat "$HOME/changed_files"

      - name: Checkout
        uses: actions/checkout@v4
        with:
          ref: refs/pull/${{ github.event.pull_request.number }}/merge

      - name: Install Nix
        uses: DeterminateSystems/nix-installer-action@main
      - uses: DeterminateSystems/magic-nix-cache-action@main

      - name: Checking EditorConfig
        shell: bash
        run: |
          cat "$HOME/changed_files" | nix-shell -p editorconfig-checker.out --run 'xargs -r editorconfig-checker -disable-indentation -exclude flake.lock --verbose'
          echo -n "Check status: $?"

      - name: Fail Gracefully
        if: ${{ failure() }}
        shell: bash
        run: |
          echo "::error:: Current formatting does not fit convention provided by .editorconfig located in the project root."
          echo "Please make sure your editor properly integrates editorconfig. See https://editorconfig.org/#download for more."
          exit 1