# This action is centrally managed in https://github.com/asyncapi/.github/
# Don't make changes to this file in this repo as they will be overwritten with changes made to the same file in above mentioned repo

# It uses Github actions to listen for comments on issues and pull requests and 
# if the comment contains /please-take-a-look or /ptal it will add a comment pinging 
# the code-owners who are reviewers for PR

name: Please take a Look

on:
  issue_comment:
    types: [created]

jobs:
  ping-for-attention:
    if: >
      github.event.issue.pull_request &&
      github.event.issue.state != 'closed' &&
      github.actor != 'asyncapi-bot' &&
      (
        contains(github.event.comment.body, '/please-take-a-look') ||
        contains(github.event.comment.body, '/ptal') ||
        contains(github.event.comment.body, '/PTAL') 
      )
    runs-on: ubuntu-latest
    steps:
      - name: Check for Please Take a Look Command
        uses: actions/github-script@v7
        with:
          github-token: ${{ secrets.GH_TOKEN }}
          script: |
            const prDetailsUrl = context.payload.issue.pull_request.url;
            const { data: pull } = await github.request(prDetailsUrl);
            const reviewers = pull.requested_reviewers.map(reviewer => reviewer.login);

            const { data: reviews } = await github.rest.pulls.listReviews({
              owner: context.repo.owner,
              repo: context.repo.repo,
              pull_number: context.issue.number
            });

            const reviewersWhoHaveReviewed = reviews.map(review => review.user.login);

            const reviewersWhoHaveNotReviewed = reviewers.filter(reviewer => !reviewersWhoHaveReviewed.includes(reviewer));

            if (reviewersWhoHaveNotReviewed.length > 0) {
              const comment = reviewersWhoHaveNotReviewed.filter(reviewer => reviewer !== 'asyncapi-bot-eve' ).map(reviewer => `@${reviewer}`).join(' ');
              await github.rest.issues.createComment({
                issue_number: context.issue.number,
                owner: context.repo.owner,
                repo: context.repo.repo,
                body: `${comment} Please take a look at this PR. Thanks! :wave:`
              });
            }
