-
-
Notifications
You must be signed in to change notification settings - Fork 57
135 lines (107 loc) · 4.03 KB
/
assets-updater.yml
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
# This action updates assets files
name: Assets updater
on:
# weekly
schedule:
- cron: '0 0 * * SUN'
# manually
workflow_dispatch:
permissions:
contents: write # need to update branches
pull-requests: write # need to create PRs
repository-projects: read # to edit PRs (see https://github.com/cli/cli/issues/6274)
jobs:
update:
runs-on: ubuntu-latest
strategy:
matrix:
include:
# ClearURL database
- fileName: "data.minify.json"
fileFolder: "./app/src/main/assets/"
fileUrl: "https://rules2.clearurls.xyz/data.minify.json"
hashUrl: "https://rules2.clearurls.xyz/rules.minify.hash"
# - fileName: "..."
# fileFolder: "..."
# fileUrl: "..."
# hashUrl: "..."
steps:
- name: Checkout master
uses: actions/checkout@v4
with:
fetch-depth: 0 # need to fetch other branches
- name: Git config
run: |
git config user.name 'github-actions'
git config user.email 'github-actions@github.com'
- name: Run script
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
# inputs
FILE_NAME="${{ matrix.fileName }}"
FILE_FOLDER="${{ matrix.fileFolder }}"
FILE_URL="${{ matrix.fileUrl }}"
HASH_URL="${{ matrix.hashUrl }}"
# dynamic inputs
BRANCH="actions/assets/$FILE_NAME"
FILE_PATH="$FILE_FOLDER$FILE_NAME"
# checkout branch (create if doesn't exist)
git checkout "$BRANCH" || git checkout -b "$BRANCH"
# get current hash
OLD_HASH=$(sha256sum "$FILE_PATH" | cut -d' ' -f1)
echo "Current hash: $OLD_HASH"
# get remote hash
REMOTE_HASH=$(curl "$HASH_URL")
echo "Remote hash: $REMOTE_HASH"
# compare hashes
if [ "$REMOTE_HASH" = "$OLD_HASH" ]; then
echo "Up-to date"
exit 0
fi
# download new file
curl "$FILE_URL" -o "$FILE_PATH"
# get new hash
NEW_HASH=$(sha256sum "$FILE_PATH" | cut -d' ' -f1)
echo "New hash: $NEW_HASH"
# check downloaded hash
if [ "$REMOTE_HASH" != "$NEW_HASH" ]; then
echo "[ERROR] Downloaded file hash doesn't match, aborted."
exit 1
fi
# create and push commit with the new file
# at this point we know the new file is different from the last because hashes are different (old==remote!=new)
git add "$FILE_PATH"
git commit -m "
[action] Updated $FILE_NAME
Url: $FILE_URL
Hash url: $HASH_URL
Old hash: $OLD_HASH
New hash: $NEW_HASH"
git push --set-upstream origin "$BRANCH"
# prepare pr
TITLE="updated $FILE_NAME"
# create details diff
if [[ $FILE_NAME == *.json ]]; then
echo "Including formatted json diff"
# download jd
wget --no-clobber -O /tmp/jd https://github.com/josephburnett/jd/releases/latest/download/jd-amd64-linux
chmod +x /tmp/jd
# create diff
DIFF="---
Formatted diff:
\`\`\`diff
$(git difftool --no-prompt --extcmd="/tmp/jd" master:$FILEPATH $FILEPATH)
\`\`\`"
fi
BODY="File $FILE_PATH was updated
$DIFF
---
This is an automatic PR run from a [github action](../actions/workflows/assets-updater.yml)"
if [ "$(gh pr list --head "$BRANCH" --json number --jq length)" = "0" ]; then
echo "Creating PR..."
gh pr create --label 'action' --title "updated $FILE_NAME" --body "$BODY"
else
echo "Updating PR"
gh pr edit --title "updated $FILE_NAME" --body "$BODY"
fi