Skip to content

Commit 6c227bb

Browse files
Merge pull request #242 from CodeForPhilly/github_action_library_api_metadata
github action for running the library metadata pipeline:
2 parents 562d643 + f729aa0 commit 6c227bb

File tree

4 files changed

+100
-73
lines changed

4 files changed

+100
-73
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
name: Load Library API Metadata
2+
3+
on:
4+
workflow_dispatch:
5+
6+
jobs:
7+
run-script:
8+
runs-on: ubuntu-latest
9+
10+
steps:
11+
- name: Checkout repo
12+
uses: actions/checkout@v4
13+
14+
- name: Set up Python
15+
uses: actions/setup-python@v5
16+
with:
17+
python-version: "3.11"
18+
19+
- name: Install dependencies
20+
working-directory: scripts
21+
run: pip install -r requirements.txt
22+
23+
- name: Create GCP credentials file
24+
run: |
25+
echo '${{ secrets.GCP_SERVICE_ACCOUNT_KEY }}' > gcp-key.json
26+
27+
- name: Run script
28+
working-directory: scripts
29+
env:
30+
GOOGLE_APPLICATION_CREDENTIALS: gcp-key.json
31+
run: |
32+
python your_script.py
33+
34+
- name: Cleanup credentials
35+
run: rm gcp-key.json

scripts/import-documents.py

Lines changed: 0 additions & 68 deletions
This file was deleted.
Lines changed: 63 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,22 @@
1-
import json
21
from copy import deepcopy
32
import requests
3+
import firebase_admin
4+
from firebase_admin import credentials, storage, firestore
5+
import json
6+
from datetime import datetime
7+
8+
# -----------------------------------
9+
# INIT FIREBASE
10+
# -----------------------------------
11+
12+
cred = credentials.ApplicationDefault()
13+
14+
firebase_admin.initialize_app(cred, {
15+
"storageBucket": "benefit-decision-toolkit-play.firebasestorage.app"
16+
})
17+
18+
db = firestore.client()
19+
bucket = storage.bucket()
420

521

622
# --------------------------------------------
@@ -234,12 +250,49 @@ def transform_situation_format(data):
234250
return data
235251

236252

253+
def save_json_to_storage_and_update_firestore(json_string, firestore_doc_path):
254+
"""
255+
Upload JSON string to Firebase Storage and update Firestore
256+
with the storage path or download URL of the uploaded file.
257+
"""
258+
259+
# ---------------------
260+
# Create filename
261+
# Example: exported_2025-02-12_14-30-59.json
262+
# ---------------------
263+
timestamp = datetime.utcnow().strftime("%Y-%m-%d_%H-%M-%S")
264+
filename = f"LibraryApiSchemaExports/export_{timestamp}.json"
265+
266+
# ---------------------
267+
# Upload to storage
268+
# ---------------------
269+
blob = bucket.blob(filename)
270+
blob.upload_from_string(json_string, content_type="application/json")
271+
272+
# Get the storage path
273+
storage_path = blob.name
274+
275+
# ---------------------
276+
# Update Firestore
277+
# ---------------------
278+
doc_ref = db.document(firestore_doc_path)
279+
doc_ref.set({
280+
"latestJsonStoragePath": storage_path,
281+
"updatedAt": firestore.SERVER_TIMESTAMP
282+
}, merge=True)
283+
284+
print("Uploaded:", storage_path)
285+
print("Firestore updated!")
286+
287+
return storage_path
288+
289+
237290
# --------------------------------------------
238291
# Load your OpenAPI JSON here
239292
# --------------------------------------------
240293
if __name__ == "__main__":
241294

242-
url = "https://library-api-cnsoqyluna-uc.a.run.app/q/openapi.json"
295+
url = "https://library-api-1034049717668.us-central1.run.app/q/openapi.json"
243296

244297
# Send a GET request
245298
response = requests.get(url)
@@ -260,7 +313,12 @@ def transform_situation_format(data):
260313
check.pop("inputs")
261314

262315
# Write JSON file using UTF-8 to avoid errors
263-
with open("endpoint_inputs.json", "w", encoding="utf-8") as out:
264-
json.dump(check_records, out, indent=2, ensure_ascii=False)
316+
json_string = json.dumps(check_records, indent=2, ensure_ascii=False)
317+
318+
print("Parsed json")
319+
print(json_string)
265320

266-
print("Output written to endpoint_inputs.json")
321+
save_json_to_storage_and_update_firestore(
322+
json_string,
323+
firestore_doc_path="system/config"
324+
)

scripts/requirements.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
requests>=2.31.0
2+
firebase-admin>=6.4.0

0 commit comments

Comments
 (0)