Skip to content

Commit 0977ff2

Browse files
committed
bot: update
Signed-off-by: Akshat Kumar <[email protected]>
1 parent a91df3a commit 0977ff2

File tree

2 files changed

+67
-51
lines changed

2 files changed

+67
-51
lines changed
Lines changed: 65 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,80 @@
11
#!/bin/bash
22
set -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
5480
fi

.github/workflows/bot-advanced-check.yml

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,17 @@ name: PythonBot - Advanced Requirement Check
22

33
on:
44
issues:
5-
types: [assigned, labeled] # Added 'labeled' to catch label changes after assignment
5+
types: [assigned, labeled]
66

7-
# Explicitly minimal permissions
87
permissions:
98
issues: write
109

11-
# Prevent duplicate runners/comments on the same issue
1210
concurrency:
1311
group: ${{ github.workflow }}-${{ github.event.issue.number }}
1412
cancel-in-progress: true
1513

1614
jobs:
1715
check-advanced-qualification:
18-
# Skip the runner entirely if the issue isn't 'advanced'
1916
if: contains(github.event.issue.labels.*.name, 'advanced')
2017
runs-on: ubuntu-latest
2118
steps:
@@ -27,16 +24,9 @@ jobs:
2724
- name: Verify User Qualification
2825
env:
2926
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
30-
# Handle cases where triggered by assignment or by adding a label to an assigned issue
31-
ASSIGNEE: ${{ github.event.assignee.login || github.event.issue.assignee.login }}
27+
TRIGGER_ASSIGNEE: ${{ github.event.assignee.login || '' }}
3228
ISSUE_NUMBER: ${{ github.event.issue.number }}
3329
REPO: ${{ github.repository }}
3430
run: |
35-
# If no one is assigned, exit early
36-
if [ -z "$ASSIGNEE" ]; then
37-
echo "No assignee to check. Skipping."
38-
exit 0
39-
fi
40-
4131
chmod +x .github/scripts/check_advanced_requirement.sh
4232
./.github/scripts/check_advanced_requirement.sh

0 commit comments

Comments
 (0)