Skip to content

A continuation of HomeBox the inventory and organization system built for the Home User

Update error handling for Currencies sync, if already synced then just skip.

Katosbb9672214c59
diff --git a/.github/scripts/update_currencies.py b/.github/scripts/update_currencies.py
index 55a5b89f..51daf822 100644
--- a/.github/scripts/update_currencies.py
+++ b/.github/scripts/update_currencies.py
@@ -5,7 +5,7 @@ import os
def fetch_currencies():
try:
response = requests.get('https://restcountries.com/v3.1/all')
- response.raise_for_status() # Raise an error for HTTP errors
+ response.raise_for_status()
except requests.exceptions.Timeout:
print("Request to the API timed out.")
return []
@@ -14,7 +14,7 @@ def fetch_currencies():
return []
try:
- countries = response.json() # Attempt to parse the JSON response
+ countries = response.json()
except json.JSONDecodeError:
print("Failed to decode JSON from the response.")
return []
@@ -36,17 +36,30 @@ def fetch_currencies():
def save_currencies(currencies, file_path):
try:
- os.makedirs(os.path.dirname(file_path), exist_ok=True) # Create directories if they don't exist
+ os.makedirs(os.path.dirname(file_path), exist_ok=True)
with open(file_path, 'w', encoding='utf-8') as f:
json.dump(currencies, f, ensure_ascii=False, indent=4)
except IOError as e:
print(f"An error occurred while writing to the file: {e}")
+def load_existing_currencies(file_path):
+ try:
+ with open(file_path, 'r', encoding='utf-8') as f:
+ return json.load(f)
+ except (IOError, json.JSONDecodeError):
+ return [] # Return an empty list if file doesn't exist or is invalid
+
def main():
- currencies = fetch_currencies()
- if currencies: # Check if currencies were successfully fetched
- save_path = 'backend/internal/core/currencies/currencies.json'
- save_currencies(currencies, save_path)
+ save_path = 'backend/internal/core/currencies/currencies.json'
+
+ existing_currencies = load_existing_currencies(save_path)
+ new_currencies = fetch_currencies()
+
+ if new_currencies == existing_currencies:
+ print("Currencies up-to-date with API, skipping commit.")
+ else:
+ save_currencies(new_currencies, save_path)
+ print("Currencies updated and saved.")
if __name__ == "__main__":
- main()
+ main()
\ No newline at end of file
diff --git a/.github/workflows/update-currencies.yml b/.github/workflows/update-currencies.yml
index 28313143..258c8044 100644
--- a/.github/workflows/update-currencies.yml
+++ b/.github/workflows/update-currencies.yml
@@ -4,34 +4,38 @@ on:
push:
branches:
- main
- workflow_dispatch:
jobs:
update-currencies:
runs-on: ubuntu-latest
steps:
- - name: Checkout repository
+ - name: Checkout code
uses: actions/checkout@v2
- name: Set up Python
uses: actions/setup-python@v2
with:
- python-version: '3.x'
+ python-version: '3.8'
- name: Install dependencies
run: |
- python -m pip install requests
+ python -m pip install --upgrade pip
+ pip install requests
- name: Fetch and update currencies
run: python .github/scripts/update_currencies.py
- - name: Commit and push changes
+ - name: Check for changes, commit and push changes if needed
run: |
- git config --global user.name "github-actions[bot]"
- git config --global user.email "github-actions[bot]@users.noreply.github.com"
- git add backend/internal/core/currencies/currencies.json
- git commit -m "Update currencies JSON"
- git push
- env:
- GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
\ No newline at end of file
+ if [[ $(git status --porcelain) ]]; then
+ echo "Changes detected, committing..."
+ git config --global user.name "github-actions[bot]"
+ git config --global user.email "github-actions[bot]@users.noreply.github.com"
+ git add backend/internal/core/currencies/currencies.json
+ git commit -m "Update currencies.json"
+ git push
+ else
+ echo "Currencies up-to-date with API, skipping commit."
+ exit 0
+ fi
\ No newline at end of file