# validate either actual commits or pr title
# src: https://raw.githubusercontent.com/patternfly/patternfly-elements/24dd236afbbc90045165fd33ddaf036e56e26cd7/.github/workflows/commitlint.yml

name: commitlint

on:
  pull_request:
    types:
      - opened
      - edited
      - labeled
      - auto_merge_enabled
      - synchronize
      - reopened
      - ready_for_review

  pull_request_review:
    types:
      - submitted

permissions:
  contents: read
  pull-requests: read

jobs:
  pr-title:
    runs-on: ubuntu-latest

    steps:

      - uses: actions/checkout@v3
        with:
          fetch-depth: 0

      - uses: wagoid/commitlint-github-action@v5
        id: commitlint
        continue-on-error: true
        with:
          configFile: .commitlint.config.js

      - name: Validate PR Title
        id: prTitle
        if: {% raw %}${{ steps.commitlint.outcome == 'failure' }}{% endraw %}
        uses: aslafy-z/conventional-pr-title-action@v2.2.0
        continue-on-error: true
        with:
          success-state: Title follows the conventional commit format.
          failure-state: Please update the title to use conventional commit format.
          context-name: conventional-pr-title
          preset: '@commitlint/config-conventional@latest'
        env:
          GITHUB_TOKEN: {% raw %}${{ secrets.GITHUB_TOKEN }}{% endraw %}

      - name: Format Commitlint Messages
        id: format
        if: {% raw %}${{ steps.commitlint.outcome == 'failure' }}{% endraw %}
        uses: actions/github-script@v6
        with:
          github-token: {% raw %}${{ secrets.GITHUB_TOKEN }}{% endraw %}
          script: |
            const results = {% raw %}${{ steps.commitlint.outputs.results }};{% endraw %}
            const titleGood = {% raw %}${{ steps.prTitle.outcome == 'success' }};{% endraw %}

            const totalErrors = results.reduce((a, r) => a + r.errors.length, 0);
            const totalWarnings = results.reduce((a, r) => a + r.warnings.length, 0);

            const header = `
            ## ?? Commitlint Problems for this PR: ${!titleGood ? '' : `

            **The PR title conforms to conventional commit style**

            ?? **Make sure the PR is squashed into a single commit using the PR title** ??`}

            ??  Found ${totalErrors} errors, ${totalWarnings} warnings
            ??   Get help: https://github.com/conventional-changelog/commitlint/#what-is-commitlint
            `;

            const comment = results.reduce((acc, result) => {
              if (!result.errors.length && !result.warnings.length) {
                return acc;
              }

              const [firstLine, ...rest] = result.message.split('\n');

              const body = rest.join('\n').trim();
              const icon = result.errors.length ? '?' : '??';
              const hash = result.hash.substring(0, 8);

              return `${acc} ${!body.length ? '\n\n' : `

            <details><summary>`}
            ${hash} - ${firstLine} ${!body.length ? '\n\n' : `\
            </summary>

            ${body}

            </details>

            `}${result.errors.map(error => `
            - ? ${error}`).join('')} ${result.warnings.map(warn => `
            - ?? ${warn}`).join('')}`;
            }, header);

            core.setOutput('comment', comment);

      - name: Update PR
        if: {% raw %}${{ steps.commitlint.outcome == 'failure' && steps.prTitle.outcome == 'failure' }}{% endraw %}
        id: comment
        uses: marocchino/sticky-pull-request-comment@v2
        with:
          GITHUB_TOKEN: {% raw %}${{ secrets.GITHUB_TOKEN }}{% endraw %}
          header: commitlint
          message: |
            {% raw %}${{ steps.format.outputs.comment }}{% endraw %}

      - name: Fail Job on Error
        if: {% raw %}${{ steps.commitlint.outcome == 'failure' && steps.prTitle.outcome == 'failure' }}{% endraw %}
        uses: actions/github-script@v6
        with:
          script: |
            core.setFailed("There were problems with this PR's commit messages")