# Verify if the PR meets all the requirements for merging on PR review event.

name: PR Pre-merge Check

on:
  pull_request_target:
    types: [opened, synchronize, reopened, edited]

jobs:
  validate_PR:

    runs-on: ubuntu-latest

    steps:
      - name: Label PR if ready to be merged.
        uses: actions/github-script@v3
        with:
          github-token: ${{secrets.GITHUB_TOKEN}}
          script: |
            const approvalLabel = 'CheckedAndApproved'

            const reviews = await github.pulls.listReviews({
              pull_number: context.payload.pull_request.number,
              owner: context.repo.owner,
              repo: context.repo.repo
            })

            // Check if any recent APPROVED or CHANGES_REQUESTED reviews.
            var approved = false
            for (const review_info of reviews.data) {
              if (review_info.author_association == 'NONE' || review_info.author_association == 'FIRST_TIMER' || review_info.author_association == 'FIRST_TIME_CONTRIBUTOR') {
                continue
              }
              if (review_info.state == 'APPROVED') {
                approved = true
              } else if (review_info.state == 'CHANGES_REQUESTED') {
                approved = false
              }
            }

            if (approved) {
              console.log("This PR has been approved.")
              github.issues.addLabels({
                issue_number: context.payload.pull_request.number,
                owner: context.repo.owner,
                repo: context.repo.repo,
                labels: [approvalLabel]
              })
            } else {
              console.log("This PR has not been approved.")
              const labels_info = await github.issues.listLabelsOnIssue({
                issue_number: context.payload.pull_request.number,
                owner: context.repo.owner,
                repo: context.repo.repo
              })

              var approvalLabelExist = false
              for (const label of labels_info.data) {
                if (label.name == approvalLabel) {
                  approvalLabelExist = true
                  break
                }
              }
              if (approvalLabelExist) {
                github.issues.removeLabel({
                  issue_number: context.payload.pull_request.number,
                  owner: context.repo.owner,
                  repo: context.repo.repo,
                  name: approvalLabel
                })
              }
            }
        if: ${{ (!contains(github.event.pull_request.body, '[ ]') || contains(github.event.pull_request.labels.*.name, 'CheckedAndApproved')) && (github.event.pull_request.state == 'open') }}
