Skip to content

Commit ebd1508

Browse files
update python libraries
1 parent 0ff6061 commit ebd1508

2 files changed

Lines changed: 53 additions & 28 deletions

File tree

.github/workflows/workflow.yaml

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,58 @@
11
name: Update Library
2+
23
on:
34
issues:
4-
types: [opened]
5+
types: [opened, edited] # Trigger on new and edited issues
56

67
jobs:
78
update:
89
runs-on: ubuntu-latest
910
permissions:
10-
contents: write # Grant write permissions
11+
contents: write # Needed to push changes
12+
issues: write # Needed to comment on issues
13+
1114
steps:
1215
- name: Checkout repository
13-
uses: actions/checkout@v2
16+
uses: actions/checkout@v3
1417

1518
- name: Set up Python
16-
uses: actions/setup-python@v2
19+
uses: actions/setup-python@v4
1720
with:
1821
python-version: '3.x'
1922

20-
- name: Install dependencies
21-
run: |
22-
python -m pip install --upgrade pip
23-
24-
2523
- name: Run Python script
2624
env:
2725
ISSUE_BODY: ${{ github.event.issue.body }}
28-
run: |
29-
python update_library.py
26+
run: python update_library.py
3027

3128
- name: Commit changes
29+
if: success()
3230
run: |
3331
git config --global user.name "github-actions"
3432
git config --global user.email "actions@github.com"
3533
git add data/library.json data/library.csv
36-
git commit -m "Update library data"
37-
git push origin HEAD:${{ github.ref }}
38-
- name: Close issue
34+
git commit -m "Update library data" || echo "No changes to commit"
35+
git push origin main
36+
37+
- name: Close issue on success
3938
if: success() && github.event.issue.state == 'open'
40-
uses: peter-evans/close-issue@v2
39+
uses: peter-evans/close-issue@v2
4140
with:
4241
issue-number: ${{ github.event.issue.number }}
43-
comment: "Library updated successfully and issue closed."
44-
reason: completed
45-
- name: Handle failure
46-
if: failure()
42+
comment: "✅ Library updated successfully and issue closed."
43+
reason: completed
44+
45+
- name: Comment on failure
46+
if: failure()
4747
uses: actions/github-script@v6
4848
with:
4949
script: |
5050
const issue_number = context.payload.issue.number;
51-
const comment = "There was an error updating the library. Please check the logs for details.";
51+
const body = "⚠️ Validation failed. Please submit valid JSON with `barcode`, `user`, and `action` fields.";
5252
await github.issues.createComment({
5353
...context.repo,
5454
issue_number,
55-
body: comment
55+
body
5656
});
5757
await github.issues.update({
5858
...context.repo,

update_library.py

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,33 @@
11
import json
22
import csv
33
import os
4+
import sys
45

56
# Load issue body from environment variable
6-
issue_body = os.environ["ISSUE_BODY"] # GitHub Actions will pass this variable
7-
data = json.loads(issue_body)
7+
issue_body = os.environ.get("ISSUE_BODY", "")
88

9+
try:
10+
# Try to parse JSON
11+
data = json.loads(issue_body)
12+
13+
# Validate required keys
14+
required_keys = {"barcode", "user", "action"}
15+
if not isinstance(data, dict):
16+
raise ValueError("Issue body is not a JSON object")
17+
18+
missing = required_keys - data.keys()
19+
if missing:
20+
raise ValueError(f"Missing required keys: {missing}")
21+
22+
except json.JSONDecodeError:
23+
print("❌ Issue body is not valid JSON.")
24+
sys.exit(1)
25+
26+
except ValueError as e:
27+
print(f"❌ Validation error: {e}")
28+
sys.exit(1)
29+
30+
# If we reach here, JSON is valid and has required fields
931
barcode = data["barcode"]
1032
user = data["user"]
1133
action = data["action"]
@@ -15,21 +37,24 @@
1537
books = json.load(f)
1638

1739
# Update book status
40+
updated = False
1841
for book in books:
1942
if book["Barcode"] == barcode:
2043
if action == "checkout":
2144
book["Status"] = "Checked Out"
2245
book["User"] = user
46+
updated = True
2347
elif action == "checkin":
2448
book["Status"] = "Available"
2549
book["User"] = ""
50+
updated = True
51+
52+
if not updated:
53+
print(f"⚠️ No book found with barcode {barcode}.")
54+
sys.exit(1)
2655

2756
# Save updated library data
2857
with open("data/library.json", "w") as f:
2958
json.dump(books, f, indent=2)
3059

31-
with open("data/library.csv", "w", newline="") as f:
32-
writer = csv.DictWriter(f, fieldnames=["Book Title", "Author", "Barcode", "Status", "User"])
33-
writer.writeheader()
34-
writer.writerows(books)
35-
60+
with open("data/library

0 commit comments

Comments
 (0)