-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gh-project
executable file
·129 lines (103 loc) · 2.21 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
#!/bin/bash
set -e
readonly GH_PROJECT_ORGANIZATION
readonly EXECUTABLES=(fzf)
usage() {
cat <<EOF
Works with GitHub Projects from the command line.
Example:
$ gh project list
$ gh project list --org=acme-org
$ gh project add 123
$ gh project add 123 --org=acme-org
$ gh project remove 123
$ gh project remove 123 --org=acme-org
Environment Variables:
GH_PROJECT_ORGANIZATION (optional)
Dependencies: ${EXECUTABLES[@]}
EOF
}
info() {
echo "✓ $*" >&2
}
# Determine if executables are in the PATH.
check_executables() {
for e in "${EXECUTABLES[@]}"; do
if ! type -p "${e}" >/dev/null; then
echo "${e} not found on the system" >&2
return 1
fi
done
}
list() {
local org
while [[ "$#" -gt 0 ]]; do
case "$1" in
--org=*)
org=${1#*=}
;;
*) ;;
esac
shift
done
if [[ -n "${GH_PROJECT_ORGANIZATION}" ]]; then
org="${GH_PROJECT_ORGANIZATION}"
fi
local endpoint="repos/:owner/:repo/projects"
if [[ -n "${org}" ]]; then
endpoint="orgs/${org}/projects"
fi
PAGER='cat' gh api "${endpoint}" --paginate --template '{{- range .}}{{ .name | printf "%s\n" }}{{- end -}}'
}
edit() {
local -r cmd="${1}"
local -r number="${2}"
local -r flags="${*:3}"
local project
project="$(list "${flags[@]}" | fzf)"
local op preposition
case $cmd in
'add')
op='link'
preposition='to'
;;
'remove')
op='unlink'
preposition='from'
;;
esac
if gh issue view "${number}" >/dev/null 2>&1; then
gh issue edit "${number}" "--${cmd}-project" "${project}" >/dev/null
info "Project '${project}' has been ${op}ed ${preposition} issue #$number"
return
fi
if gh pr view "${number}" >/dev/null 2>&1; then
gh pr edit "${number}" "--${cmd}-project" "${project}" >/dev/null
info "Project '${project}' has been ${op}ed ${preposition} pull request #$number"
return
fi
}
main() {
if [[ "$#" -lt 1 ]]; then
usage
return 1
fi
if ! check_executables; then
usage
return 1
fi
local -r cmd="${1}"
case $cmd in
'list')
list "${@:2}"
;;
'add' | 'remove')
edit "${cmd}" "${@:2}"
;;
*)
usage
return 1
;;
esac
}
main "$@"