-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithub-api-push.py
More file actions
33 lines (28 loc) · 1.03 KB
/
github-api-push.py
File metadata and controls
33 lines (28 loc) · 1.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#!/usr/bin/env python3
"""
GitHub API Push Script - Fallback when git push fails
Usage:
python3 scripts/github-api-push.py
"""
import os, requests, base64
from pathlib import Path
token = os.environ.get("GH_TOKEN", "")
repo = "LaugHan/papershare-github"
api = f"https://api.github.com/repos/{repo}"
hdrs = {"Authorization": f"token {token}", "Accept": "application/vnd.github.v3+json"}
def get_sha(p):
r = requests.get(f"{api}/contents/{p}", headers=hdrs)
return r.json()["sha"] if r.status_code == 200 else None
def push(p, c, m):
d = {"message": m, "content": base64.b64encode(c).decode()}
sha = get_sha(p)
if sha: d["sha"] = sha
r = requests.put(f"{api}/contents/{p}", headers=hdrs, json=d)
return r.status_code in [200, 201]
if __name__ == "__main__":
for path, msg in [
("daily-paper/index.html", "fix: update daily-paper via API"),
]:
fp = Path(__file__).parent.parent / path
if fp.exists():
print(f"{'✓' if push(path, fp.read_bytes(), msg) else '✗'} {path}")