This repository was archived by the owner on Sep 10, 2025. It is now read-only.
Invite Random GitHub User via Gemini #1291
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Invite Random GitHub User via Gemini | |
| on: | |
| schedule: | |
| - cron: "*/10 * * * *" # Every 10 minutes | |
| workflow_dispatch: | |
| permissions: | |
| contents: write # Needed to commit and push code | |
| id-token: write # (Optional, in case you use OIDC/GitHub Apps) | |
| # issues: write # Optional: if you ever automate issues | |
| jobs: | |
| invite: | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Checkout main branch | |
| uses: actions/checkout@v4 | |
| with: | |
| ref: main | |
| - name: Set up Python & jq | |
| run: sudo apt-get update && sudo apt-get install -y jq python3 | |
| - name: Configure Git identity | |
| run: | | |
| git config --global user.email "[email protected]" | |
| git config --global user.name "openrocketsofficial" | |
| - name: Generate GitHub username from Gemini | |
| id: gemini | |
| run: | | |
| echo "Calling Gemini 1.5 Flash to fetch one active GitHub user..." | |
| API_KEY="${{ secrets.GEMINI_API_KEY }}" | |
| PROMPT="Return a GitHub username of a random open-source contributor. Only return the username. No @ symbol, no quotes, no explanations." | |
| RESPONSE=$(curl -s -X POST \ | |
| -H "Content-Type: application/json" \ | |
| "https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash:generateContent?key=$API_KEY" \ | |
| -d "{ | |
| \"contents\": [{ | |
| \"role\": \"user\", | |
| \"parts\": [{\"text\": \"$PROMPT\"}] | |
| }] | |
| }") | |
| echo "Raw Gemini response: $RESPONSE" | |
| USERNAME=$(echo "$RESPONSE" | jq -r '.candidates[0].content.parts[0].text' | grep -oE '^[a-zA-Z0-9-]{1,39}' | sed 's/@//g' | xargs) | |
| if [[ "$USERNAME" == "null" || -z "$USERNAME" ]]; then | |
| echo "Gemini failed to return a valid username." | |
| exit 1 | |
| fi | |
| echo "USERNAME=$USERNAME" >> $GITHUB_ENV | |
| - name: Skip if username already invited | |
| run: | | |
| FILE="invited_users.json" | |
| USERNAME="${{ env.USERNAME }}" | |
| # Create the file if it doesn't exist | |
| if [ ! -f "$FILE" ]; then | |
| echo "[]" > "$FILE" | |
| fi | |
| if grep -q "\"$USERNAME\"" "$FILE"; then | |
| echo "User $USERNAME already invited. Skipping." | |
| exit 0 | |
| fi | |
| - name: Get GitHub User ID | |
| run: | | |
| USER_API_RESPONSE=$(curl -s https://api.github.com/users/${{ env.USERNAME }}) | |
| USER_ID=$(echo "$USER_API_RESPONSE" | jq -r '.id') | |
| if [ "$USER_ID" == "null" ] || [ -z "$USER_ID" ]; then | |
| echo "Could not fetch user ID for ${{ env.USERNAME }}" | |
| exit 1 | |
| fi | |
| echo "USER_ID=$USER_ID" >> $GITHUB_ENV | |
| - name: Invite user to organization | |
| run: | | |
| ORG_NAME="openrockets" # Replace with your GitHub org name | |
| curl -X POST \ | |
| -H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \ | |
| -H "Accept: application/vnd.github.v3+json" \ | |
| https://api.github.com/orgs/$ORG_NAME/invitations \ | |
| -d "{\"invitee_id\": \"${{ env.USER_ID }}\"}" | |
| - name: Append to invited_users.json | |
| run: | | |
| echo "Adding ${{ env.USERNAME }} to invited_users.json" | |
| FILE="invited_users.json" | |
| tmp=$(mktemp) | |
| jq ". + [\"${{ env.USERNAME }}\"]" "$FILE" > "$tmp" && mv "$tmp" "$FILE" | |
| - name: Commit and push updated invited_users.json using PAT | |
| env: | |
| PAT_TOKEN: ${{ secrets.PAT_TOKEN }} | |
| run: | | |
| git remote set-url origin https://openrocketsofficial:${PAT_TOKEN}@github.com/OpenRockets/web-dev-error-solutions.git | |
| git add invited_users.json | |
| git commit -m "Add ${{ env.USERNAME }} to invited list" || echo "No changes to commit." | |
| git push origin main |