forked from mislav/gh-repo-collab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gh-repo-collab
executable file
·151 lines (141 loc) · 3.66 KB
/
gh-repo-collab
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
#!/usr/bin/env bash
set -e
usage() {
echo "Usage: gh repo-collab list [<repo>]"
echo " gh repo-collab add <repo> <handle> [--permission {pull|triage|push|maintain|admin}]"
echo " gh repo-collab add <repo> < users-file.txt"
echo " gh repo-collab remove <repo> <handle>"
echo
echo "Handle may be a GitHub user login handle or \`<org>/<slug>' for an organization team."
}
list() {
local repo='{owner}/{repo}'
[ $# -eq 0 ] || repo="${1#https://github.com/}"
unset PAGER
# list individual collaborators
gh api "repos/$repo/collaborators" --paginate --template '
{{- range . -}}
{{- $perm := "" -}}
{{- range $key, $val := .permissions -}}
{{- if $val -}}
{{- if $perm -}}
{{- $perm = printf "%s,%s" $perm $key -}}
{{- else -}}
{{- $perm = $key -}}
{{- end -}}
{{- end -}}
{{- end -}}
{{- tablerow (.login | autocolor "green+h") (.role_name | autocolor "yellow+h") ($perm | autocolor "blue+h" | printf "[%s]") -}}
{{- end -}}'
# list team collaborators for organization repositories
gh api "repos/$repo/teams" --paginate --template '
{{- range . -}}
{{- $perm := "" -}}
{{- range $key, $val := .permissions -}}
{{- if $val -}}
{{- if $perm -}}
{{- $perm = printf "%s,%s" $perm $key -}}
{{- else -}}
{{- $perm = $key -}}
{{- end -}}
{{- end -}}
{{- end -}}
{{- tablerow (printf "team:%s" .name | autocolor "green+h") (.permission | autocolor "yellow+h") ($perm | autocolor "blue+h" | printf "[%s]") -}}
{{- end -}}'
}
add() {
local repo user
local args=(--silent)
while [ $# -gt 0 ]; do
case "$1" in
--permission)
args+=(-f permission="$2")
shift 2
;;
*)
if [ -z "$repo" ]; then
repo="${1#https://github.com/}"
shift 1
elif [ -z "$user" ]; then
user="$1"
shift 1
else
echo "invalid argument: $1" >&2
return 1
fi
;;
esac
done
if [ -z "$repo" ]; then
usage >&2
return 1
fi
if [ -z "$user" ]; then
if [ -t 0 ]; then
usage >&2
return 1
fi
while read -r user; do
if [[ $user == */* ]]; then
gh api --method=PUT "orgs/${repo%/*}/teams/${user#*/}/repos/$repo" "${args[@]}"
else
gh api --method=PUT "repos/$repo/collaborators/$user" "${args[@]}"
fi
done
fi
if [ -t 1 ]; then
local display
if ! display="$(user-or-team "$user" "$repo")"; then
echo "error looking up user or team: $user"
return 1
fi
read -r -n 1 -p "Add $display to the $repo repository? (y/N) "
echo
[ "$REPLY" = "y" ] || return 1
fi
if [[ $user == */* ]]; then
gh api --method=PUT "orgs/${repo%/*}/teams/${user#*/}/repos/$repo" "${args[@]}"
else
gh api --method=PUT "repos/$repo/collaborators/$user" "${args[@]}"
fi
}
user-or-team() {
local handle="${1?}"
local repo="${2?}"
if [[ $handle == */* ]]; then
local org="${handle%/*}"
[ -n "$org" ] || org="${repo%/*}"
gh api "orgs/${org}/teams/${handle#*/}" --jq '"team \(.organization.login)/\(.slug) (\(.name))"'
else
gh api "users/$handle" --jq '"\(.login) (\(.name))"'
fi
}
remove() {
local repo="${1#https://github.com/}"
local user="$2"
if [[ $user == */* ]]; then
gh api --method=DELETE "orgs/${repo%/*}/teams/${user#*/}/repos/$repo" --silent
else
gh api --method=DELETE "repos/$repo/collaborators/$user" --silent
fi
}
cmd="$1"
[ $# -eq 0 ] || shift 1
case "$cmd" in
-h|--help)
usage
;;
add)
add "$@"
;;
remove)
remove "$@"
;;
list)
list "$@"
;;
*)
usage >&2
exit 1
;;
esac