-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgh-clean-notifications
executable file
·231 lines (199 loc) · 5.35 KB
/
gh-clean-notifications
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
#!/usr/bin/env bash
set -e
NOTIFICATION_QUERY="query NotificationsQuery(\$first: Int!, \$endCursor: String, \$miscQuery: String) {
viewer {
notificationThreads(first: \$first, after: \$endCursor, query: \$miscQuery) {
totalCount
pageInfo {
hasNextPage endCursor
}
nodes {
id title subject {
__typename ... on PullRequest {
state resourcePath
}
... on Issue {
state resourcePath
}
... on Discussion {
isAnswered resourcePath
}
}
}
}
}
}
"
MARK_IDS_AS_DONE_QUERY="
mutation MarkNotificationsAsDone(\$ids: [ID!]!) {
markNotificationsAsDone(input: {ids: \$ids}) {
success
}
}
"
BASE_QUERY="is:unread is:read"
STATES='"CLOSED","MERGED"'
BASE_LIMIT=50
VERBOSE=0
MIN_GH_VERSION="v2.40"
help() {
echo "gh clean-notifications"
echo "Cleans up merged and closed items from notifications in github."
echo "Syntax: gh clean-notifications [-h|-q|-l|-s|-v]"
echo ""
echo "Options:"
echo " -h -> Help"
echo " -q -> Query to pass onto getting notifications"
echo " Same as available here: https://github.com/notifications"
echo " Default: is:unread is:read"
echo " -l -> Limit on pagination. Default 50."
echo " -s -> Set the states to mark down as. Default is closed, and merged"
echo " -c -> Check that gh is correct version, and scopes are set."
echo " -v -> Verbose messaging."
}
check() {
has_errored=0
has_jq=$(which jq)
has_jq_exit=$?
if [ ${VERBOSE} -gt 0 ]; then
echo "Has jq: ${has_jq}"
fi
if [[ $has_jq_exit != "0" ]]; then
echo "jq is not installed. Please install jq."
has_errored=1
fi
current_gh_ver=$(gh --version | \
tail -n 1 | \
awk 'match($0, "\\/v.*$"){print substr($0,RSTART+1,RLENGTH)}'
)
if [ ${VERBOSE} -gt 0 ]; then
echo "Current gh cli version: ${current_gh_ver}"
fi
min_version=$(printf "%s\n%s" "${MIN_GH_VERSION}" "${current_gh_ver}" | \
sort -V | \
head -n 1
)
if [ "${min_version}" != "${MIN_GH_VERSION}" ]; then
echo "Minimum gh version needs to be ${MIN_GH_VERSION}. You have ${current_gh_ver}."
has_errored=1
fi
# Check the scopes of the API key to make sure we can run.
scopes_str=$(gh auth status | \
grep -i "Token scopes" | \
awk '{ gsub(/\047/, "", $0); split($0,s,": "); gsub(/, /, "\n", s[2]); print s[2] }'
)
org=0
notifications=0
repo=0
while read -r scope; do
case "${scope}" in
repo)
repo=1;;
read:org)
org=1;;
org)
org=1;;
notifications)
notifications=1;;
esac
done < <(echo "${scopes_str}")
if [ ${VERBOSE} -gt 0 ]; then
echo "The scope check returns:"
echo " repo: ${repo}"
echo " org: ${org}"
echo " notification: ${notifications}"
fi
if [ ${notifications} -eq 0 ] || [ ${org} -eq 0 ] || [ ${repo} -eq 0 ]; then
has_errored=1
echo "gh scopes are wrong. You need to add the following:"
if [ ${notifications} -eq 0 ]; then
echo " notifications"
fi
if [ ${repo} -eq 0 ]; then
echo " repo"
fi
if [ ${org} -eq 0 ]; then
echo " org:read"
fi
fi
if [ ${has_errored} -eq 0 ]; then
echo "All OK!"
fi
exit ${has_errored}
}
process_notifications() {
data=$(gh api graphql \
-F first="${BASE_LIMIT}" -F miscQuery="${BASE_QUERY}" \
-f query="${NOTIFICATION_QUERY}" \
--paginate \
-q ".data.viewer.notificationThreads.nodes[] | select(.subject.state == (${STATES})) | {\"id\":.id,\"title\":.title,\"path\":.subject.resourcePath}"
)
if [ ${#data} -gt 0 ]; then
IDS=()
while read -r item; do
echo "Marking as Done: $(echo "${item}" | jq -r .title)"
echo " https://github.com$(echo "${item}" | jq -r .path)"
IDS+=(-f "ids[]=$(echo "${item}" | jq -r .id)")
if test ${#IDS[@]} -eq 100
then
gh api graphql --silent "${IDS[@]}" -f query="${MARK_IDS_AS_DONE_QUERY}"
IDS=()
fi
# Since this indented with spaces, EOT is fully left aligned.
done <<EOT
$(echo "$data" | jq -s -c .[])
EOT
test ${#IDS[@]} -eq 0 ||
gh api graphql --silent "${IDS[@]}" -f query="${MARK_IDS_AS_DONE_QUERY}"
else
echo "No tasks to mark as done!"
fi
}
while getopts :hq:l:s:vc flag
do
case "${flag}" in
h) # Display help
help
exit;;
q) # Update default query
BASE_QUERY=${OPTARG};;
l) # Update base limit
value=${OPTARG}
re='^[0-9]+$'
if ! [[ $value =~ $re ]] ; then
echo "-l wasn't set to a number: ${value}" >&2;
exit 1;
fi
BASE_LIMIT=${value};;
v) # Do verbose messages
VERBOSE=1;;
s) # Process states into a string for jq
value=$(echo "${OPTARG}" | tr '[:lower:]' '[:upper:]')
oldIFS=$IFS
IFS=','
STATES=""
values=( "${value}" )
IFS=$oldIFS
for item in "${values[@]}"; do
if [ -z "${STATES}" ]; then
STATES="\"${item}\""
else
STATES="${STATES},\"${item}\""
fi
done;;
c)
check;;
\?)
echo "Error: Invalid option was specified." >&2;
echo "" >&2
help
exit 2;;
esac
done
if [ ${VERBOSE} -gt 0 ]; then
echo "Input arguments are:"
echo "Query: ${BASE_QUERY}"
echo "Limit: ${BASE_LIMIT}"
echo "States: ${STATES}"
fi
process_notifications