name: ⚔️ Check Merge Conflicts

on:
  # So that PRs touching the same files as the push are updated
  push:
  # So that the `dirtyLabel` is removed if conflicts are resolved
  pull_request_target:
    types: [synchronize]

permissions:
  contents: read
  pull-requests: write

jobs:
  check-merge-conflicts:
    name: ⚔️ Check Merge Conflicts
    runs-on: ubuntu-latest
    if: always()

    steps:
      - name: Mint App Token
        id: app
        # Create a GitHub App token
        # https://github.com/marketplace/actions/create-github-app-token
        uses: actions/create-github-app-token@fee1f7d63c2ff003460e3d139729b119787bc349 # v2
        with:
          app-id: ${{ vars.ST_BOT_APP_ID }}
          private-key: ${{ secrets.ST_BOT_PRIVATE_KEY }}
          owner: ${{ github.repository_owner }}

      - name: Check Merge Conflicts
        # Label Conflicting Pull Requests
        # https://github.com/marketplace/actions/label-conflicting-pull-requests
        uses: eps1lon/actions-label-merge-conflict@1df065ebe6e3310545d4f4c4e862e43bdca146f0 # v3.0.3
        with:
          dirtyLabel: '🚫 Merge Conflicts'
          repoToken: ${{ steps.app.outputs.token }}
          commentOnDirty: >
            ⚠️ This PR has conflicts that need to be resolved before it can be merged.
