Setting the file. One moment.
Query Discussions · GitHub Discussion Query · github/gh-aw · Skills Docs
ContentsBack to the top of the page
Raw file
query-discussions.sh
Shell · 105 lines · 3 KB
15
16 set -e
17
18 # Default values
19 REPO = ""
20 LIMIT = 30
21 JQ_FILTER = ""
22
23 # Parse arguments
24 while [[ $# -gt 0 ]]; do
25 case $1 in
26 --repo )
27 REPO = " $2 "
28 shift 2
29 ;;
30 --limit )
31 LIMIT = " $2 "
32 shift 2
33 ;;
34 --jq )
35 JQ_FILTER = " $2 "
36 shift 2
37 ;;
38 *)
39 echo "Unknown option: $1 " >&2
40 exit 1
41 ;;
42 esac
43 done
44
45 # JSON fields to fetch
46 JSON_FIELDS = "number,title,author,createdAt,updatedAt,body,category,labels,comments,answer,url"
47
48 # Build and execute gh command with proper quoting
49 if [[ -n " $REPO " ]]; then
50 OUTPUT = $( gh discussion list --limit " $LIMIT " --json " $JSON_FIELDS " --repo " $REPO " )
51 else
52 OUTPUT = $( gh discussion list --limit " $LIMIT " --json " $JSON_FIELDS " )
53 fi
54
55 # Apply jq filter if specified
56 if [[ -n " $JQ_FILTER " ]]; then
57 echo " $OUTPUT " | jq " $JQ_FILTER "
58 else
59 # Return schema and size instead of full data
60 ITEM_COUNT = $( jq 'length' <<< " $OUTPUT " )
61 DATA_SIZE = ${ # OUTPUT}
62
63 # Validate values are numeric
64 if ! [[ " $ITEM_COUNT " =~ ^[0-9]+$ ]]; then
65 ITEM_COUNT = 0
66 fi
67 if ! [[ " $DATA_SIZE " =~ ^[0-9]+$ ]]; then
68 DATA_SIZE = 0
69 fi
70
71 cat << EOF
72 {
73 "message": "No --jq filter provided. Use --jq to filter and retrieve data.",
74 "item_count": $ITEM_COUNT ,
75 "data_size_bytes": $DATA_SIZE ,
76 "schema": {
77 "type": "array",
78 "description": "Array of discussion objects",
79 "item_fields": {
80 "number": "integer - Discussion number",
81 "title": "string - Discussion title",
82 "author": "object - Author info with login field",
83 "createdAt": "string - ISO timestamp of creation",
84 "updatedAt": "string - ISO timestamp of last update",
85 "body": "string - Discussion body content",
86 "category": "object - Category info with name field",
87 "labels": "array - Array of label objects with name field",
88 "comments": "object - Comments info with totalCount field",
89 "answer": "object|null - Accepted answer if exists",
90 "url": "string - Discussion URL"
91 }
92 },
93 "suggested_queries": [
94 {"description": "Get all data", "query": "."},
95 {"description": "Get discussion numbers and titles", "query": ".[] | {number, title}"},
96 {"description": "Get discussions by author", "query": ".[] | select(.author.login == \" USERNAME \" )"},
97 {"description": "Get discussions in category", "query": ".[] | select(.category.name == \" Ideas \" )"},
98 {"description": "Get answered discussions", "query": ".[] | select(.answer != null)"},
99 {"description": "Get unanswered discussions", "query": ".[] | select(.answer == null) | {number, title, category: .category.name}"},
100 {"description": "Get discussions with labels", "query": ".[] | {number, title, labels: [.labels[].name]}"},
101 {"description": "Count by category", "query": "group_by(.category.name) | map({category: .[0].category.name, count: length})"}
102 ]
103 }
104 EOF
105 fi