-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgh-subscription
More file actions
executable file
·429 lines (409 loc) · 11.7 KB
/
gh-subscription
File metadata and controls
executable file
·429 lines (409 loc) · 11.7 KB
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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
#!/usr/bin/env bash
#
# Copyright (c) 2023 Igor B. Poretsky
#
# SPDX-License-Identifier: MIT
set -e
declare -a name_list
declare -i list_size
declare blocked
function usage {
cat <<EOF
Usage: gh subscription <subcommand> [<options>]
The subcommands are as follows:
list -- List watched repositories and followed users.
add -- Subscribe for watching a repository or following a user.
delete -- Unsubscribe from watching a repository or following a user.
block -- Block notifications from a repository or from a user.
unblock -- Unblock notifications from a repository or from a user.
status -- Print a subscription status for a repository or for a user.
Type "gh subscription <subcommand> --help"
to get a subcommand help.
EOF
}
# Get authenticated user subscriptions or repositories for specified user.
#
# -f -- list followers
# -b -- list blocked users
# Any other value is treated as owner of the repositories to be listed
function retrieve {
local -i pgn=0 count=0
local -ir limit=100
local endpoint="user/subscriptions" id=login
case $1 in
-f)
endpoint="user/following"
shift
;;
-b)
endpoint="user/blocks"
shift
;;
*)
id=full_name
;;
esac
[ -n "$1" ] && endpoint="users/$1/repos"
list_size=0
until [[ $count != 0 ]]; do
let pgn++ count=$limit
while read item; do
name_list[$((list_size++))]=$item
let count--
done < <(gh api $endpoint --jq ".[].$id" -X GET -F page=$pgn -F per_page=$limit)
done
}
function list_usage {
cat <<EOF
Usage: gh subscription list [{<owner> | --own | --foreign | --following | --blocked | --summary}] [--limit <n>]
List followed and blocked users and watched repositories.
If an owner is specified explicitly, only the repositories
of that particular owner are listed.
The other options are as follows:
--summary -- Print summary subscription info:
number of followed and blocked users
and number of watched repositories for each owner.
--own -- List only your own repositories.
--foreign -- List only foreign repositories.
--followed -- List followed users.
--blocked -- List blocked users.
--limit <n> -- Show no more than n repositories (30 by default).
EOF
}
function list_error {
echo "Command syntax error!"
echo
list_usage
exit 1
}
# Filter repositories list according to the specified parameters:
# $1 -- -b (blocked) | -n (not subscribed) | owner
# $2 -- limit (optional)
# $3 -- regexp (optional)
function filter {
local mode
case $1 in
-b)
mode=blocked
shift
;;
-n)
mode=unsub
shift
;;
*)
;;
esac
if [ $# -gt 0 ]; then
local -i count=0 limit=0
local owner=$1 grep_switches
[ $# -gt 1 ] && limit=$2
[ $# -gt 2 ] && grep_switches=$3
for ((i=0; $i < $list_size; i++))
do echo ${name_list[$i]} | grep $grep_switches "^$owner/" && [ $limit -gt 0 -a $((++count)) -ge $limit ] && break
done
else
for ((i=0; $i < $list_size; i++)); do
local repo=${name_list[$i]}
case $mode in
blocked)
subscribed $repo && [ "$blocked" = "true" ] && echo $repo
;;
unsub)
subscribed $repo || echo $repo
;;
*)
;;
esac
done
fi
}
function list {
local -i limit=0
local mode owner grep_switches
while [[ $# != 0 ]]; do
case $1 in
--limit)
shift
[ $# -eq 0 ] && list_error
let limit=$1 || list_error
;;
--own)
[ -n "$mode" ] && list_error
owner=`gh api user --jq '.login'`
mode=owner
;;
--foreign)
[ -n "$mode" ] && list_error
owner=`gh api user --jq '.login'`
mode=owner
grep_switches="-v"
;;
--followed)
[ -n "$mode" ] && list_error
mode=following
;;
--blocked)
[ -n "$mode" ] && list_error
mode=blocks
;;
--summary)
[ -n "$mode" -o -n "$owner" ] && list_error
mode=summary
;;
--help)
list_usage
exit
;;
*)
[ -n "$mode" ] && list_error
owner=$1
mode=owner
;;
esac
shift
done
if [[ $limit > 0 || -n $mode ]]; then
if [ $limit -eq 0 ]; then
case $mode in
following|blocks)
gh api user/$mode --jq '.[].login'
exit
;;
*)
let limit=30
;;
esac
fi
case $mode in
following)
retrieve -f
;;
blocks)
retrieve -b
;;
*)
retrieve
;;
esac
case $mode in
owner)
filter $owner $limit $grep_switches
;;
summary)
local -A owners_stat
local -i followers_count=0 blocks_count=0
for ((i=0; $i < $list_size; i++)); do
owner=$(dirname ${name_list[$i]})
count=${owners_stat[$owner]}
if [ -n "$count" ]
then owners_stat[$owner]=$((count + 1))
else owners_stat[$owner]=1
fi
done
retrieve -f
followers_count=$list_size
retrieve -b
blocks_count=$list_size
echo "Followed users: $followers_count"
echo "Blocked users: $blocks_count"
echo
echo "Watched repositories summary:"
count=0
for owner in ${!owners_stat[@]}; do
echo $owner ${owners_stat[$owner]}
[ $limit -gt 0 -a $((++count)) -ge $limit ] && break
done
;;
*)
for ((i=0; $i < $list_size; i++)); do
[ $limit -gt 0 -a $i -ge $limit ] && break
echo ${name_list[$i]}
done
;;
esac
else
gh api user/subscriptions --jq '.[].full_name'
fi
}
function subscribed {
case $1 in
*/*)
blocked=`gh api repos/$1/subscription --jq '.ignored' 2>/dev/null`
;;
*)
gh api user/following/$1 --silent 2>/dev/null
;;
esac
}
function banned {
gh api user/blocks/$1 --silent 2>/dev/null
}
# Execute "delete", "block", and "unblock" commands.
function manage_subscription {
local cmd=$1 options
case $1 in
add)
options="PUT -F subscribed=true"
report="Subscribed to"
;;
delete)
options=DELETE
report="Unsubscribed from"
;;
block)
options="PUT -F ignored=true"
report="Blocked notifications from"
;;
unblock)
options="PUT -F ignored=false"
report="Unblocked notifications from"
;;
*)
;;
esac
shift
case $1 in
--help)
echo "Usage: gh subscription $cmd {<owner>[/[<repo>]]}"
echo
echo "When repository is not specified, but owner name ends with slash,"
echo "the action is applied to all repositories of that owner."
;;
*/)
local -i count=0
local repo list
case $cmd in
add)
list="filter -n"
retrieve ${1%/}
;;
unblock)
list="filter -b"
retrieve ${1%/}
;;
*)
list="filter ${1%/}"
retrieve
;;
esac
while read repo; do
gh api repos/$repo/subscription --silent -X $options
let ++count
done < <($list)
repo=repositories
[ $count -eq 1 ] && repo=repository
echo "$report $count $repo owned by ${1%/}"
;;
*/*)
case $cmd in
add)
if subscribed $1; then
if [ "$blocked" = "true" ]
then echo "This repository is blocked. Unblock it at first"
else echo "Already subscribed"
fi
else
gh api repos/$1/subscription --silent -X $options
fi
;;
*)
if subscribed $1; then
case "$options" in
*$blocked)
;;
*)
gh api repos/$1/subscription --silent -X $options
;;
esac
else
echo "Not subscribed to $1"
fi
;;
esac
;;
*)
case $cmd in
add)
if banned $1
then echo "This user is blocked. Unblock it at first"
elif subscribed $1
then echo "Already followed"
else gh api user/following/$1 -X PUT
fi
;;
delete)
if subscribed $1
then gh api user/following/$1 -X DELETE
else echo "User $1 is not followed"
fi
;;
block)
if banned $1
then echo "User $1 is already blocked"
else gh api user/blocks/$1 -X PUT --silent
fi
;;
unblock)
if banned $1
then gh api user/blocks/$1 -X DELETE --silent
else echo "User $1 is not blocked"
fi
;;
*)
;;
esac
;;
esac
}
function subscription_status {
case $1 in
--help)
echo "Usage: gh subscription status <owner>[/<repo>]"
;;
*/*)
if subscribed $1; then
case $blocked in
false)
echo "Watched"
;;
*)
echo "Blocked"
;;
esac
else
echo "Not subscribed"
fi
;;
*)
if banned $1
then echo "Blocked"
elif subscribed $1
then echo "Followed"
else echo "Not subscribed"
fi
;;
esac
}
case $1 in
--help)
usage
;;
list)
shift
list "$@"
;;
status)
shift
subscription_status "$@"
;;
add|delete|block|unblock)
manage_subscription "$@"
;;
*)
echo "Unknown command \"$1\""
echo
usage
exit 1
;;
esac