Skip to content

Commit 1562a57

Browse files
Merge pull request #90 from joshjohanning/add-sub-issue-parent-scripts
Add sub issue and parent scripts
2 parents 4956a65 + 1071c7e commit 1562a57

6 files changed

+226
-2
lines changed

gh-cli/README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -912,6 +912,10 @@ Retrieve the download URL for a specific version of a package in GitHub Packages
912912
> [!NOTE]
913913
> No longer works for GitHub.com and deprecated for GHES 3.7+. See [Changelog post](https://github.blog/changelog/2022-08-18-deprecation-notice-graphql-for-packages/), [GraphQL breaking changes](https://docs.github.com/en/graphql/overview/breaking-changes#changes-scheduled-for-2022-11-21-1), and [GHES 3.7 deprecations](https://docs.github.com/en/enterprise-server@3.7/admin/release-notes#3.7.0-deprecations)
914914
915+
### get-parent-issue-of-issue.sh
916+
917+
Gets the parent issue of a given sub-issue (child). See: [Community Discussions Post](https://github.com/orgs/community/discussions/139932)
918+
915919
### get-projects-added-to-repository.sh
916920

917921
Gets ProjectsV2 added to a repository
@@ -1088,6 +1092,14 @@ Retrieves all SSO enabled PATs users have created for an organization.
10881092

10891093
Retrieves all SSO-enabled SSH keys users have created for an organization.
10901094

1095+
### get-sub-issue-summary-of-issue.sh
1096+
1097+
Gets a summary of the sub-issues (children) of an issue (parent). See: [Community Discussions Post](https://github.com/orgs/community/discussions/139932)
1098+
1099+
### get-sub-issues-of-issue.sh
1100+
1101+
Gets the sub-issues (children) of an issue (parent). See: [Community Discussions Post](https://github.com/orgs/community/discussions/139932)
1102+
10911103
### get-user-id.sh
10921104

10931105
Retrieves the ID of a user for other GraphQL calls

gh-cli/add-sub-issue-to-issue.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ parent_issue_id=$(fetch_issue_id "$org" "$repo" "$parent_issue_number")
4747
# Fetch the child issue ID given the issue number
4848
child_issue_id=$(fetch_issue_id "$org" "$repo" "$child_issue_number")
4949

50-
# Set the issue type on the issue
50+
# Add the sub-issue to the parent issue
5151
gh api graphql -H GraphQL-Features:issue_types -H GraphQL-Features:sub_issues -f parrentIssueId="$parent_issue_id" -f childIssueId="$child_issue_id" -f query='
5252
mutation($parrentIssueId: ID!, $childIssueId: ID!) {
5353
addSubIssue(input: { issueId: $parrentIssueId, subIssueId: $childIssueId }) {

gh-cli/get-parent-issue-of-issue.sh

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
#!/bin/bash
2+
3+
# Gets the parent issue of an issue
4+
5+
if [ -z "$3" ]; then
6+
echo "Usage: $0 <org> <repo> <issue-number>"
7+
echo "Example: ./get-parent-issue-of-issue.sh joshjohanning-org migrating-ado-to-gh-issues-v2 5"
8+
exit 1
9+
fi
10+
11+
org="$1"
12+
repo="$2"
13+
issue_number="$3"
14+
15+
# Define color codes
16+
RED='\033[0;31m'
17+
NC='\033[0m' # No Color
18+
19+
# Fetch the issue ID given the issue number
20+
issue_id=$(gh api graphql -f owner="$org" -f repository="$repo" -F number="$issue_number" -f query='
21+
query ($owner: String!, $repository: String!, $number: Int!) {
22+
repository(owner: $owner, name: $repository) {
23+
issue(number: $number) {
24+
id
25+
}
26+
}
27+
}' --jq '.data.repository.issue.id')
28+
29+
# Check if the query was successful
30+
if [ $? -ne 0 ]; then
31+
echo -e "${RED}Issue #$issue_number not found in $org/$repo${NC}"
32+
exit 1
33+
fi
34+
35+
# Get the parent issue for the issue
36+
parent_issue=$(gh api graphql -H GraphQL-Features:sub_issues -H GraphQL-Features:issue_types -f issueId="$issue_id" -f query='
37+
query($issueId: ID!) {
38+
node(id: $issueId) {
39+
... on Issue {
40+
parent {
41+
title
42+
number
43+
url
44+
issueType {
45+
name
46+
}
47+
}
48+
}
49+
}
50+
}')
51+
52+
# Check if the gh api graphql command was successful
53+
if [ $? -ne 0 ]; then
54+
echo -e "${RED}Failed to get the parent issue for $org/$repo#$issue_number.${NC}"
55+
exit 1
56+
fi
57+
58+
# Extract and format the parent issue details using jq
59+
formatted_parent_issue=$(echo "$parent_issue" | jq -r '
60+
.data.node.parent | {
61+
title: .title,
62+
number: .number,
63+
url: .url,
64+
issueType: .issueType
65+
}')
66+
67+
# Print the formatted parent issue details
68+
echo "$formatted_parent_issue" | jq .
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
#!/bin/bash
2+
3+
# Gets the sub-issue summary of an issue
4+
5+
if [ -z "$3" ]; then
6+
echo "Usage: $0 <org> <repo> <issue-number>"
7+
echo "Example: ./get-sub-issues-of-issue.sh joshjohanning-org migrating-ado-to-gh-issues-v2 5"
8+
exit 1
9+
fi
10+
11+
org="$1"
12+
repo="$2"
13+
issue_number="$3"
14+
15+
# Define color codes
16+
RED='\033[0;31m'
17+
YELLOW='\033[0;33m'
18+
NC='\033[0m' # No Color
19+
20+
# Fetch the issue ID given the issue number
21+
issue_id=$(gh api graphql -f owner="$org" -f repository="$repo" -F number="$issue_number" -f query='
22+
query ($owner: String!, $repository: String!, $number: Int!) {
23+
repository(owner: $owner, name: $repository) {
24+
issue(number: $number) {
25+
id
26+
}
27+
}
28+
}' --jq '.data.repository.issue.id')
29+
30+
# Check if the query was successful
31+
if [ $? -ne 0 ]; then
32+
echo -e "${RED}Issue #$issue_number not found in $org/$repo${NC}"
33+
exit 1
34+
fi
35+
36+
# Get the sub-issues for the issue
37+
sub_issue_summary=$(gh api graphql -H GraphQL-Features:sub_issues -f issueId="$issue_id" -f query='
38+
query($issueId: ID!) {
39+
node(id: $issueId) {
40+
... on Issue {
41+
subIssuesSummary {
42+
total
43+
completed
44+
percentCompleted
45+
}
46+
}
47+
}
48+
}')
49+
50+
# Check if the gh api graphql command was successful
51+
if [ $? -ne 0 ]; then
52+
echo -e "${RED}Failed to get sub-issue summary for $org/$repo#$issue_number.${NC}"
53+
exit 1
54+
fi
55+
56+
# Extract and format the sub-issue summary details using jq
57+
formatted_sub_issue_summary=$(echo "$sub_issue_summary" | jq -r '
58+
.data.node.subIssuesSummary | {
59+
total: .total,
60+
completed: .completed,
61+
percentCompleted: .percentCompleted
62+
}')
63+
64+
# Print the formatted sub-issue summary details
65+
echo "$formatted_sub_issue_summary" | jq .
66+
67+
# Check if total is 0 and print a warning
68+
total=$(echo "$formatted_sub_issue_summary" | jq -r '.total')
69+
if [ "$total" -eq 0 ]; then
70+
echo -e "${YELLOW}Warning: The total number of sub-issues for $org/$repo#$issue_number is 0.${NC}"
71+
fi

gh-cli/get-sub-issues-of-issue.sh

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
#!/bin/bash
2+
3+
# Gets a list of sub-issues from an issue
4+
5+
if [ -z "$3" ]; then
6+
echo "Usage: $0 <org> <repo> <issue-number>"
7+
echo "Example: ./get-sub-issues-of-issue.sh joshjohanning-org migrating-ado-to-gh-issues-v2 5"
8+
exit 1
9+
fi
10+
11+
org="$1"
12+
repo="$2"
13+
issue_number="$3"
14+
15+
# Define color codes
16+
RED='\033[0;31m'
17+
NC='\033[0m' # No Color
18+
19+
# Fetch the issue ID given the issue number
20+
issue_id=$(gh api graphql -f owner="$org" -f repository="$repo" -F number="$issue_number" -f query='
21+
query ($owner: String!, $repository: String!, $number: Int!) {
22+
repository(owner: $owner, name: $repository) {
23+
issue(number: $number) {
24+
id
25+
}
26+
}
27+
}' --jq '.data.repository.issue.id')
28+
29+
# Check if the query was successful
30+
if [ $? -ne 0 ]; then
31+
echo -e "${RED}Issue #$issue_number not found in $org/$repo${NC}"
32+
exit 1
33+
fi
34+
35+
# Get the sub-issues for the issue
36+
sub_issues=$(gh api graphql --paginate -H GraphQL-Features:sub_issues -H GraphQL-Features:issue_types -f issueId="$issue_id" -f query='
37+
query($issueId: ID!, $endCursor: String) {
38+
node(id: $issueId) {
39+
... on Issue {
40+
subIssues(first: 100, after: $endCursor) {
41+
totalCount
42+
nodes {
43+
title
44+
number
45+
url
46+
issueType {
47+
name
48+
}
49+
}
50+
pageInfo {
51+
hasNextPage
52+
endCursor
53+
}
54+
}
55+
}
56+
}
57+
}')
58+
59+
# Check if the gh api graphql command was successful
60+
if [ $? -ne 0 ]; then
61+
echo -e "${RED}Failed to get sub-issues for $org/$repo#$issue_number.${NC}"
62+
exit 1
63+
fi
64+
65+
# Combine the results using jq
66+
combined_result=$(echo "$sub_issues" | jq -s '
67+
{
68+
totalCount: .[0].data.node.subIssues.totalCount,
69+
issues: (map(.data.node.subIssues.nodes) | add)
70+
}')
71+
72+
# Print the combined result as a colorized JSON object
73+
echo "$combined_result" | jq .

gh-cli/remove-sub-issue-from-issue.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ else
6969
exit 1
7070
fi
7171

72-
# Set the issue type on the issue
72+
# Remove the sub-issue from the parent issue
7373
gh api graphql -H GraphQL-Features:issue_types -H GraphQL-Features:sub_issues -f parrentIssueId="$parent_issue_id" -f childIssueId="$child_issue_id" -f query='
7474
mutation($parrentIssueId: ID!, $childIssueId: ID!) {
7575
removeSubIssue(input: { issueId: $parrentIssueId, subIssueId: $childIssueId }) {

0 commit comments

Comments
 (0)