Assets updater #110
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 | |
# at this point we know the new file is different from the last because hashes are different (old==remote!=new) | |
git add "$FILE_PATH" | |
# update the timestamp | |
marker="/\*$FILE_NAME-timestamp\*/" | |
grep -rli "$marker" * | while read file ; do | |
echo "Updating timestamp from file $file" | |
sed -i "s|$marker\(.*\)$marker|$marker$(date +%s)000L$marker|" "$file" | |
git add "$file" | |
done | |
# create and push commit with the changes | |
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 |