-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e5a75d0
commit 406f37d
Showing
5 changed files
with
203 additions
and
1 deletion.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
name: Update Supabase | ||
|
||
on: | ||
push: | ||
paths: | ||
- '**/*.png' | ||
- '**/*.jpg' | ||
|
||
jobs: | ||
update-db: | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- name: Checkout code | ||
uses: actions/checkout@v3 | ||
|
||
- name: Set up Python | ||
uses: actions/setup-python@v4 | ||
with: | ||
python-version: '3.x' | ||
|
||
- name: Install dependencies | ||
run: | | ||
python -m pip install --upgrade pip | ||
pip install supabase | ||
- name: Get all changed image files | ||
id: changed-image-files | ||
uses: tj-actions/changed-files@v45 | ||
with: | ||
recover_deleted_files: true | ||
recover_deleted_files_to_destination: 'actions/recovered' | ||
separator: "," | ||
files: | | ||
**/*.png | ||
**/*.jpg | ||
- name: List all changed image files | ||
if: steps.changed-image-files.outputs.any_changed == 'true' | ||
run: | | ||
IFS=$',' read -a MODIFIED_FILES_ARRAY <<< "${{ steps.changed-image-files.outputs.modified_files }}" | ||
for file in "${MODIFIED_FILES_ARRAY[@]}"; do | ||
echo $file | ||
done | ||
shell: | ||
bash | ||
|
||
- name: Process changes | ||
env: | ||
CHANGED_IMAGE_FILES: ${{ steps.changed-image-files.outputs.modified_files }} | ||
ADDED_IMAGE_FILES: ${{ steps.changed-image-files.outputs.added_files }} | ||
DELETED_IMAGE_FILES: ${{ steps.changed-image-files.outputs.deleted_files }} | ||
SUPABASE_URL: ${{ secrets.SUPABASE_URL }} | ||
SUPABASE_SERVICE_KEY: ${{ secrets.SUPABASE_SERVICE_KEY }} | ||
GITHUB_REPOSITORY_URL: ${{ github.repository }} | ||
run: python scripts/update_buckets.py |
Binary file not shown.
Binary file not shown.
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 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
import os | ||
import logging | ||
import sys | ||
|
||
logging.basicConfig(level=logging.INFO) | ||
logger = logging.getLogger(__name__) | ||
|
||
import os | ||
from supabase import create_client, Client | ||
|
||
API_KEY: str = os.getenv("SUPABASE_SERVICE_KEY") or "" | ||
SUPABASE_URL: str = os.getenv("SUPABASE_URL") or "" | ||
|
||
supabase: Client = create_client(SUPABASE_URL, API_KEY) | ||
|
||
def get_added_files_from_env(): | ||
try: | ||
added_files_env = os.getenv("ADDED_IMAGE_FILES") | ||
if not added_files_env: | ||
logger.error("No added image files found in the environment variable.") | ||
return [] | ||
|
||
added_files = [ | ||
file.strip() for file in added_files_env.split(",") | ||
if file.strip() | ||
] | ||
return added_files | ||
except Exception as e: | ||
logger.error(f"Error parsing added image files: {e}") | ||
sys.exit(1) | ||
|
||
def get_deleted_files_from_env(): | ||
try: | ||
deleted_files_env = os.getenv("DELETED_IMAGE_FILES") | ||
if not deleted_files_env: | ||
logger.error("No deleted image files found in the environment variable.") | ||
return [] | ||
|
||
deleted_files = [ | ||
file.strip() for file in deleted_files_env.split(",") | ||
if file.strip() | ||
] | ||
return deleted_files | ||
except Exception as e: | ||
logger.error(f"Error parsing deleted image files: {e}") | ||
sys.exit(1) | ||
|
||
def get_changed_files_from_env(): | ||
try: | ||
changed_files_env = os.getenv("CHANGED_IMAGE_FILES") | ||
if not changed_files_env: | ||
logger.error("No changed image files found in the environment variable.") | ||
return [] | ||
|
||
changed_files = [ | ||
file.strip() for file in changed_files_env.split(",") | ||
if file.strip() | ||
] | ||
return changed_files | ||
except Exception as e: | ||
logger.error(f"Error parsing changed image files: {e}") | ||
sys.exit(1) | ||
|
||
def check(response, metadata, field): | ||
return response[field] == metadata[field] | ||
|
||
def update_image(file): | ||
try: | ||
with open(file, "rb") as f: | ||
_ = supabase.storage.from_("images").upload( | ||
file=f, | ||
path=file, | ||
file_options={"cache-control": "3600", "upsert": "true"}, | ||
) | ||
logger.info(f"Successfully updated {file}") | ||
except Exception as e: | ||
logger.error(f"Error updating {file}: {e}") | ||
sys.exit(1) | ||
|
||
def create_image(file): | ||
try: | ||
with open(file, "rb") as f: | ||
_ = supabase.storage.from_("images").upload( | ||
file=f, | ||
path=file, | ||
file_options={"cache-control": "3600", "upsert": "false"}, | ||
) | ||
logger.info(f"Successfully created {file}") | ||
except Exception as e: | ||
logger.error(f"Error creating new post for {file}: {e}") | ||
sys.exit(1) | ||
|
||
def delete_image(file): | ||
try: | ||
supabase.storage.from_("images").remove([file]) | ||
logger.info(f"Successfully deleted {file}") | ||
except Exception as e: | ||
logger.error(f"Error deleting post for {file}: {e}") | ||
sys.exit(1) | ||
|
||
def is_image(file: str): | ||
return file.endswith('.png') or file.endswith('.jpg') | ||
|
||
def main(): | ||
try: | ||
repo_url = os.getenv("GITHUB_REPOSITORY_URL") | ||
if not repo_url: | ||
logger.error("GITHUB_REPOSITORY_URL environment variable not set.") | ||
sys.exit(1) | ||
|
||
# added | ||
added_files = get_added_files_from_env() | ||
if not added_files: | ||
logger.info("No image files were added.") | ||
|
||
for file in added_files: | ||
if is_image(file): | ||
logger.info(f"Processing {file}") | ||
create_image(file) | ||
|
||
# changed | ||
changed_files = get_changed_files_from_env() | ||
if not changed_files: | ||
logger.info("No image files were changed.") | ||
|
||
for file in changed_files: | ||
if is_image(file): | ||
logger.info(f"Processing {file}") | ||
update_image(file) | ||
|
||
# deleted | ||
deleted_files = get_deleted_files_from_env() | ||
if not deleted_files: | ||
logger.info("No image files were deleted.") | ||
|
||
for file in deleted_files: | ||
if is_image(file): | ||
logger.info(f"Processing {file}") | ||
delete_image(file) | ||
|
||
except Exception as e: | ||
logger.error(f"An unexpected error occurred: {e}") | ||
sys.exit(1) | ||
|
||
if __name__ == "__main__": | ||
main() |