|
| 1 | +#!/bin/bash |
| 2 | +set -euo pipefail |
| 3 | + |
| 4 | +# Mentor Assignment Script for New Contributors |
| 5 | +# This script automatically assigns a mentor from the triage team to new contributors |
| 6 | +# working on "good first issue" labeled issues. |
| 7 | +# |
| 8 | +# Environment Variables: |
| 9 | +# ASSIGNEE - GitHub username of the issue assignee |
| 10 | +# ISSUE_NUMBER - Issue number |
| 11 | +# REPO - Repository in format owner/repo |
| 12 | +# DRY_RUN - If set to "1", only logs actions without posting comments |
| 13 | + |
| 14 | +# Default DRY_RUN to 0 if not set |
| 15 | +DRY_RUN="${DRY_RUN:-0}" |
| 16 | + |
| 17 | +if [ -z "$ASSIGNEE" ] || [ -z "$ISSUE_NUMBER" ] || [ -z "$REPO" ]; then |
| 18 | + echo "Error: Missing required environment variables (ASSIGNEE, ISSUE_NUMBER, REPO)." |
| 19 | + exit 1 |
| 20 | +fi |
| 21 | + |
| 22 | +echo "Processing mentor assignment for user $ASSIGNEE on issue #$ISSUE_NUMBER" |
| 23 | +echo "DRY_RUN mode: $DRY_RUN" |
| 24 | + |
| 25 | +# Triage team configuration |
| 26 | +TRIAGE_ORG="hiero-ledger" |
| 27 | +TRIAGE_TEAM_SLUG="hiero-sdk-python-triage" |
| 28 | +TRIAGE_TEAM="$TRIAGE_ORG/$TRIAGE_TEAM_SLUG" |
| 29 | + |
| 30 | +# Check if the issue has "good first issue" label |
| 31 | +LABELS_JSON=$(gh issue view "$ISSUE_NUMBER" --repo "$REPO" --json labels) |
| 32 | +HAS_GFI_LABEL=$(echo "$LABELS_JSON" | jq -r '.labels[] | select(.name == "good first issue") | .name') |
| 33 | + |
| 34 | +if [ -z "$HAS_GFI_LABEL" ]; then |
| 35 | + echo "Issue #$ISSUE_NUMBER does not have 'good first issue' label. Skipping mentor assignment." |
| 36 | + exit 0 |
| 37 | +fi |
| 38 | + |
| 39 | +echo "Issue has 'good first issue' label. Checking if $ASSIGNEE is a new contributor..." |
| 40 | + |
| 41 | +# Check if user is a maintainer/committer (they don't need a mentor) |
| 42 | +PERM_JSON=$(gh api "repos/$REPO/collaborators/$ASSIGNEE/permission" 2>/dev/null || echo '{"permission":"none"}') |
| 43 | +PERMISSION=$(echo "$PERM_JSON" | jq -r '.permission') |
| 44 | + |
| 45 | +echo "User permission level: $PERMISSION" |
| 46 | + |
| 47 | +if [[ "$PERMISSION" == "admin" ]] || [[ "$PERMISSION" == "write" ]]; then |
| 48 | + echo "User is a maintainer or committer. No mentor needed." |
| 49 | + exit 0 |
| 50 | +fi |
| 51 | + |
| 52 | +# Check if user has any merged PRs (indicating they're not a new contributor) |
| 53 | +MERGED_PRS=$(gh pr list --repo "$REPO" --author "$ASSIGNEE" --state merged --limit 1 --json number | jq '. | length') |
| 54 | + |
| 55 | +if [ "$MERGED_PRS" -gt 0 ]; then |
| 56 | + echo "User $ASSIGNEE has previously merged PRs. Not a new contributor, skipping mentor assignment." |
| 57 | + exit 0 |
| 58 | +fi |
| 59 | + |
| 60 | +echo "User $ASSIGNEE is a new contributor. Assigning a mentor..." |
| 61 | + |
| 62 | +# Try to get team members via API (requires appropriate permissions) |
| 63 | +MENTORS_JSON=$(gh api "orgs/$TRIAGE_ORG/teams/$TRIAGE_TEAM_SLUG/members" 2>/dev/null || echo "[]") |
| 64 | +MENTORS_COUNT=$(echo "$MENTORS_JSON" | jq '. | length') |
| 65 | + |
| 66 | +if [ "$MENTORS_COUNT" -eq 0 ]; then |
| 67 | + echo "Could not fetch team members. Using team mention instead." |
| 68 | + MENTOR="@$TRIAGE_TEAM" |
| 69 | + MENTOR_MENTION="$TRIAGE_TEAM" |
| 70 | +else |
| 71 | + # Select a random mentor from the list (excluding the assignee) |
| 72 | + AVAILABLE_MENTORS=$(echo "$MENTORS_JSON" | jq -r --arg assignee "$ASSIGNEE" '[.[] | select(.login != $assignee) | .login]') |
| 73 | + AVAILABLE_COUNT=$(echo "$AVAILABLE_MENTORS" | jq '. | length') |
| 74 | + |
| 75 | + if [ "$AVAILABLE_COUNT" -eq 0 ]; then |
| 76 | + echo "No available mentors found." |
| 77 | + MENTOR="@$TRIAGE_TEAM" |
| 78 | + MENTOR_MENTION="$TRIAGE_TEAM" |
| 79 | + else |
| 80 | + # Select random mentor |
| 81 | + RANDOM_INDEX=$((RANDOM % AVAILABLE_COUNT)) |
| 82 | + MENTOR=$(echo "$AVAILABLE_MENTORS" | jq -r ".[$RANDOM_INDEX]") |
| 83 | + MENTOR_MENTION="@$MENTOR" |
| 84 | + echo "Selected mentor: $MENTOR" |
| 85 | + fi |
| 86 | +fi |
| 87 | + |
| 88 | +# Post welcome message with mentor assignment |
| 89 | +WELCOME_MSG=$(cat <<EOF |
| 90 | +👋 **Welcome to the Hiero Python SDK, @$ASSIGNEE!** |
| 91 | +
|
| 92 | +We're excited to have you contribute to our project! You are now officially assigned to this issue. |
| 93 | +
|
| 94 | +📚 **Before you begin, please:** |
| 95 | +1. Read the issue description carefully |
| 96 | +2. Check out our [Contributing Guide](https://github.com/hiero-ledger/hiero-sdk-python/blob/main/CONTRIBUTING.md) |
| 97 | +3. Review the [DCO signing guide](https://github.com/hiero-ledger/hiero-sdk-python/blob/main/docs/sdk_developers/signing.md) |
| 98 | +
|
| 99 | +🧑🏫 **Your Mentor:** $MENTOR_MENTION |
| 100 | +
|
| 101 | +We've assigned a mentor from our triage team to help guide you through your first contribution. Feel free to reach out to them by mentioning them in the comments if you have any questions or need assistance. |
| 102 | +
|
| 103 | +💡 **Helpful Resources:** |
| 104 | +- [Changelog Guide](https://github.com/hiero-ledger/hiero-sdk-python/blob/main/docs/sdk_developers/changelog_entry.md) |
| 105 | +- [Testing Guide](https://github.com/hiero-ledger/hiero-sdk-python/blob/main/docs/sdk_developers/testing.md) |
| 106 | +- [Discord Community](https://github.com/hiero-ledger/hiero-sdk-python/blob/main/docs/discord.md) |
| 107 | +- [Community Calls](https://zoom-lfx.platform.linuxfoundation.org/meetings/hiero?view=week) |
| 108 | +
|
| 109 | +⭐ If you enjoy working on this project, don't forget to [star the repository](https://github.com/hiero-ledger/hiero-sdk-python)! |
| 110 | +
|
| 111 | +Best of luck with your contribution! |
| 112 | +*From the Hiero Python SDK Team* 🚀 |
| 113 | +EOF |
| 114 | +) |
| 115 | + |
| 116 | +# Check if we've already posted a welcome message for this user on this issue |
| 117 | +# Uses the bot's username and checks for the welcome message pattern |
| 118 | +EXISTING_WELCOME=$(gh issue view "$ISSUE_NUMBER" --repo "$REPO" --comments --json comments \ |
| 119 | + | jq -r --arg assignee "$ASSIGNEE" ' |
| 120 | + .comments[] |
| 121 | + | select(.body | test("Welcome to the Hiero Python SDK,?\\s*@" + $assignee)) |
| 122 | + | .id' | head -1) |
| 123 | + |
| 124 | +if [ -n "$EXISTING_WELCOME" ]; then |
| 125 | + echo "Welcome message already posted for $ASSIGNEE on issue #$ISSUE_NUMBER. Skipping." |
| 126 | + exit 0 |
| 127 | +fi |
| 128 | + |
| 129 | +if [ "$DRY_RUN" = "1" ]; then |
| 130 | + echo "=== DRY RUN MODE ===" |
| 131 | + echo "Would post the following welcome message to issue #$ISSUE_NUMBER:" |
| 132 | + echo "---" |
| 133 | + echo "$WELCOME_MSG" |
| 134 | + echo "---" |
| 135 | + echo "DRY RUN: No comment posted." |
| 136 | +else |
| 137 | + echo "Posting welcome message..." |
| 138 | + gh issue comment "$ISSUE_NUMBER" --repo "$REPO" --body "$WELCOME_MSG" |
| 139 | + echo "Mentor assignment complete!" |
| 140 | +fi |
0 commit comments