name: Check PR for configuration and SQL changes

on:
  pull_request:
    types: [review_requested]
    paths:
    - 'test/config-next/*.json'
    - 'test/config-next/*.yaml'
    - 'test/config-next/*.yml'
    - 'sa/db-users/*.sql'
    - 'sa/db-next/**/*.sql'
    - 'sa/db/**/*.sql'

jobs:
  check-changes:
    runs-on: ubuntu-latest
    permissions:
      pull-requests: write
    steps:
      - name: Comment PR
        uses: actions/github-script@v7
        with:
          script: |
            const commentMarker = '<!-- deployment_ticket_check -->';
            const prAuthor = context.payload.pull_request.user.login;
            const commentBody = `${commentMarker}\n@${prAuthor}, this PR appears to contain configuration and/or SQL schema changes. Please ensure that a corresponding deployment ticket has been filed with the new values.\n`;
            const { owner, repo, number: issue_number } = context.issue;
            const issueRegexp = /IN-\d+/;

            // Get PR body and all issue comments.
            const prBody = context.payload.pull_request.body;
            const comments = await github.rest.issues.listComments({
              owner,
              repo,
              issue_number
            });

            if (issueRegexp.test(prBody) || comments.data.some(c => issueRegexp.test(c.body))) {
                // Issue number exists in PR body or comments.
                return;
              }

            if (comments.data.find(c => c.body.includes(commentMarker))) {
              // Comment already exists.
              return;
            }

            // No issue number or comment were found, post the comment.
            await github.rest.issues.createComment({
              owner,
              repo,
              issue_number,
              body: commentBody
            });
          github-token: ${{ secrets.GITHUB_TOKEN }}
