-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate.sh
72 lines (56 loc) · 1.64 KB
/
update.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
#!/bin/bash
if [ -f ./.env ] ;
then
source .env
else
echo "error: missing .env file. Unable to source authorization token."
exit 2
fi
RATE_RESPONSE=$(curl -s --fail -H "Authorization: token $TOKEN" "https://api.github.com/rate_limit")
OUTFILE_ISSUES="./_data/issues.json"
OUTFILE_COMMENTS="./_data/comments.json"
HEADERS_FILE="./tmp/headers"
if [ -f $OUTFILE_ISSUES ] ;
then
rm $OUTFILE_ISSUES
fi
if [ -f $OUTFILE_COMMENTS ] ;
then
rm $OUTFILE_COMMENTS
fi
mkdir -p "./tmp/"
if [ -f $HEADERS_FILE ] ;
then
rm $HEADERS_FILE
fi
depaginate_and_write () {
# Arguments:
# $1: initial target url
# $2: file to write to
TARGET_URL=$1
OUTFILE=$2
DONE=0
echo "[" >> $OUTFILE
while [ $DONE != 1 ] ;
do
RESPONSE=$(curl -D "$HEADERS_FILE" -s --fail \
-H "Authorization: token $TOKEN" \
"$TARGET_URL")
# Use pagination data to get next url.
TARGET_URL=$(cat $HEADERS_FILE | sed -n "s/^link: .*<\(.*\)>; rel=\"next\".*$/\1/p")
# No "next" in header => we're done.
if [ -z "$TARGET_URL" ] ;
then
DONE=1
fi
echo "${RESPONSE:1: -1}" >> $OUTFILE
echo "," >> $OUTFILE
done
# Remove the last ","
sed -i '$d' $OUTFILE
echo "]" >> $OUTFILE
}
PER_PAGE=100
depaginate_and_write "https://api.github.com/repos/cu-mkp/m-k-manuscript-data/issues?state=all&direction=asc&sort=created&page=1&per_page=$PER_PAGE" $OUTFILE_ISSUES
depaginate_and_write "https://api.github.com/repos/cu-mkp/m-k-manuscript-data/issues/comments?direction=asc&sort=created&per_page=$PER_PAGE" $OUTFILE_COMMENTS
rm -r "./tmp/"