Skip to content

Commit 4956a65

Browse files
Merge pull request #89 from joshjohanning/add-issue-type-and-sub-issue-scripts
feat: adding issue types / sub-issues scripts
2 parents 3963b88 + 1a45ef3 commit 4956a65

File tree

5 files changed

+340
-0
lines changed

5 files changed

+340
-0
lines changed

gh-cli/README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,10 @@ Use the [get-enterprise-id.sh](./get-enterprise-id.sh) or [get-organization-id.s
112112

113113
See the [docs](https://docs.github.com/en/graphql/reference/mutations#createipallowlistentry) for further information.
114114

115+
### add-sub-issue-to-issue.sh
116+
117+
Adds a sub-issue (child) to an issue (parent). See: [Community Discussions Post](https://github.com/orgs/community/discussions/139932)
118+
115119
### add-team-to-repositories-from-list.sh
116120

117121
This script adds a specified team to a list of repositories with specified permissions.
@@ -1164,6 +1168,14 @@ Removes an enterprise user. See notes:
11641168
1. List org members and get the id from there: `./get-organization-members.sh`
11651169
2. Get user id: `./get-user-id.sh`
11661170

1171+
### remove-issue-issue-type.sh
1172+
1173+
Remove the issue type from an issue (set it to `null`). See: [Community Discussions Post](https://github.com/orgs/community/discussions/139933)
1174+
1175+
### remove-sub-issue-from-issue.sh
1176+
1177+
Removes a sub-issue (child) from an issue (parent). See: [Community Discussions Post](https://github.com/orgs/community/discussions/139932)
1178+
11671179
### remove-users-from-organization.sh
11681180

11691181
Removes a list of users from the organization.
@@ -1265,6 +1277,10 @@ Updates a branch protection rule for a given branch.
12651277

12661278
Adds your account to an organization in an enterprise as an owner, member, or leave the organization. This requires the user running the script to be an Enterprise Owner.
12671279

1280+
### update-issue-issue-type.sh
1281+
1282+
Updates / sets the issue type for an issue. See: [Community Discussions Post](https://github.com/orgs/community/discussions/139933)
1283+
12681284
### verify-team-membership.sh
12691285

12701286
Simple script to verify that a user is a member of a team

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

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
#!/bin/bash
2+
3+
# Adds a sub-issue (child) to an issue (parent)
4+
5+
if [ -z "$4" ]; then
6+
echo "Usage: $0 <org> <repo> <parent-issue-number> <child-issue-number>"
7+
echo "Example: ./add-sub-issue-to-issue.sh joshjohanning-org migrating-ado-to-gh-issues-v2 5 6"
8+
exit 1
9+
fi
10+
11+
org="$1"
12+
repo="$2"
13+
parent_issue_number="$3"
14+
child_issue_number="$4"
15+
16+
# Define color codes
17+
RED='\033[0;31m'
18+
NC='\033[0m' # No Color
19+
20+
# Function to fetch the issue ID given the issue number
21+
fetch_issue_id() {
22+
local org=$1
23+
local repo=$2
24+
local issue_number=$3
25+
26+
issue_id=$(gh api graphql -F owner="$org" -f repository="$repo" -F number="$issue_number" -f query='
27+
query ($owner: String!, $repository: String!, $number: Int!) {
28+
repository(owner: $owner, name: $repository) {
29+
issue(number: $number) {
30+
id
31+
}
32+
}
33+
}' --jq '.data.repository.issue.id')
34+
35+
# Check if the query was successful
36+
if [ $? -ne 0 ] || [ -z "$issue_id" ]; then
37+
echo -e "${RED}Issue #$issue_number not found in repository '$repo' of organization '$org'.${NC}"
38+
exit 1
39+
fi
40+
41+
echo "$issue_id"
42+
}
43+
44+
# Fetch the parent issue ID given the issue number
45+
parent_issue_id=$(fetch_issue_id "$org" "$repo" "$parent_issue_number")
46+
47+
# Fetch the child issue ID given the issue number
48+
child_issue_id=$(fetch_issue_id "$org" "$repo" "$child_issue_number")
49+
50+
# Set the issue type on the issue
51+
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='
52+
mutation($parrentIssueId: ID!, $childIssueId: ID!) {
53+
addSubIssue(input: { issueId: $parrentIssueId, subIssueId: $childIssueId }) {
54+
issue {
55+
title
56+
number
57+
url
58+
issueType {
59+
name
60+
}
61+
}
62+
subIssue {
63+
title
64+
number
65+
url
66+
issueType {
67+
name
68+
}
69+
}
70+
}
71+
}'
72+
73+
# Check if the mutation was successful
74+
if [ $? -eq 0 ]; then
75+
echo "Successfully added issue $org/$repo#$child_issue_number as a sub-issue to $org/$repo#$parent_issue_number."
76+
else
77+
echo -e "${RED}Failed to add issue $org/$repo#$child_issue_number as a sub-issue to $org/$repo#$parent_issue_number.${NC}"
78+
exit 1
79+
fi

gh-cli/remove-issue-issue-type.sh

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
#!/bin/bash
2+
3+
# Remove the issue type from an issue (set it to `null`)
4+
5+
if [ -z "$3" ]; then
6+
echo "Usage: $0 <org> <repo> <issue-number>"
7+
echo "Example: ./remove-issue-issue-type.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=$(gh api graphql -H GraphQL-Features:issue_types -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+
issueType {
26+
name
27+
}
28+
}
29+
}
30+
}')
31+
32+
# Check if the query was successful
33+
if [ $? -ne 0 ]; then
34+
echo -e "${RED}Issue #$issue_number not found in $org/$repo${NC}"
35+
exit 1
36+
fi
37+
38+
issue_id=$(echo "$issue" | jq -r '.data.repository.issue.id')
39+
issue_type=$(echo "$issue" | jq -r '.data.repository.issue.issueType.name')
40+
41+
# Check if the issue type is already null
42+
if [ "$issue_type" == "null" ]; then
43+
echo -e "${RED}The issue type is already null for issue #$issue_number in $org/$repo${NC}"
44+
exit 1
45+
fi
46+
47+
# Remove the issue type on the issue
48+
gh api graphql -H GraphQL-Features:issue_types -f issueId="$issue_id" -f query='
49+
mutation($issueId: ID!) {
50+
updateIssueIssueType(input: {issueId: $issueId, issueTypeId: null}) {
51+
issue {
52+
title
53+
number
54+
url
55+
issueType {
56+
name
57+
}
58+
}
59+
}
60+
}'
61+
62+
# Check if the mutation was successful
63+
if [ $? -eq 0 ]; then
64+
echo "Issue type removed for issue #$issue_number."
65+
else
66+
echo -e "${RED}Failed to remove issue type for issue $org/$repo#$issue_number.${NC}"
67+
exit 1
68+
fi

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

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
#!/bin/bash
2+
3+
# Removes a sub-issue (child) from an issue (parent)
4+
5+
if [ -z "$4" ]; then
6+
echo "Usage: $0 <org> <repo> <parent-issue-number> <child-issue-number>"
7+
echo "Example: ./remove-sub-issue-to-issue.sh joshjohanning-org migrating-ado-to-gh-issues-v2 5 bug"
8+
exit 1
9+
fi
10+
11+
org="$1"
12+
repo="$2"
13+
parent_issue_number="$3"
14+
child_issue_number="$4"
15+
16+
# Define color codes
17+
RED='\033[0;31m'
18+
NC='\033[0m' # No Color
19+
20+
# Function to fetch the issue ID given the issue number
21+
fetch_issue_id() {
22+
local org=$1
23+
local repo=$2
24+
local issue_number=$3
25+
26+
issue_id=$(gh api graphql -F owner="$org" -F repository="$repo" -F number="$issue_number" -f query='
27+
query ($owner: String!, $repository: String!, $number: Int!) {
28+
repository(owner: $owner, name: $repository) {
29+
issue(number: $number) {
30+
id
31+
}
32+
}
33+
}' --jq '.data.repository.issue.id')
34+
35+
# Check if the query was successful
36+
if [ $? -ne 0 ] || [ -z "$issue_id" ]; then
37+
echo -e "${RED}Issue #$issue_number not found in repository '$repo' of organization '$org'.${NC}"
38+
exit 1
39+
fi
40+
41+
echo "$issue_id"
42+
}
43+
44+
# Fetch the parent issue ID given the issue number
45+
parent_issue_id=$(fetch_issue_id "$org" "$repo" "$parent_issue_number")
46+
47+
# Fetch the child issue ID given the issue number
48+
child_issue_id=$(fetch_issue_id "$org" "$repo" "$child_issue_number")
49+
50+
child_issues=$(gh api graphql -H GraphQL-Features:sub_issues -F parrentIssueId="$parent_issue_id" -f query='
51+
query ($parrentIssueId: ID!) {
52+
node(id: $parrentIssueId) {
53+
... on Issue {
54+
subIssues(first: 10) {
55+
nodes {
56+
number
57+
id
58+
}
59+
}
60+
}
61+
}
62+
}')
63+
64+
# Check if the child issue number is in the list of child issues
65+
if echo "$child_issues" | jq -e --argjson child_issue_number "$child_issue_number" '.data.node.subIssues.nodes[] | select(.number == $child_issue_number)' > /dev/null; then
66+
echo "Child issue #$child_issue_number is a sub-issue of parent issue #$parent_issue_number."
67+
else
68+
echo -e "${RED}Child issue #$child_issue_number is not a sub-issue of parent issue #$parent_issue_number.${NC}"
69+
exit 1
70+
fi
71+
72+
# Set the issue type on the issue
73+
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='
74+
mutation($parrentIssueId: ID!, $childIssueId: ID!) {
75+
removeSubIssue(input: { issueId: $parrentIssueId, subIssueId: $childIssueId }) {
76+
issue {
77+
title
78+
number
79+
url
80+
issueType {
81+
name
82+
}
83+
}
84+
subIssue {
85+
title
86+
number
87+
url
88+
issueType {
89+
name
90+
}
91+
}
92+
}
93+
}'
94+
95+
# Check if the mutation was successful
96+
if [ $? -eq 0 ]; then
97+
echo "Successfully removed issue $org/$repo#$child_issue_number as a sub-issue to $org/$repo#$parent_issue_number."
98+
else
99+
echo -e "${RED}Failed to remove issue $org/$repo#$child_issue_number as a sub-issue to $org/$repo#$parent_issue_number.${NC}"
100+
exit 1
101+
fi

gh-cli/update-issue-issue-type.sh

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
#!/bin/bash
2+
3+
# Updates / sets the issue type for an issue
4+
5+
if [ -z "$4" ]; then
6+
echo "Usage: $0 <org> <repo> <issue-number> <type>"
7+
echo "Example: ./update-issue-issue-type.sh joshjohanning-org migrating-ado-to-gh-issues-v2 5 'user story'"
8+
exit 1
9+
fi
10+
11+
org="$1"
12+
repo="$2"
13+
issue_number="$3"
14+
type=$(echo "$4" | tr '[:upper:]' '[:lower:]')
15+
16+
# Define color codes
17+
RED='\033[0;31m'
18+
NC='\033[0m' # No Color
19+
20+
# Fetch issue types and filter to get the ID where name equals $type_upper
21+
issue_type_id=$(gh api graphql -H GraphQL-Features:issue_types -f owner="$org" -f query='
22+
query($owner: String!) {
23+
organization(login: $owner) {
24+
issueTypes(first: 100) {
25+
nodes {
26+
id
27+
name
28+
}
29+
}
30+
}
31+
}' | jq -r --arg type "$type" '.data.organization.issueTypes.nodes[] | select(.name | ascii_downcase == $type) | .id')
32+
33+
# Check if issue type ID was found
34+
if [ -z "$issue_type_id" ]; then
35+
echo "Issue type '$type' not found in organization '$org'."
36+
exit 1
37+
fi
38+
39+
# Fetch the issue ID given the issue number
40+
issue_id=$(gh api graphql -f owner="$org" -f repository="$repo" -F number="$issue_number" -f query='
41+
query ($owner: String!, $repository: String!, $number: Int!) {
42+
repository(owner: $owner, name: $repository) {
43+
issue(number: $number) {
44+
id
45+
}
46+
}
47+
}' --jq '.data.repository.issue.id')
48+
49+
# Check if the query was successful
50+
if [ $? -ne 0 ]; then
51+
echo -e "${RED}Issue #$issue_number not found in $org/$repo${NC}"
52+
exit 1
53+
fi
54+
55+
# Set the issue type on the issue
56+
gh api graphql -H GraphQL-Features:issue_types -f issueId="$issue_id" -f issueTypeId="$issue_type_id" -f query='
57+
mutation($issueId: ID!, $issueTypeId: ID!) {
58+
updateIssueIssueType(input: {issueId: $issueId, issueTypeId: $issueTypeId}) {
59+
issue {
60+
title
61+
number
62+
url
63+
issueType {
64+
name
65+
}
66+
}
67+
}
68+
}'
69+
70+
# Check if the mutation was successful
71+
if [ $? -eq 0 ]; then
72+
echo "Issue type set to '$type' for issue #$issue_number."
73+
else
74+
echo -e "${RED}Failed to set issue type for issue $org/$repo#$issue_number.${NC}"
75+
exit 1
76+
fi

0 commit comments

Comments
 (0)