1 #!/usr/bin/env bash
2 #
3 # Converts a GitHub issue to a discussion.
4 # Usage: ./scripts/convert_to_discussion.sh [issue_number] [category_name]
5 #
6 # issue_number: optional, reads from workflow event payload if omitted
7 # category_name: optional, name of the discussion category (e.g. "Feature Requests", "Support")
8 # Falls back to "General", then the first available category.
9 #
10 # Requires the GITHUB_TOKEN to have the necessary permissions.
11 #
12
13 set -euo pipefail
14
15 # Parse args: first numeric arg is issue number, remaining string is category
16 ISSUE=""
17 CATEGORY_NAME=""
18
19 if [[ $# -ge 1 && "$1" =~ ^[0-9]+$ ]]; then
20 ISSUE="$1"
21 shift
22 fi
23
24 if [[ $# -ge 1 ]]; then
25 CATEGORY_NAME="$1"
26 fi
27
28 if [[ -z "$ISSUE" ]]; then
29 ISSUE=$(jq -r '.issue.number // empty' "${GITHUB_EVENT_PATH:?GITHUB_EVENT_PATH not set}")
30 fi
31
32 if ! [[ "$ISSUE" =~ ^[0-9]+$ ]]; then
33 echo "Error: no valid issue number provided or found in event payload" >&2
34 exit 1
35 fi
36
37 REPO="${GITHUB_REPOSITORY:?GITHUB_REPOSITORY not set}"
38
39 # Get the issue node ID
40 ISSUE_NODE_ID=$(gh api "repos/${REPO}/issues/${ISSUE}" --jq '.node_id')
41 if [[ -z "$ISSUE_NODE_ID" ]]; then
42 echo "Error: could not retrieve node ID for issue #$ISSUE" >&2
43 exit 1
44 fi
45
46 # Get all discussion categories
47 REPO_DATA=$(gh api graphql -f query='
48 query($owner: String!, $repo: String!) {
49 repository(owner: $owner, name: $repo) {
50 discussionCategories(first: 25) {
51 nodes { id name }
52 }
53 }
54 }
55 ' -f owner="${REPO%%/*}" -f repo="${REPO##*/}")
56
57 # Resolve category: requested name → "General" → first available
58 if [[ -n "$CATEGORY_NAME" ]]; then
59 CATEGORY_ID=$(echo "$REPO_DATA" | jq -r --arg name "$CATEGORY_NAME" '
60 .data.repository.discussionCategories.nodes
61 | map(select(.name == $name))[0].id // empty
62 ')
63 if [[ -z "$CATEGORY_ID" ]]; then
64 echo "Warning: category '$CATEGORY_NAME' not found, falling back to 'General'" >&2
65 fi
66 fi
67
68 if [[ -z "${CATEGORY_ID:-}" ]]; then
69 CATEGORY_ID=$(echo "$REPO_DATA" | jq -r '
70 .data.repository.discussionCategories.nodes
71 | map(select(.name == "General"))[0].id
72 // .data.repository.discussionCategories.nodes[0].id
73 ')
74 fi
75
76 if [[ -z "$CATEGORY_ID" ]]; then
77 echo "Error: could not find a discussion category in the repo" >&2
78 exit 1
79 fi
80
81 # Convert the issue to a discussion via GraphQL mutation
82 gh api graphql -f query='
83 mutation($issueId: ID!, $categoryId: ID!) {
84 convertIssueToDiscussion(
85 input: { issueId: $issueId, categoryId: $categoryId }
86 ) {
87 discussion { url }
88 }
89 }
90 ' -f issueId="$ISSUE_NODE_ID" -f categoryId="$CATEGORY_ID"
91
92 echo "Converted issue #$ISSUE to a discussion"