name: Fixes validation for backport PR

on:
  pull_request:
    types: [opened, reopened, edited]
    branches: [branch-*]

jobs:
  check-fixes-prefix:
    runs-on: ubuntu-latest
    permissions:
      contents: read
      issues: write
    steps:
      - name: Check PR body for "Fixes" prefix patterns
        uses: actions/github-script@v7
        with:
          script: |
            const body = context.payload.pull_request.body;
            const repo = context.payload.repository.full_name;
            
            // Regular expression pattern to check for "Fixes" prefix
            // Adjusted to dynamically insert the repository full name
            const pattern = `Fixes:? ((?:#|${repo.replace('/', '\\/')}#|https://github\\.com/${repo.replace('/', '\\/')}/issues/)(\\d+)|(?:https://scylladb\\.atlassian\\.net/browse/)?([A-Z]+-\\d+))`;
            const regex = new RegExp(pattern);
            
            if (!regex.test(body)) {
              const error = "PR body does not contain a valid 'Fixes' reference.";
              core.setFailed(error);
              await github.rest.issues.createComment({
                issue_number: context.issue.number,
                owner: context.repo.owner,
                repo: context.repo.repo,
                body: `:warning: ${error}`
              });
            }
