This file contains hidden or 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
name: Obfuscate allowlist, see https://github.com/Bon-Appetit/porn-domains/discussions/75 | |
on: | |
push: | |
branches: | |
- main | |
paths: | |
- allow.txt | |
env: | |
LIST_FILE: allow.txt | |
LIST_FILE_BASE_NAME: allow | |
LIST_TYPE: allowlist | |
concurrency: | |
group: obfuscate-${{ github.ref }} | |
cancel-in-progress: false | |
jobs: | |
obfuscate: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout repository | |
uses: actions/checkout@v3 | |
- name: Pull latest changes with rebase | |
id: pull_rebase_latest | |
run: | | |
set -e | |
git pull --rebase "https://x-access-token:${{ secrets.GITHUB_TOKEN }}@github.com/${{ github.repository }}.git" main | |
- name: Check if required files exist | |
id: check_required_files | |
run: | | |
set -e | |
for file in "${{ env.LIST_FILE }}" meta.json; do | |
if [ ! -f "$file" ]; then | |
echo "[ERROR] File '$file' not found" | |
exit 1 | |
fi | |
done | |
- name: Generate new filename for list file | |
id: hash_and_rename | |
run: | | |
set -e | |
# Get the hash of the files content and cut it to the first 10 characters | |
FILE_HASH=$(sha1sum "${{ env.LIST_FILE }}" | awk '{print $1}' | cut -c1-10) | |
echo "[DEBUG] File hash: $FILE_HASH" | |
# Generate a random hash for the filename | |
RANDOM_HASH=$(openssl rand -base64 24 | tr -dc 'a-z0-9' | head -c 6) | |
echo "[DEBUG] Random hash: $RANDOM_HASH" | |
NEW_LIST_FILE_NAME="${{ env.LIST_FILE_BASE_NAME }}.${FILE_HASH}.${RANDOM_HASH}.txt" | |
echo "new_list_file_name=$NEW_LIST_FILE_NAME" >> $GITHUB_ENV | |
git mv "${{ env.LIST_FILE }}" "$NEW_LIST_FILE_NAME" | |
echo "[DEBUG] New filename: $NEW_LIST_FILE_NAME" | |
- name: Remove previous list file | |
id: remove_old_list_file | |
run: | | |
set -e | |
OLD_LIST_FILE_NAME=$(jq -r ".${{ env.LIST_TYPE }}.name // empty" meta.json) | |
if [ -n "$OLD_LIST_FILE_NAME" ] && [ -f "$OLD_LIST_FILE_NAME" ]; then | |
echo "[DEBUG] Removing old list file: $OLD_LIST_FILE_NAME" | |
git rm "$OLD_LIST_FILE_NAME" | |
else | |
echo "[DEBUG] No old list file to remove" | |
fi | |
- name: Update meta.json with list file info | |
id: update_meta_file | |
run: | | |
set -e | |
# Get last modification timestamp of the list file | |
FILE_TS=$(stat -c %Y "${{ env.new_list_file_name }}") | |
UPDATED=$(date -u -d "@$FILE_TS" +"%Y-%m-%dT%H:%M:%SZ") | |
echo "[DEBUG] Updated: $UPDATED" | |
# Get number of lines in the file | |
LINES=$(grep -c '^' "${{ env.new_list_file_name }}") | |
echo "[DEBUG] Lines: $LINES" | |
# Get file size in bytes | |
SIZE=$(stat -c %s "${{ env.new_list_file_name }}") | |
echo "[DEBUG] Size: $SIZE" | |
# Set proper locale for numeric format | |
export LC_NUMERIC="en_US.UTF-8" | |
# Format lines (e.g. "1,234 lines") | |
LINES_FORMAT=$(printf "%'d" "$LINES") | |
echo "[DEBUG] Lines format: $LINES_FORMAT" | |
# Format size (e.g. "2.1 KB") based on size | |
if [ "$SIZE" -lt 1024 ]; then | |
SIZE_FORMAT="${SIZE} B" | |
elif [ "$SIZE" -lt $((1024*1024)) ]; then | |
SIZE_FORMAT=$(awk "BEGIN {printf \"%.1f KB\", $SIZE/1024}") | |
else | |
SIZE_FORMAT=$(awk "BEGIN {printf \"%.1f MB\", $SIZE/1024/1024}") | |
fi | |
echo "[DEBUG] Size format: $SIZE_FORMAT" | |
jq --arg filename "${{ env.new_list_file_name }}" \ | |
--arg updated "$UPDATED" \ | |
--argjson lines "$LINES" \ | |
--arg lines_format "$LINES_FORMAT" \ | |
--argjson size "$SIZE" \ | |
--arg size_format "$SIZE_FORMAT" \ | |
".${{ env.LIST_TYPE }} = {name: \$filename, updated: \$updated, lines: \$lines, lines_format: \$lines_format, size: \$size, size_format: \$size_format}" \ | |
meta.json > meta.tmp && mv meta.tmp meta.json | |
- name: Commit and push changes | |
id: commit_and_push | |
run: | | |
set -e | |
git config user.name "github-actions" | |
git config user.email "[email protected]" | |
git add "${{ env.new_list_file_name }}" meta.json | |
git commit -m "[ACTION] Obfuscate ${{ env.LIST_TYPE }}" || echo "Nothing to commit" | |
git push "https://x-access-token:${{ secrets.GITHUB_TOKEN }}@github.com/${{ github.repository }}.git" HEAD:main |