-
Notifications
You must be signed in to change notification settings - Fork 0
/
isu.sh
executable file
·387 lines (371 loc) · 14.8 KB
/
isu.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
#!/bin/bash
# This script creates a new issue on GitHub
isu() {
# check bash version is 4 or higher
if [ "${BASH_VERSINFO[0]}" -lt 4 ]; then
echo "This script requires bash version 4 or higher"
return 1
fi
check_yes_no_internal() {
while true; do
sleep 0.1
# check which shell is being used
if [ -n "$BASH_VERSION" ]; then
read -p "$1" yn
elif [ -n "$ZSH_VERSION" ]; then
vared -p "$1" -c yn
fi
# read -p "$1" yn
if [ "$yn" = "" ]; then
yn='Y'
fi
case "$yn" in
[Yy]) return 0 ;;
[Nn]) return 1 ;;
*) echo -n "Not a valid option. Please enter y or n: " ;;
esac
done
}
if [[ -n $COMP_LINE ]]; then
# if COMP_LINE is only isu and space then show title
if [[ "$COMP_LINE" == "isu " ]]; then
echo '--title'
exit
fi
if [[ ! "${COMP_LINE}" == *"--title"* ]]; then
echo '--title'
fi
if [[ ! "${COMP_LINE}" == *"--body"* ]]; then
echo '--body'
fi
if [[ ! "${COMP_LINE}" == *"--column"* ]]; then
echo '--column'
fi
if [[ ! "${COMP_LINE}" == *"--labels"* ]]; then
echo '--labels'
fi
if [[ ! "${COMP_LINE}" == *"--no-branch"* ]]; then
echo '--no-branch'
fi
if [[ ! "${COMP_LINE}" == *"--no-storypoints"* ]]; then
echo '--no-storypoints'
fi
exit
fi
show_help() {
echo "isu"
echo "Creates a new issue on github"
echo "The new issue will be created in the current directory's git repo and added to the most recent project connected to the repo"
echo ""
echo "Flags"
echo ""
echo "-h, --help: Show help"
echo ""
echo "-t, --title: Title of the issue"
echo ""
echo "-b, --body: Body of the issue (optional)"
echo ""
echo "-l, --labels: Labels of the issue (optional)"
echo ""
echo "-c, --column: Column of the issue (optional)"
echo ""
echo "-n, --no-branch: Don't create a new branch for the issue"
echo ""
echo "-p, --no-storypoints: Don't prompt for story points"
return 0
}
if [ "$1" = "-h" ] || [ "$1" = "--help" ]; then
show_help
return 0
fi
rest_of_input="$@"
mk_branch=true
msg_title=""
msg_body=""
msg_label=""
msg_column=""
no_story_points=0
# if args don't start with - then assume that everything is the title up to the first flag
if [[ "$1" != -* ]]; then
msg_title="$@"
msg_title=$(echo "$msg_title" | awk 'NR==1')
msg_title=$(echo "$msg_title" | sed -E 's/ (-t |-b |-l |-c |-n |-p |-h |--title |--body |--labels |--column |--no-branch |--no-storypoints |--help).*//')
fi
for arg in "$@"; do
case $arg in
-t | --title)
if [[ "$@" == *"--title "* ]]; then
msg_title=${rest_of_input#*--title }
msg_title=$(echo "$msg_title" | awk 'NR==1')
msg_title=$(echo "$msg_title" | sed -E 's/ (-b |-l |-c |-n |-p |-h |--body |--labels |--column |--no-branch |--no-storypoints |--help).*//')
elif [[ "$@" == *"-t "* ]]; then
msg_title=${rest_of_input#*-t }
msg_title=$(echo "$msg_title" | awk 'NR==1')
msg_title=$(echo "$msg_title" | sed -E 's/ (-b |-l |-c |-n |-p |-h |--body |--labels |--column |--no-branch |--no-storypoints |--help).*//')
fi
;;
-b | --body)
if [[ "$@" == *" --body "* ]]; then
msg_body=${rest_of_input#* --body }
msg_body=$(echo "$msg_body" | sed -E 's/ (-t |-l |-c |-n |-p |-h |--title |--labels |--column |--no-branch |--no-storypoints |--help).*//')
elif [[ "$@" == *" -b "* ]]; then
msg_body=${rest_of_input#* -b }
msg_body=$(echo "$msg_body" | sed -E 's/ (-t |-l |-c |-n |-p |-h |--title |--labels |--column |--no-branch |--no-storypoints |--help).*//')
fi
;;
-l | --labels)
if [[ "$@" == *" --labels "* ]]; then
msg_label=${rest_of_input#* --labels }
msg_label=$(echo "$msg_label" | awk 'NR==1')
msg_label=$(echo "$msg_label" | sed -E 's/ (-t |-b |-c |-n |-p |-h |--title |--body |--column |--no-branch |--no-storypoints |--help).*//')
elif [[ "$@" == *" -l "* ]]; then
msg_label=${rest_of_input#* -l }
msg_label=$(echo "$msg_label" | awk 'NR==1')
#regex for any character that is not whitespace
msg_label=$(echo "$msg_label" | sed -E 's/ (-t |-b |-c |-n |-p |-h |--title |--body |--column |--no-branch |--no-storypoints |--help).*//')
fi
;;
-c | --column)
if [[ "$@" == *" --column "* ]]; then
msg_column=${rest_of_input#* --column }
msg_column=$(echo "$msg_column" | awk 'NR==1')
msg_column=$(echo "$msg_column" | sed -E 's/ (-t |-b |-l |-n |-p |-h |--title |--body |--labels |--no-branch |--no-storypoints |--help).*//')
elif [[ "$@" == *" -c "* ]]; then
msg_column=${rest_of_input#* -c }
msg_column=$(echo "$msg_column" | awk 'NR==1')
msg_column=$(echo "$msg_column" | sed -E 's/ (-t |-b |-l |-n |-p |-h |--title |--body |--labels |--no-branch |--no-storypoints |--help).*//')
fi
;;
-n | --no-branch)
if [[ "$@" == *" --no-branch"* ]]; then
mk_branch=false
elif [[ "$@" == *" -n"* ]]; then
mk_branch=false
fi
;;
-p | --no-storypoints)
if [[ "$@" == *"--no-storypoints"* ]]; then
no_story_points=1
elif [[ "$@" == *"-p"* ]]; then
no_story_points=1
fi
;;
-h | --help)
show_help
return 0
;;
-[^\s]*)
show_help
return 0
;;
esac
done
# get the repo owner
gh_name=$(git remote get-url origin | sed -e 's/.*github.com\///' -e 's/\/.*//')
gh_name=${gh_name#*:}
# get the repo name from path
repo_name=$(basename $(git rev-parse --show-toplevel))
#needed in order to not have to run interactive mode
if [ -z "$msg_body" ]; then
msg_body=" "
fi
if [ -z "$msg_title" ]; then
echo "No title provided, exiting."
show_help
return 0
else
if [ $no_story_points == 0 ]; then
# check if the title starts with story points
check_sp=$(echo $msg_title | sed 's/^\[[0-9]\+\] //')
# if they are identical then the title did not start with story points
if [ "$msg_title" = "$check_sp" ]; then
#prompt for story points, only allow numbers
read -p "Story points: " story_points
while [[ ! $story_points =~ ^[0-9]+$ ]]; do
#if empty then set to 1
if [ -z "$story_points" ]; then
story_points=1
break
fi
echo "Story points must be a number"
read -p "Story points: " story_points
done
msg_title="[$story_points] $msg_title"
fi
fi
command_string="gh issue create -R "$gh_name/$repo_name" -t "\"$msg_title\"""
if [ -n "$msg_body" ]; then
# echo "body is not empty"
command_string=$command_string" -b "\"$msg_body\"""
fi
# if message label is empty
if [ -n "$msg_label" ]; then
# echo "label is not empty"
# if label contains a space
if [[ "$msg_label" == *" "* ]]; then
while [[ "$msg_label" == *" "* ]]; do
label=${msg_label%% *}
msg_label=${msg_label#* }
command_string=$command_string" -l "\"$label\"""
done
fi
command_string=$command_string" -l "\"$msg_label\"""
fi
fi
gh_response=$(eval "$command_string")
repo_id="$(gh api graphql -f ownerrepo="$gh_name" -f reponame="$repo_name" -f query='
query($ownerrepo: String!, $reponame: String!) {
repository(owner: $ownerrepo, name: $reponame) {
id
projectsV2(first: 1, orderBy: {field: CREATED_AT, direction: DESC}) {
nodes {
id
}
}
}
}')"
repo_id=${repo_id#*\"id\":\"}
project_id=${repo_id#*\"id\":\"}
repo_id=${repo_id%%\"*}
project_id=${project_id%%\"*}
issues="$(gh api graphql -f ownerrepo="$gh_name" -f reponame="$repo_name" -f query='
query($ownerrepo: String!, $reponame: String!) {
repository(owner: $ownerrepo, name: $reponame) {
issues(first: 3, orderBy: {field: CREATED_AT, direction: DESC}) {
nodes {
id
title
number
}
}
}
}')"
declare -A issue_ids
while [[ $issues == *"id"* ]]; do
issue_id=${issues#*\"id\":\"}
issue_id=${issue_id%%\"*}
issues=${issues#*\"id\":\"$issue_id\"}
issue_title=${issues#*\"title\":\"}
issue_title=${issue_title%%\"*}
issue_num=${issues#*\"number\":}
issue_num=${issue_num%%'}'*}
issues=${issues#*\"title\":\"$issue_title\"}
issue_ids[$issue_title]="$issue_id"";""$issue_num"
done
issue_id=${issue_ids[$msg_title]}
issue_num=${issue_id#*;}
issue_id=${issue_id%%;*}
if [ -z "$issue_id" ]; then
echo "Newly created issue not found, guess it's browser-time ... exiting."
return 0
fi
# check if status is not done or Done or backlog or Backlog
if ! [ -z "$msg_column" ] && [ "$msg_column" != "Done" ] && [ "$msg_column" != "done" ] && [[ "$msg_column" != *"acklog"* ]]; then
branch_name=$issue_num"-"$msg_title
#substitute all spaces with dashes
branch_name=${branch_name// /-}
# remove all non-alphanumeric characters
branch_name=${branch_name//[^a-zA-Z0-9-]/}
# convert to lowercase
branch_name=${branch_name,,}
# check if branch exists
if [ $mk_branch == true ]; then
if [ -z "$(git branch --list | grep " $branch_name"$)" ]; then
if check_yes_no_internal "It's dangerous to go alone! Take a branch with you? [Y/n]: "; then
#make sure we are on main or dev
#check if dev exists
if [ -z "$(git branch --list | grep " dev$")" ]; then
if [ "$(git branch --show-current)" != "main" ]; then
if check_yes_no_internal "You are not on main, switch to main and pull before creating branch? [Y/n]: "; then
git checkout main
git pull
elif ! check_yes_no_internal "Continue without switching to main? [Y/n]: "; then
echo "Aborting"
return 0
fi
fi
else
if [ "$(git branch --show-current)" != "dev" ]; then
if check_yes_no_internal "You are not on dev, switch to dev and pull before creating branch? [Y/n]: "; then
git checkout dev
git pull
elif ! check_yes_no_internal "Continue without switching to dev? [Y/n]: "; then
echo "Aborting"
return 0
fi
fi
fi
git checkout -b $branch_name
fi
elif [ "$(git branch --show-current)" != "$branch_name" ]; then
if check_yes_no_internal "Switch to $branch_name? [Y/n]: "; then
git checkout $branch_name
fi
fi
fi
fi
adding_issue_response="$(gh api graphql -f project="$project_id" -f item="$issue_id" -f query='
mutation AddToProject($project: ID!, $item: ID!) {
addProjectV2ItemById(input: { projectId: $project, contentId: $item }) {
item {
id
}
}
}')"
project_issue_id=${adding_issue_response#*\"id\":\"}
project_issue_id=${project_issue_id%%\"*}
project_field_id="$(gh api graphql -f project="$project_id" -f query='
query($project: ID!) {
node(id: $project) {
... on ProjectV2 {
fields(first: 100) {
nodes {
... on ProjectV2SingleSelectField {
id
name
options {
id
name
}
}
}
}
}
}
}')"
status_field_id=${project_field_id#*\"id\":\"}
status_field_id=${status_field_id%%\"*}
status_options=${project_field_id#*\"options\":\[}
status_options=${status_options%%\]*}
# if message column is empty
if [ -z "$msg_column" ]; then
# get the id of the first option
todo_option_id=${status_options#*\"id\":\"}
todo_option_id=${todo_option_id%%\"*}
else
# todo_option_id=$(echo "$project_field_id" | grep -oP '(?<="id":")[^"]*(?=","name":"'"${msg_column}"'")')
todo_option_id=$(echo "$project_field_id" | grep -oE '"id":"[^"]*","name":"'"${msg_column}"'"' | sed -E 's/"id":"([^"]*)","name":"'"${msg_column}"'"/\1/')
fi
if [ -z "$todo_option_id" ]; then
echo "Status not found, exiting."
return 0
fi
add_to_col="$(gh api graphql -f project="$project_id" -f issueid="$project_issue_id" -f field="$status_field_id" -f optionvalue="$todo_option_id" -f query='
mutation ($project: ID!, $issueid: ID!, $field: ID!, $optionvalue: String!) {
updateProjectV2ItemFieldValue(
input: {
projectId: $project
itemId: $issueid
fieldId: $field
value: {
singleSelectOptionId: $optionvalue
}
}
) {
projectV2Item {
id
}
}
}')"
echo -e "Issue \e[32m$issue_num\e[0m added to column \e[32m$msg_column\e[0m"
}