Created
January 29, 2026 07:57
-
-
Save peers8862/72bd6f2e0043069f0fcc2a02aae40216 to your computer and use it in GitHub Desktop.
Replace org or username on line starting with ORG
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
| ~/bin/gh-discuss | |
| #!/usr/bin/env bash | |
| set -e | |
| ORG="babbworks" | |
| echo "Fetching repositories for org '$ORG'..." | |
| # Get public + private repos into array | |
| mapfile -t REPO_ARRAY < <( | |
| gh repo list "$ORG" --visibility public --json name --jq '.[].name' | |
| gh repo list "$ORG" --visibility private --json name --jq '.[].name' | |
| ) | |
| if [[ ${#REPO_ARRAY[@]} -eq 0 ]]; then | |
| echo "No repositories found in $ORG or you don’t have access." | |
| exit 1 | |
| fi | |
| # Numbered repo selection | |
| echo "Available repositories:" | |
| for i in "${!REPO_ARRAY[@]}"; do | |
| printf "%d) %s\n" $((i+1)) "${REPO_ARRAY[$i]}" | |
| done | |
| read -p "Enter repository number: " INDEX | |
| if ! [[ "$INDEX" =~ ^[0-9]+$ ]] || (( INDEX < 1 || INDEX > ${#REPO_ARRAY[@]} )); then | |
| echo "Invalid selection." | |
| exit 1 | |
| fi | |
| REPO_NAME="${REPO_ARRAY[$INDEX-1]}" | |
| REPO="$ORG/$REPO_NAME" | |
| # Fetch discussion categories into array | |
| mapfile -t CATEGORY_ARRAY < <(gh api repos/$REPO/discussions/categories --jq '.[].name') | |
| if [[ ${#CATEGORY_ARRAY[@]} -eq 0 ]]; then | |
| echo "No discussion categories found in $REPO." | |
| exit 1 | |
| fi | |
| # Numbered category selection | |
| echo "Available categories:" | |
| for i in "${!CATEGORY_ARRAY[@]}"; do | |
| printf "%d) %s\n" $((i+1)) "${CATEGORY_ARRAY[$i]}" | |
| done | |
| read -p "Enter category number: " CAT_INDEX | |
| if ! [[ "$CAT_INDEX" =~ ^[0-9]+$ ]] || (( CAT_INDEX < 1 || CAT_INDEX > ${#CATEGORY_ARRAY[@]} )); then | |
| echo "Invalid selection." | |
| exit 1 | |
| fi | |
| CATEGORY_NAME="${CATEGORY_ARRAY[$CAT_INDEX-1]}" | |
| CATEGORY_ID=$(gh api repos/$REPO/discussions/categories --jq ".[] | select(.name==\"$CATEGORY_NAME\") | .id") | |
| # Prompt for discussion title | |
| read -p "Enter discussion title: " TITLE | |
| if [[ -z "$TITLE" ]]; then | |
| echo "Title cannot be empty." | |
| exit 1 | |
| fi | |
| # Prompt for multi-line body | |
| echo "Enter discussion body. Press Ctrl+D when done:" | |
| BODY=$(cat) | |
| # Get repository ID | |
| REPO_ID=$(gh api graphql -f query="{ repository(owner: \"$ORG\", name: \"$REPO_NAME\") { id } }" --jq '.data.repository.id') | |
| # Create the discussion | |
| DISCUSSION_URL=$(gh api graphql -f query=" | |
| mutation { | |
| createDiscussion(input:{ | |
| repositoryId: \"$REPO_ID\", | |
| categoryId: \"$CATEGORY_ID\", | |
| title: \"$TITLE\", | |
| body: \"$BODY\" | |
| }) { | |
| discussion { url } | |
| } | |
| } | |
| " --jq '.data.createDiscussion.discussion.url') | |
| echo "Discussion created: $DISCUSSION_URL" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment