name: eXo PR Task Webhook

on:
  pull_request:
    types: [opened, reopened, closed, review_requested]
  pull_request_review:
    types: [submitted]

env:
  message: ${{ github.event.pull_request.title }}
  state: ${{ github.event.pull_request.state }}
  pull_number: ${{ github.event.pull_request.number }}
  requested_reviewer: ${{ github.event.requested_reviewer.login }}
  creator: ${{ github.event.pull_request.user.login }}
  repo_name: ${{ github.event.repository.full_name }}
  base_branch_name: ${{ github.event.pull_request.base.ref }}
jobs:
  check_tasks:
    name: Check for eXo tasks identifiers
    runs-on: ubuntu-latest
    steps:
      - name: eXo Tasks Webhook
        run: |
          TASKS_IDS="$(echo ${message:-}| grep -oP '[0-9]{4,}' | xargs)"
          if [ -z "${TASKS_IDS}" ]; then
            echo "No tasks found! Abort."
            exit 0
          fi
          echo "OK Task(s) found! Starting notifications..."
          link="PR <a href=\"https://github.com/${repo_name}/pull/${pull_number}\">${repo_name}#${pull_number}</a>"
          if [ "${{ github.event_name }}" = "pull_request" ]; then
            if [ "${{ github.event.action }}" = "review_requested" ]; then
              tribeUser=$(curl -s -f -L -u ${TRIBE_USERNAME}:${TRIBE_PASSWORD} -XGET "${TRIBE_GAMGH_CONNECTOR_REST_URL}/users/${requested_reviewer}" || echo "")
              if [ ! -z "${tribeUser}" ]; then
                echo "Requested reviewer is: ${tribeUser}."
                msg="💭 $link requested a review from @${tribeUser} "
              else
                echo "Could not get Tribe user identifier! Abort"
                exit 0
              fi
            elif [ "${{ github.event.pull_request.merged }}" = "true" ]; then
              msg="🌟 $link has been merged into ${base_branch_name}."
            else
              msg="ℹ️ $link has been ${{ github.event.action }}."
            fi
          elif [ "${{ github.event_name }}" = "pull_request_review" ] && [ "${{ github.event.action }}" = "submitted" ]; then
            mentionCreator=""
            response=$(curl -s -f -L -u ${TRIBE_USERNAME}:${TRIBE_PASSWORD} -XGET "${TRIBE_GAMGH_CONNECTOR_REST_URL}/users/${creator}" || echo "")
            [ -z "${response}" ] || mentionCreator=" FYI @${response} "
            if [ "${{ github.event.review.state }}" = "changes_requested" ]; then
              msg="🛠️ $link requires some changes.${mentionCreator}"
            elif [ "${{ github.event.review.state }}" = "approved" ]; then
              msg="✅ $link has been ${{ github.event.review.state }}.${mentionCreator}"
            else
              msg="ℹ️ $link has been ${{ github.event.review.state }}."
            fi
          fi
          echo "*** Message is:"
          echo ${msg}
          echo "***"
          for TASK_ID in ${TASKS_IDS}; do
            echo "Commenting to Task #${TASK_ID}..."
            curl -s -L -u ${TRIBE_USERNAME}:${TRIBE_PASSWORD} -XPOST --data-urlencode "<p>${msg}</p>" "${TRIBE_TASK_REST_PREFIXE_URL}/comments/${TASK_ID}"
          done
        env:
          TRIBE_USERNAME: ${{ secrets.TRIBE_USERNAME }}
          TRIBE_PASSWORD: ${{ secrets.TRIBE_PASSWORD }}
          TRIBE_TASK_REST_PREFIXE_URL: ${{ secrets.TRIBE_TASK_REST_PREFIXE_URL }}
          TRIBE_GAMGH_CONNECTOR_REST_URL: ${{ secrets.TRIBE_GAMGH_CONNECTOR_REST_URL }}