name: Style

on:
  # Triggers the workflow on push or pull request events but only for the master branch
  # Event list: https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows
  pull_request:
    types: [opened, edited, synchronize]
    branches: [ master ]

  # Allows you to run this workflow manually from the Actions tab
  workflow_dispatch:

jobs:
  install-and-lint-pr-title:
    runs-on: ubuntu-latest
    name: Conventional commits style check

    steps:
      - uses: actions/checkout@v4

      - name: Install conventional commits linter
        run: npm install --save-dev @commitlint/config-conventional @commitlint/cli

      - name: Configure commitlint
        run: |
          echo "module.exports = {extends: ['@commitlint/config-conventional'], rules: {'subject-case': [0, 'never']}}" > commitlint.config.js

      - name: Lint current pull request title
        run: jq --raw-output ".pull_request.title" "$GITHUB_EVENT_PATH" | npx commitlint --verbose

# The code below is saved for future references.
#        env:
#          GH_TOKEN: ${{ github.token }}
#          pr_number=$(jq --raw-output .pull_request.number "$GITHUB_EVENT_PATH")
#          gh pr view $pr_number --json=title --jq '.title' | npx commitlint --verbose
