name: Pull request auto-labeller

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

concurrency:
  group: ${{ github.workflow }}-${{ github.ref == 'refs/heads/main' && github.run_id || github.event.pull_request.number || github.ref }}
  cancel-in-progress: true

jobs:
  validate:
    name: Validate PR title
    runs-on: ubuntu-latest
    permissions:
      pull-requests: write
    steps:
      - uses: amannn/action-semantic-pull-request@cfb60706e18bc85e8aec535e3c577abe8f70378e
        id: lint_pr_title
        env:
          GITHUB_TOKEN: ${{ secrets.GIT_MASTER_TOKEN }}
        with:
          types: |-
            build
            chore
            ci
            deprecate
            docs
            feat
            fix
            perf
            refactor
            remove
            revert
            security
            style
            test
          requireScope: false
          # Ensures the subject start with an uppercase character.
          subjectPattern: ^([A-Z]).+$
          headerPattern: '^\s*.*?\s(\w*)(?:\(([\w$.\-*/ ]*)\))?: (.*)$'
          headerPatternCorrespondence: type, scope, subject
          subjectPatternError: |
            The subject "{subject}" found in the pull request title "{title}"
            didn't match the configured pattern. Please ensure that the subject
            starts with an uppercase character
  
      - uses: marocchino/sticky-pull-request-comment@331f8f5b4215f0445d3c07b4967662a32a2d3e31
        # When the previous steps fails, the workflow would stop. By adding this
        # condition you can continue the execution with the populated error message.
        if: always() && (steps.lint_pr_title.outputs.error_message != null)
        with:
          header: pr-title-lint-error
          message: |
            Hey there and thank you for opening this pull request! 👋🏼
            
            We require pull request titles to follow the [Conventional Commits specification](https://www.conventionalcommits.org/en/v1.0.0/) and it looks like your proposed title needs to be adjusted.
            Additionally, the subject of the title must start with an uppercase character (e.g. feat: New `search` component).
            
            ```
            ${{ steps.lint_pr_title.outputs.error_message }}
            ```
      # Delete a previous comment when the issue has been resolved
      - if: ${{ steps.lint_pr_title.outputs.error_message == null }}
        uses: marocchino/sticky-pull-request-comment@331f8f5b4215f0445d3c07b4967662a32a2d3e31
        with:   
          header: pr-title-lint-error
          delete: true

  label:
    needs: [ validate ]
    runs-on: ubuntu-latest
    name: Add labels
    permissions:
      contents: read
      pull-requests: write
    steps:
      - name: Checkout your code
        uses: actions/checkout@v4

      - uses: srvaroa/labeler@v1
        with:
          config_path: .github/labeler.yml
          use_local_config: true #FIXME:
          fail_on_error: true
        env:
          GITHUB_TOKEN: "${{ secrets.GIT_MASTER_TOKEN }}"