name: Close Discussion on PR Merge

on:
  pull_request:
    types: [closed]

jobs:
  closeDiscussion:
    runs-on: ubuntu-latest
    steps:
      - name: Check if PR was merged
        if: github.event.pull_request.merged == true
        run: echo "PR was merged"

      - name: Extract Discussion Number & Close If any Where Found
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        if: github.event.pull_request.merged == true
        id: extract-discussion
        run: |
          echo '${{ github.event.pull_request.body }}' > pr_body.txt

          discussion_ids_arr=()
          cat pr_body.txt | grep -i -Po '^\s*(-|\d+\.)?\s*(Resolve(s|d)?)\s*#\d+\s*$|^\s*(-|\d+\.)?\s*(Fix(es|ed)?)\s*#\d+\s*$|^\s*(-|\d+\.)?\s*(Close(s|d)?)\s*#\d+\s*$' | awk -F '#' '{print $2}' > discussion_ids_list.txt
          while read -r line; do
            discussion_ids_arr+=("$line")
          done < discussion_ids_list.txt

          number_of_ids=${#discussion_ids_arr[@]}
          for (( i=0; i<${number_of_ids}; i++ ));
          do
            discussion_id=${discussion_ids_arr[$i]}
            echo "$discussion_id"
            curl -X PATCH -H "Authorization: token $GITHUB_TOKEN" \
                 -d '{"state": "closed"}' \
                 'https://api.github.com/repos/${{ github.repository }}/discussions/$discussion_id'
          done
        shell: bash
