diff --git a/.github/scripts/file-updater.sh b/.github/scripts/file-updater.sh new file mode 100644 index 0000000..380469e --- /dev/null +++ b/.github/scripts/file-updater.sh @@ -0,0 +1,48 @@ +# Retrieve parameters +fileName=$1 +fileURL=$2 +path=$3 +hashURL=$4 + +# Temporal folder to work on +# Needed in case a file is updated in the main folder, and the hash checking fails +downloadFolder="./.file_updater_action_temporal_folder/" +mkdir $downloadFolder +tempPath="$downloadFolder$fileName" + +# Store checksum +curlHashExit=1 +if [ "$path" != "" ]; then + checksum=$(curl $hashURL) + curlHashExit=$? +fi + +# Avoid download of same file +oldFileChecksum=$(sha256sum "$path$fileName") +if [ "$oldFileChecksum" = "$checksum $path$fileName" ]; then + echo "Current file checksum is the same as the new one" + exit 1 +fi + +# Download file +curl "$fileURL" -o $tempPath +curlFileExit=$? + +# If both downloads were succesful +if [[ $curlFileExit -eq 0 && $curlHashExit -eq 0 ]]; then + # Checksum of file + dlFileChecksum=$(sha256sum $tempPath) + # If matches + if [ "$dlFileChecksum" = "$checksum $tempPath" ]; then + mv $tempPath "$path$fileName" + # No need to check exit code, if mv fails GitHub actions will too + else + # If not, failure + echo "Hash does not match" >&2 + exit 2 + fi +else + # If not, failure + echo "Downloads failed" >&2 + exit 3 +fi \ No newline at end of file diff --git a/.github/workflows/assets-updater.yml b/.github/workflows/assets-updater.yml new file mode 100644 index 0000000..f2696d2 --- /dev/null +++ b/.github/workflows/assets-updater.yml @@ -0,0 +1,60 @@ +# This action updates assets files +name: Assets updater + +on: + # weekly + schedule: + - cron: '0 0 * * Sun' + +permissions: + contents: write + pull-requests: write + +jobs: + download: + runs-on: ubuntu-latest + + env: + workingBranch: assets-updater-action + steps: + - name: Checkout main + uses: actions/checkout@v3 + + - name: Git config + run: | + git config user.name 'github-actions[bot]' + git config user.email 'github-actions[bot]@users.noreply.github.com' + + - name: Check if branch exists + run: | + count=($(git ls-remote --heads origin "$workingBranch" | wc)) + if [[ ${count[0]} -ne 0 ]]; then + # Exists + git fetch + git checkout -b $workingBranch "origin/$workingBranch" + else + # Does not exist + git checkout -b $workingBranch + git push --set-upstream origin $workingBranch + fi + + - name: ClearURLs catalog + continue-on-error: true + run: | + bash ./.github/scripts/file-updater.sh $fileName $fileURL $filePath $hashURL + git add "$filePath$fileName" + git commit -m "$message" + git push + echo "$fileName commited successfully" + env: + fileName: "data.minify.json" + fileURL: "https://rules2.clearurls.xyz/data.min.json" + filePath: "./app/src/main/assets/" + hashURL: "https://rules2.clearurls.xyz/rules.min.hash" + message: "ClearURLs catalog updated" + + - name: Pull request + run: + gh pr create --title 'Automated assets updater action' --body '' + env: + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}