-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnrgh-sync.sh
executable file
·309 lines (262 loc) · 5.44 KB
/
nrgh-sync.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
#!/bin/bash
usage() {
echo "Usage: $(basename "$0") sync|delete [-n] --gh-token GH_TOKEN --nr-token NR_TOKEN [--delete]"
}
get_starred_repos() {
local token="$1"
local gh_username
cd "$(readlink -f "$(dirname "$0")")" || exit 9
gh_username="$(./github.sh -t "$token" /user | jq -r '.login')"
if [[ -z "$gh_username" ]]
then
echo "Failed to determine GitHub username" >&2
return 8
fi
./github.sh -t "$token" "/users/${gh_username}/starred" | \
jq -r '.[].full_name' | \
sort -f
}
_nr() {
local extra_args=()
if [[ -n "$NR_TOKEN" ]]
then
extra_args+=(--auth-key "$NR_TOKEN")
fi
newreleases "$@" "${extra_args[@]}"
}
nr() {
local res
while true
do
if res=$(_nr "$@" 2>&1)
then
echo "$res"
return
else
if grep -qi "too many requests" <<< "$res"
then
echo "$(date -Iseconds) Too many requests. We need to wait to continue." >&2
sleep 61m
else
echo "$res" >&2
return 1
fi
fi
done
}
get_discord_channel_id() {
# Get first discord channel ID
# NOTE This is *not* the Discord chan ID you can copy from the Discord UI
nr discord | awk '!/^ID/{ print $1; exit }'
}
is_already_subscribed() {
local repo="$1"
for proj in $(nr project search "$repo" --provider github | \
awk '!/^ID/ { print $2 }')
do
if [[ "$proj" == "$repo" ]]
then
return
fi
done
return 1
}
nr_list_gh_sub_page() {
local page="$1"
nr project list --provider github -p "$page" 2>/dev/null | \
awk '!/^ID/ { print $2 }'
}
nr_list_all_subscriptions() {
local output
local page=1
local proj
local projects=()
local res
while res="$(nr_list_gh_sub_page "$page")"
do
echo "Processing page ${page}" >&2
while read -r proj
do
if [[ -z "$proj" ]]
then
# No more projects, return output
for output in "${projects[@]}"
do
echo "$output"
done | sort -f
return
fi
projects+=("$proj")
done <<< "$res"
page=$(( page + 1 ))
# DEBUG
echo "Current project list: ${projects[*]}" >&2
done
}
nr_subscribe() {
local repo="$1"
local chan_id="${2:-$(get_discord_channel_id)}"
local email_freq=none
local action=add
echo "Subscribing to $repo"
if is_already_subscribed "$repo"
then
echo "Already subscribed to $repo. Updating." >&2
action=update
fi
nr project "$action" github "$repo" \
--exclude-prereleases \
--email "$email_freq" \
--discord "$chan_id"
}
nr_unsubscribe() {
echo "Unsubscribing from $1"
nr project remove github "$1"
}
sync_deletions() {
local starred_repos
local nr_projects
local delete_repo
mapfile -t starred_repos < <(get_starred_repos "$GITHUB_TOKEN")
mapfile -t nr_projects < <(nr_list_all_subscriptions)
if [[ -z "${starred_repos[*]}" ]]
then
echo "Failed to retrieve list of starred repos" >&2
return 7
fi
if [[ -z "${nr_projects[*]}" ]]
then
echo "Failed to retrieve list of subscribed repositories" >&2
return 9
fi
for proj in "${nr_projects[@]}"
do
delete_repo=1
for repo in "${starred_repos[@]}"
do
if [[ "$repo" == "$proj" ]]
then
delete_repo=0
break
fi
done
if [[ "$delete_repo" == "1" ]]
then
if [[ -n "$DRYRUN" ]]
then
echo "DRY RUN: Would delete subscription to $proj" >&2
else
nr_unsubscribe "$proj"
fi
fi
done
}
sync_starred() {
local discord_channel_id
local proj
local starred_repos
if [[ -n "$DRYRUN" ]]
then
discord_channel_id="__FAKE__DISCORD_CHANNEL_ID__"
else
discord_channel_id="$(get_discord_channel_id)"
fi
if [[ -z "$discord_channel_id" ]]
then
echo "Unable to determine discord channel ID." >&2
return 3
fi
# DEBUG
# starred_repos="$1"
# starred_repos="$(cat /tmp/stars_part2)"
mapfile -t starred_repos < <(get_starred_repos "$GITHUB_TOKEN")
if [[ -z "${starred_repos[*]}" ]]
then
echo "Failed to retrieve list of starred repos" >&2
return 7
fi
local index=0
for proj in "${starred_repos[@]}"
do
index=$(( index + 1 ))
echo "Processing $proj [${index}/${#starred_repos[@]}]"
if [[ -n "$DRYRUN" ]]
then
echo "DRY RUN: Would subscribe to $proj" >&2
else
if ! nr_subscribe "$proj" "$discord_channel_id"
then
echo "SHIT." >&2
return 7
break
fi
fi
done
if [[ -n "$DELETE" ]]
then
sync_deletions
fi
}
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]
then
# Default action
ACTION=sync
case "$1" in
help|h|-h|--help)
usage
exit 0
;;
sync)
ACTION=sync
shift
;;
delete)
ACTION=delete
shift
;;
esac
while [[ -n "$*" ]]
do
case "$1" in
--delete|-d)
# FIXME delete --delete is redundant
DELETE=1
shift
;;
--nr-token|-t)
NR_TOKEN="$2"
shift 2
;;
--gh-token|-g)
GITHUB_TOKEN="$2"
shift 2
;;
--dryrun|-n)
DRYRUN=1
shift
;;
*)
echo "Unknown option: $1"
usage
exit 2
;;
esac
done
if [[ -z "$GITHUB_TOKEN" ]]
then
echo "Missing GitHub token (--token TOKEN)" >&2
exit 5
fi
case "$ACTION" in
sync)
sync_starred
;;
delete)
sync_deletions
;;
*)
echo "Unknown action: $ACTION" >&2
exit 4
;;
esac
fi