Skip to content

Commit 523d640

Browse files
authored
Feat: add action to keep package.json updated (#68)
1 parent 949d21c commit 523d640

2 files changed

Lines changed: 138 additions & 0 deletions

File tree

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
#!/usr/bin/env python3
2+
import json
3+
import sys
4+
from pathlib import Path
5+
6+
7+
def get_actual_files():
8+
"""Get all Python files from walter_modem folder (excluding .pyi type stubs)."""
9+
files = []
10+
walter_modem_dir = Path("walter_modem")
11+
12+
for py_file in sorted(walter_modem_dir.glob("*.py")):
13+
if py_file.is_file():
14+
files.append(py_file.relative_to(Path(".")))
15+
16+
for py_file in sorted((walter_modem_dir / "mixins").glob("*.py")):
17+
if py_file.is_file():
18+
files.append(py_file.relative_to(Path(".")))
19+
20+
return [str(f) for f in files]
21+
22+
23+
def get_github_urls(files):
24+
"""Generate GitHub URLs for package.json format."""
25+
return [[f, f"github:QuickSpot/walter-micropython/{f}"] for f in files]
26+
27+
28+
def validate_and_update_package_json():
29+
"""
30+
Validates and syncs package.json with actual files in walter_modem folder.
31+
Returns: (changed: bool, added: list, removed: list)
32+
"""
33+
package_json_path = "package.json"
34+
35+
with open(package_json_path, "r") as f:
36+
package_data = json.load(f)
37+
38+
actual_files = get_actual_files()
39+
correct_urls = get_github_urls(actual_files)
40+
41+
current_urls = package_data.get("urls", [])
42+
current_files = [url[0] for url in current_urls]
43+
44+
actual_set = set(actual_files)
45+
current_set = set(current_files)
46+
added = sorted(actual_set - current_set)
47+
removed = sorted(current_set - actual_set)
48+
49+
if added or removed:
50+
package_data["urls"] = correct_urls
51+
52+
with open(package_json_path, "w") as f:
53+
f.write("{\n")
54+
f.write(' "urls": [\n')
55+
for i, url_pair in enumerate(correct_urls):
56+
f.write(f' {json.dumps(url_pair)}')
57+
f.write("," if i < len(correct_urls) - 1 else "")
58+
f.write("\n")
59+
f.write(" ],\n")
60+
f.write(f' "version": "{package_data["version"]}"\n')
61+
f.write("}\n")
62+
63+
return True, added, removed
64+
65+
return False, [], []
66+
67+
68+
if __name__ == "__main__":
69+
try:
70+
changed, added, removed = validate_and_update_package_json()
71+
72+
if changed:
73+
if added:
74+
print(f"Added files: {added}")
75+
if removed:
76+
print(f"Removed files: {removed}")
77+
print(f"Updated package.json")
78+
sys.exit(1) # Exit 1 to signal changes for GitHub Actions
79+
else:
80+
print("package.json is in sync")
81+
sys.exit(0)
82+
83+
except Exception as e:
84+
print(f"Error: {e}", file=sys.stderr)
85+
sys.exit(2)
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
name: Update Package JSON on walter_modem Changes
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
paths:
8+
- 'walter_modem/**'
9+
- '.github/workflows/update-package-json.yml'
10+
11+
jobs:
12+
validate-and-update:
13+
runs-on: ubuntu-latest
14+
permissions:
15+
contents: write
16+
17+
steps:
18+
- name: Checkout code
19+
uses: actions/checkout@v4
20+
21+
- name: Set up Python
22+
uses: actions/setup-python@v4
23+
with:
24+
python-version: '3.11'
25+
26+
- name: Validate and update package.json
27+
id: validate
28+
run: |
29+
python .github/scripts/validate-package-json.py
30+
EXIT_CODE=$?
31+
echo "changes_made=$EXIT_CODE" >> $GITHUB_OUTPUT
32+
exit 0
33+
34+
- name: Commit and push changes
35+
if: steps.validate.outputs.changes_made == '1'
36+
run: |
37+
git config user.name "github-actions[bot]"
38+
git config user.email "github-actions[bot]@users.noreply.github.com"
39+
git add package.json
40+
git commit -m "Chore: sync package.json with walter_modem files"
41+
git push
42+
43+
- name: Comment on PR if changes made
44+
if: steps.validate.outputs.changes_made == '1' && github.event_name == 'pull_request'
45+
uses: actions/github-script@v7
46+
with:
47+
script: |
48+
github.rest.issues.createComment({
49+
issue_number: context.issue.number,
50+
owner: context.repo.owner,
51+
repo: context.repo.repo,
52+
body: '✓ package.json has been automatically updated to sync with walter_modem folder changes'
53+
})

0 commit comments

Comments
 (0)