-
Notifications
You must be signed in to change notification settings - Fork 3
/
gh-project
executable file
·359 lines (332 loc) · 10 KB
/
gh-project
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
#!/bin/bash
set -e
cmd=$1
shift
list_projects() {
local path=$1
TEMPLATE='{{tablerow "ID" "Name" "URL"}}{{range .}}{{tablerow .id .name .html_url}}{{end}}'
exec gh api --preview inertia "${path}/projects" --template="$TEMPLATE"
}
list_projects_help() {
echo "List projects from an organisation, a user or a repository"
echo ""
echo "Without flags, projects from the current repository are listed"
echo ""
echo "USAGE"
echo " gh project list [flags]"
echo ""
echo "FLAGS"
printf " --org organisation\tList projects from organisation\n"
printf " -u, --user user\tList projects from user\n"
printf " -o, --owner owner\tList projects from owner (requires repository)\n"
printf " -r, --repository repository\tList projects from repository (requires owner)\n"
echo ""
echo "INHERITED FLAGS"
echo " --help Show help for command"
echo ""
echo "EXAMPLES"
echo " $ gh project list"
echo " $ gh project list --org my-corporation"
echo " $ gh project list --owner rethab --repository gh-project"
}
list_columns() {
local project_id="$1"
TEMPLATE='{{tablerow "ID" "Name" }}{{range .}}{{tablerow .id .name }}{{end}}'
exec gh api --preview inertia "projects/${project_id}/columns" --template="$TEMPLATE"
}
list_columns_help() {
echo "List columns in a project"
echo ""
echo "USAGE"
echo " gh project list-columns [flags]"
echo ""
echo "FLAGS"
printf " -p, --project id\tId of the project. Use the \"list\" command to show projects\n"
echo ""
echo "INHERITED FLAGS"
echo " --help Show help for command"
echo ""
echo "EXAMPLES"
echo " $ gh project list-columns --project 12789786"
}
list_cards() {
local column_id="$1"
shift
TEMPLATE='{{tablerow "ID" "Note" }}{{range .}}{{tablerow .id .note }}{{end}}'
exec gh api "/projects/columns/${column_id}/cards" --template="$TEMPLATE"
}
list_cards_help() {
echo "List cards in a column"
echo ""
echo "USAGE"
echo " gh project list-cards [flags]"
echo ""
echo "FLAGS"
printf " -c, --column id\tColumn from which to list cards. Use \"list-column\" to show columns\n"
echo ""
echo "INHERITED FLAGS"
echo " --help Show help for command"
echo ""
echo "EXAMPLES"
echo " $ gh project list-cards --column 1489862"
}
create_card() {
local owner="$1"
shift
local column_id="$1"
shift
local issue_repo="$1"
shift
local label="$1"
shift
local title=$*
if [[ -n $label ]]; then
label="\"${label//,/\",\"}\""
fi
issue_payload="{\"title\": \"$title\", \"labels\": [$label]}"
issue_id=$(echo "$issue_payload" | gh api "/repos/${owner}/${issue_repo}/issues" --input - --jq '.id')
content_url=$(gh api --preview inertia "/projects/columns/${column_id}/cards" -F content_id="$issue_id" -f content_type=Issue --jq '.content_url')
echo "$content_url" | sed s/api.// | sed s-repos/--
}
create_card_help() {
echo "Create a new card in a project"
echo ""
echo "The content of a card is based on an issue, which is going to be created as part of this command."
echo ""
echo "USAGE"
echo " gh project create-card [flags]"
echo ""
echo "FLAGS"
printf " -o, --owner owner \t\tOwner/organization to create the card in. Defaults to the owner of the current repository.\n"
printf " -c, --column id \t\tColumn in which to create card. Use \"list-column\" to show columns\n"
printf " -r, --issue-repository name\tName of the repository in which to create the issue\n"
printf " -l, --label string\t\tAdd labels by name. Separate multiple labels with a comma\n"
printf " -t, --title string\t\tTitle of the card\n"
echo ""
echo "INHERITED FLAGS"
echo " --help Show help for command"
echo ""
echo "EXAMPLES"
echo " $ gh project create-card --column 1489862 --issue-repository backend-service --label \"help wanted\" --title \"implement new feature\""
}
move_card() {
local card_id="$1"
local position="$2"
local column_id="$3"
if [ -z "$column_id" ]; then
exec gh api "/projects/columns/cards/$card_id/moves" -f "position=$position"
else
exec gh api "/projects/columns/cards/$card_id/moves" -f "position=$position" -F "column_id=$column_id"
fi
}
move_card_help() {
echo "Move a card within a column or to a different column"
echo ""
echo "USAGE"
echo " gh project move-card [flags]"
echo ""
echo "FLAGS"
printf " -c, --card card_id\t\tCard to move. Use \"list-cards\" to show cards\n"
printf " -p, --position position\tPosition to move the card to. Options: top, bottom, after:<card_id>. Defaults to top\n"
printf " --column column_id\t\tID of the column to move to. If omitted, card is moved within column\n"
echo ""
echo "INHERITED FLAGS"
echo " --help Show help for command"
echo ""
echo "EXAMPLES"
echo " $ gh project move-card --card 69500449 --position top --column 16122294"
}
show_help() {
echo "Work with GitHub Projects"
echo ""
echo "USAGE"
echo " gh project <command> [flags]"
echo ""
echo "CORE COMMANDS"
printf " list\t\tList projects\n"
printf " list-columns\tList columns in project\n"
printf " list-cards\tList cards in a column\n"
printf " create-card\tCreate a new issue and add it as a card to a column\n"
printf " move-card\tMove card to a different column or within a column\n"
echo ""
echo "SHOW COMMAND HELP AND USAGE"
echo " $ gh project <command> --help"
echo ""
echo "INHERITED FLAGS"
echo " --help Show help for command"
echo ""
echo "EXAMPLES"
echo " $ gh project list"
}
require_arg() {
local flag="$1"
echo "$flag requires an argument"
exit 1
}
case "$cmd" in
list)
while [ "${1:-}" != "" ]; do
case "$1" in
-u|--user)
user="$2"
shift 2 || require_arg "user"
;;
--org)
org="$2"
shift 2 || require_arg "org"
;;
-o|--owner)
owner="$2"
shift 2 || require_arg "owner"
;;
-r|--repository)
repository="$2"
shift 2 || require_arg "repository"
;;
-h|--help)
list_projects_help
exit 0
;;
*)
echo "Unexpected argument: $1"
list_projects_help
exit 1
;;
esac
done
if [[ -n "$user" ]]; then
path="/users/$user"
[[ -n "$org$owner$repository" ]] && { echo "invalid flags in combination with user"; exit 1; }
elif [[ -n "$org" ]]; then
path="/orgs/$org"
[[ -n "$owner$repository" ]] && { echo "invalid flags in combination with org"; exit 1; }
elif [[ -n "$owner" && -n "$repository" ]]; then
path="/repos/$owner/$repository"
else
path="/repos/{owner}/{repo}"
fi
list_projects "$path"
;;
list-columns)
while [ "${1:-}" != "" ]; do
case "$1" in
-p|--project)
project_id="$2"
shift 2 || require_arg "project"
;;
-h|--help)
list_columns_help
exit 0
;;
*)
echo "Unexpected argument: $1"
list_columns_help
exit 1
;;
esac
done
[[ -z "$project_id" ]] && { echo "Missing project"; exit 1; }
list_columns "$project_id"
;;
list-cards)
owner='{owner}'
while [ "${1:-}" != "" ]; do
case "$1" in
-c|--column)
column_id="$2"
shift 2 || require_arg "column"
;;
-h|--help)
list_cards_help
exit 0
;;
*)
echo "Unexpected argument: $1"
list_cards_help
exit 1
;;
esac
done
[[ -z "$column_id" ]] && { echo "Missing column"; exit 1; }
list_cards "$column_id"
;;
create-card)
owner='{owner}'
labels=''
while [ "${1:-}" != "" ]; do
case "$1" in
-o|--owner)
owner="$2"
shift 2 || require_arg "owner"
;;
-c|--column)
column_id="$2"
shift 2 || require_arg "column"
;;
-r|--issue-repository)
issue_repository="$2"
shift 2 || require_arg "repository"
;;
-l|--label)
labels="$2"
shift 2 || require_arg "label"
;;
-t|--title)
title="$2"
shift 2 || require_arg "title"
;;
-h|--help)
create_card_help
exit 0
;;
*)
echo "Unexpected argument: $1"
create_card_help
exit 1
;;
esac
done
[[ -z "$column_id" ]] && { echo "Missing column"; exit 1; }
[[ -z "$issue_repository" ]] && { echo "Missing issue-repository"; exit 1; }
[[ -z "$title" ]] && { echo "Missing title"; exit 1; }
create_card "$owner" "$column_id" "$issue_repository" "$labels" "$title"
;;
move-card)
while [ "${1:-}" != "" ]; do
case "$1" in
-c|--card)
card_id="$2"
shift 2 || require_arg "card"
;;
-p|--position)
position="$2"
shift 2 || require_arg "position"
;;
--column)
column_id="$2"
shift 2 || require_arg "column"
;;
-h|--help)
move_card_help
exit 0
;;
*)
echo "Unexpected argument: $1"
show_help
exit 1
;;
esac
done
[[ -z "$card_id" ]] && { echo "Missing card"; exit 1; }
[[ -z "$position" ]] && { position="top"; }
move_card "$card_id" "$position" "$column_id"
;;
--help)
show_help
;;
*)
echo "Invalid command '$cmd'"
show_help
exit 1
;;
esac