name: pr-name
on:
  pull_request:
    types: ['opened', 'edited', 'reopened', 'synchronize']
    branches-ignore:
      - "[0-9]+.[0-9]+"

jobs:
  pr_name_lint:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
        with:
          persist-credentials: false
          fetch-depth: 0
      - uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020 # v4.4.0
        name: Install Node.js
        with:
          node-version: 16
      - name: Install dependencies
        run: |
          npm install @commitlint/lint@18.6.1 @commitlint/load@18.6.1 @commitlint/config-conventional@18.6.2 @actions/core
      - name: Lint PR name
        uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1
        with:
          script: |
            const load = require('@commitlint/load').default;
            const lint = require('@commitlint/lint').default;

            const CONFIG = {
              extends: ['./commitlint.config.js'],
            };

            const title = context.payload.pull_request.title;

            core.info(`Linting: ${title}`);

            load(CONFIG)
              .then((opts) => {
                lint(
                  title,
                  opts.rules,
                  opts.parserPreset ? {parserOpts: opts.parserPreset.parserOpts} : {}
                ).then((report) => {
                  report.warnings.forEach((warning) => {
                    core.warning(warning.message);
                  });

                  report.errors.forEach((error) => {
                    core.error(error.message);
                  });

                  if (!report.valid) {
                    core.setFailed("PR title linting failed");
                  }
                });
              });
