name: count activity by non-members this month

on:
  workflow_dispatch:

jobs:
  count-activity:
    runs-on: ubuntu-latest

    steps:
    - name: Checkout repository
      uses: actions/checkout@v4

    - name: Count issues and PRs activity by non-members with pagination
      env:
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      run: |
        # Function to paginate through API results
        function paginate() {
          url=$1
          all_data="[]"
          while [ -n "$url" ]; do
            response=$(curl -H "Authorization: token $GITHUB_TOKEN" -s -I "$url")
            data=$(curl -H "Authorization: token $GITHUB_TOKEN" -s "$url")
            all_data=$(echo "$all_data$data" | jq -s add)
            next_link=$(echo "$response" | grep -oP '(?<=<)(.*?)(?=>; rel="next")')
            url=${next_link:+$next_link}
          done
          echo "$all_data"
        }

        # Get the first day of the current month
        month_start=$(date -u +"%Y-%m-01T00:00:00Z")

        # Pagination for opened issues
        opened_issues_data=$(paginate "https://api.github.com/repos/${{ github.repository }}/issues?state=open&since=$month_start")
        opened_issues_count=$(echo "$opened_issues_data" | jq '[.[] | select(.author_association != "MEMBER") and select(.author_association != "OWNER") and (.pull_request == null)] | length')

        # Pagination for opened PRs
        opened_prs_data=$(paginate "https://api.github.com/repos/${{ github.repository }}/pulls?state=open&since=$month_start")
        opened_prs_count=$(echo "$opened_prs_data" | jq '[.[] | select(.author_association != "MEMBER") and select(.author_association != "OWNER")] | length')

        # Pagination for closed issues
        closed_issues_data=$(paginate "https://api.github.com/repos/${{ github.repository }}/issues?state=closed")
        closed_issues_count=$(echo "$closed_issues_data" | jq '[.[] | select(.closed_at >= "'$month_start'") and select(.author_association != "MEMBER") and select(.author_association != "OWNER") and (.pull_request == null)] | length')

        # Pagination for closed PRs
        closed_prs_data=$(paginate "https://api.github.com/repos/${{ github.repository }}/pulls?state=closed")
        closed_prs_count=$(echo "$closed_prs_data" | jq '[.[] | select(.closed_at >= "'$month_start'") and select(.author_association != "MEMBER") and select(.author_association != "OWNER")] | length')

        echo "Number of issues opened by non-members this month: $opened_issues_count"
        echo "Number of PRs opened by non-members this month: $opened_prs_count"
        echo "Number of issues closed by non-members this month: $closed_issues_count"
        echo "Number of PRs closed by non-members this month: $closed_prs_count"
