11#! /bin/bash
22set -euo pipefail
33
4- # 1. Check for Exemptions (Triage, Committers, Maintainers)
5- # Handle non-collaborator (404) and quote variables
6- PERM_JSON=$( gh api " repos/$REPO /collaborators/$ASSIGNEE /permission" 2> /dev/null || echo ' {"permission":"none"}' )
7- PERMISSION=$( echo " $PERM_JSON " | jq -r ' .permission // "none"' )
8-
9- if [[ " $PERMISSION " =~ ^(admin| write| triage)$ ]]; then
10- echo " User @$ASSIGNEE is a core member ($PERMISSION ). Qualification check skipped."
11- exit 0
12- fi
13-
14- # 2. Get counts of completed issues
15- # Use -f q= for native URL encoding
16- GFI_QUERY=" repo:$REPO is:issue is:closed assignee:$ASSIGNEE -reason:\" not planned\" label:\" good first issue\" "
17- INT_QUERY=" repo:$REPO is:issue is:closed assignee:$ASSIGNEE -reason:\" not planned\" label:\" intermediate\" "
18-
19- GFI_COUNT=$( gh api " search/issues" -f q=" $GFI_QUERY " --jq ' .total_count' || echo " 0" )
20- INT_COUNT=$( gh api " search/issues" -f q=" $INT_QUERY " --jq ' .total_count' || echo " 0" )
21-
22- # Robust numeric validation
23- if ! [[ " $GFI_COUNT " =~ ^[0-9]+$ ]]; then GFI_COUNT=0; fi
24- if ! [[ " $INT_COUNT " =~ ^[0-9]+$ ]]; then INT_COUNT=0; fi
25-
26- echo " Stats for @$ASSIGNEE : GFI: $GFI_COUNT , Intermediate: $INT_COUNT "
27-
28- # 3. Validation Logic
29- if (( GFI_COUNT >= 1 )) && (( INT_COUNT >= 1 )) ; then
30- echo " Qualification met."
31- exit 0
32- else
33- echo " Qualification failed. Revoking assignment."
4+ # Function to check a single user
5+ check_user () {
6+ local user=$1
7+ echo " Checking qualification for @$user ..."
8+
9+ # 1. Permission exemption
10+ PERM_JSON=$( gh api " repos/$REPO /collaborators/$user /permission" 2> /dev/null || echo ' {"permission":"none"}' )
11+ PERMISSION=$( echo " $PERM_JSON " | jq -r ' .permission // "none"' )
12+
13+ if [[ " $PERMISSION " =~ ^(admin| write| triage)$ ]]; then
14+ echo " User @$user is a core member ($PERMISSION ). Qualification check skipped."
15+ return 0
16+ fi
17+
18+ # 2. Get counts
19+ GFI_QUERY=" repo:$REPO is:issue is:closed assignee:$user -reason:\" not planned\" label:\" good first issue\" "
20+ INT_QUERY=" repo:$REPO is:issue is:closed assignee:$user -reason:\" not planned\" label:\" intermediate\" "
21+
22+ GFI_COUNT=$( gh api " search/issues" -f q=" $GFI_QUERY " --jq ' .total_count' || echo " 0" )
23+ INT_COUNT=$( gh api " search/issues" -f q=" $INT_QUERY " --jq ' .total_count' || echo " 0" )
24+
25+ if ! [[ " $GFI_COUNT " =~ ^[0-9]+$ ]]; then GFI_COUNT=0; fi
26+ if ! [[ " $INT_COUNT " =~ ^[0-9]+$ ]]; then INT_COUNT=0; fi
3427
35- # Quote variables to prevent word-splitting
36- gh issue edit " $ISSUE_NUMBER " --repo " $REPO " --remove-assignee " $ASSIGNEE "
28+ # 3. Validation Logic
29+ if (( GFI_COUNT >= 1 )) && (( INT_COUNT >= 1 )) ; then
30+ echo " User @$user qualified."
31+ return 0
32+ else
33+ echo " User @$user failed. Unassigning..."
3734
38- # 4. Message Construction
39- MSG=" Hi @$ASSIGNEE , I cannot assign you to this issue yet.
35+ # CodeRabbit Improvement: Tailor the suggestion based on what is missing
36+ if (( GFI_COUNT == 0 )) ; then
37+ SUGGESTION=" [good first issue](https://github.com/$REPO /labels/good%20first%20issue)"
38+ else
39+ SUGGESTION=" [intermediate issue](https://github.com/$REPO /labels/intermediate)"
40+ fi
41+
42+ # Remove the user
43+ gh issue edit " $ISSUE_NUMBER " --repo " $REPO " --remove-assignee " $user "
44+
45+ # Post the tailored message
46+ MSG=" Hi @$user , I cannot assign you to this issue yet.
4047
4148**Why?**
42- Advanced issues involve high-risk changes to the core codebase. They require significant testing and can impact automation and CI behavior. Because of this, we require developers to demonstrate familiarity with the repo first.
49+ Advanced issues involve high-risk changes to the core codebase. They require significant testing and can impact automation and CI behavior.
4350
4451**Requirement:**
4552- Complete at least **1** 'good first issue' (You have: **$GFI_COUNT **)
4653- Complete at least **1** 'intermediate' issue (You have: **$INT_COUNT **)
4754
48- Please feel free to pick up an [intermediate issue](https://github.com/$REPO /labels/intermediate) to get started! We look forward to your contributions."
55+ Please check out our **$SUGGESTION ** tasks to build your experience first!"
56+
57+ gh issue comment " $ISSUE_NUMBER " --repo " $REPO " --body " $MSG "
58+ fi
59+ }
4960
50- # Post the comment safely without printf formatting risks
51- gh issue comment " $ISSUE_NUMBER " --repo " $REPO " --body " $MSG "
61+ # --- Main Logic ---
62+
63+ # Define a simple log function so the script doesn't crash
64+ log () {
65+ echo " [advanced-check] $1 "
66+ }
67+
68+ # If TRIGGER_ASSIGNEE is set (from 'assigned' event), check just that person.
69+ # Otherwise (from 'labeled' event), check EVERYONE currently assigned.
70+ if [[ -n " ${TRIGGER_ASSIGNEE:- } " ]]; then
71+ check_user " $TRIGGER_ASSIGNEE "
72+ else
73+ log " Checking all current assignees..."
74+ # Fetch all current assignees using GitHub CLI
75+ ASSIGNEES=$( gh issue view " $ISSUE_NUMBER " --repo " $REPO " --json assignees --jq ' .assignees[].login' )
5276
53- exit 1
77+ for user in $ASSIGNEES ; do
78+ check_user " $user "
79+ done
5480fi
0 commit comments