-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgithub_sync.sh
More file actions
executable file
·85 lines (69 loc) · 4.06 KB
/
github_sync.sh
File metadata and controls
executable file
·85 lines (69 loc) · 4.06 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
#!/bin/bash
SEE_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
GITHUB_LOG="${SEE_DIR}/github_updates.log"
# update github with the latest data
update_github() {
if [ -f "${SEE_DIR}/.env" ]; then
source "${SEE_DIR}/.env"
echo "$(date): starting GitHub sync" >> "$GITHUB_LOG"
for file in "cumulative_data.csv" "past_24_hours_data.csv"; do
if [ -f "$SEE_DIR/$file" ]; then
# first get the SHA of the existing file
echo "$(date): getting SHA for $file" >> "$GITHUB_LOG"
SHA_RESPONSE=$(curl -s -H "Authorization: Bearer $GITHUB_TOKEN" \
-H "Accept: application/vnd.github+json" \
"https://api.github.com/repos/$GITHUB_REPO/contents/$file")
# check if file exists and extract SHA
if echo "$SHA_RESPONSE" | grep -q "sha"; then
# extract SHA using grep and cut
SHA=$(echo "$SHA_RESPONSE" | grep '"sha":' | head -1 | sed 's/.*"sha": "\([^"]*\)".*/\1/')
echo "$(date): found SHA: $SHA" >> "$GITHUB_LOG"
# encode file content
content=$(base64 -i "$SEE_DIR/$file")
timestamp=$(date -u +"%Y-%m-%d %H:%M:%S")
echo "$(date): updating $file on GitHub" >> "$GITHUB_LOG"
# update file with SHA
UPDATE_RESPONSE=$(curl -s -X PUT \
-H "Authorization: Bearer $GITHUB_TOKEN" \
-H "Accept: application/vnd.github+json" \
-H "X-GitHub-Api-Version: 2022-11-28" \
"https://api.github.com/repos/$GITHUB_REPO/contents/$file" \
-d "{\"message\":\"Update $file - $timestamp\",\"content\":\"$content\",\"sha\":\"$SHA\",\"committer\":{\"name\":\"bilals12\",\"email\":\"bilal.s12@protonmail.com\"}}")
echo "$UPDATE_RESPONSE" >> "$GITHUB_LOG"
# check response for success
if echo "$UPDATE_RESPONSE" | grep -q '"content":'; then
echo "$(date): successfully updated $file" >> "$GITHUB_LOG"
else
echo "$(date): failed to update $file" >> "$GITHUB_LOG"
fi
else
# file doesn't exist yet, create it
echo "$(date): file $file does not exist yet on GitHub, creating it" >> "$GITHUB_LOG"
# encode file content
content=$(base64 -i "$SEE_DIR/$file")
timestamp=$(date -u +"%Y-%m-%d %H:%M:%S")
# create new file
CREATE_RESPONSE=$(curl -s -X PUT \
-H "Authorization: Bearer $GITHUB_TOKEN" \
-H "Accept: application/vnd.github+json" \
-H "X-GitHub-Api-Version: 2022-11-28" \
"https://api.github.com/repos/$GITHUB_REPO/contents/$file" \
-d "{\"message\":\"Create $file - $timestamp\",\"content\":\"$content\",\"committer\":{\"name\":\"bilals12\",\"email\":\"bilal.s12@protonmail.com\"}}")
echo "$CREATE_RESPONSE" >> "$GITHUB_LOG"
# check response for success
if echo "$CREATE_RESPONSE" | grep -q '"content":'; then
echo "$(date): successfully created $file" >> "$GITHUB_LOG"
else
echo "$(date): failed to create $file" >> "$GITHUB_LOG"
fi
fi
else
echo "$(date): file $file not found locally, skipping" >> "$GITHUB_LOG"
fi
done
else
echo "$(date): .env file not found, skipping GitHub sync" >> "$GITHUB_LOG"
fi
}
# run the update
update_github