Spaces:
Sleeping
Sleeping
feat: use user OAuth token for git push, log sync errors
Browse files
app.py
CHANGED
|
@@ -75,7 +75,7 @@ def is_allowed(args):
|
|
| 75 |
return base in SAFE_COMMANDS
|
| 76 |
|
| 77 |
|
| 78 |
-
def _try_sync_repo(owner: str, repo: str):
|
| 79 |
path = repo_path(owner, repo)
|
| 80 |
if not os.path.isdir(path) or not os.path.exists(os.path.join(path, ".git")):
|
| 81 |
print(f">>> _try_sync_repo skip: no repo at {path}", file=sys.stderr)
|
|
@@ -103,6 +103,11 @@ def _try_sync_repo(owner: str, repo: str):
|
|
| 103 |
else:
|
| 104 |
print(f">>> _try_sync_repo committed: {commit.stdout.strip()}", file=sys.stderr)
|
| 105 |
sys.stderr.flush()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 106 |
push = subprocess.run(["git", "push", "origin", f"HEAD:{branch}"], cwd=path, capture_output=True, text=True, timeout=30)
|
| 107 |
if push.returncode != 0:
|
| 108 |
print(f">>> _try_sync_repo push failed: rc={push.returncode} {push.stderr.strip()}", file=sys.stderr)
|
|
@@ -115,13 +120,14 @@ def _try_sync_repo(owner: str, repo: str):
|
|
| 115 |
sys.stderr.flush()
|
| 116 |
|
| 117 |
|
| 118 |
-
def spawn(command: str, owner: str = "", repo: str = ""):
|
| 119 |
try:
|
| 120 |
parsed = json.loads(command)
|
| 121 |
if isinstance(parsed, dict):
|
| 122 |
command = parsed.get("command", "")
|
| 123 |
owner = parsed.get("owner", "")
|
| 124 |
repo = parsed.get("repo", "")
|
|
|
|
| 125 |
except Exception:
|
| 126 |
pass
|
| 127 |
|
|
@@ -153,7 +159,7 @@ def spawn(command: str, owner: str = "", repo: str = ""):
|
|
| 153 |
)
|
| 154 |
output = (result.stdout + result.stderr).strip() or "(no output)"
|
| 155 |
if owner and repo:
|
| 156 |
-
threading.Thread(target=_try_sync_repo, args=(owner, repo), daemon=True).start()
|
| 157 |
return output
|
| 158 |
except subprocess.TimeoutExpired:
|
| 159 |
return "Error: command timed out\n"
|
|
|
|
| 75 |
return base in SAFE_COMMANDS
|
| 76 |
|
| 77 |
|
| 78 |
+
def _try_sync_repo(owner: str, repo: str, user_token: str = ""):
|
| 79 |
path = repo_path(owner, repo)
|
| 80 |
if not os.path.isdir(path) or not os.path.exists(os.path.join(path, ".git")):
|
| 81 |
print(f">>> _try_sync_repo skip: no repo at {path}", file=sys.stderr)
|
|
|
|
| 103 |
else:
|
| 104 |
print(f">>> _try_sync_repo committed: {commit.stdout.strip()}", file=sys.stderr)
|
| 105 |
sys.stderr.flush()
|
| 106 |
+
token = user_token or os.environ.get("GITHUB_TOKEN", "")
|
| 107 |
+
if token:
|
| 108 |
+
remote_url = subprocess.run(["git", "remote", "get-url", "origin"], cwd=path, capture_output=True, text=True).stdout.strip()
|
| 109 |
+
auth_url = remote_url.replace("https://", f"https://{token}@") if "github.com" in remote_url else remote_url
|
| 110 |
+
subprocess.run(["git", "remote", "set-url", "origin", auth_url], cwd=path, capture_output=True)
|
| 111 |
push = subprocess.run(["git", "push", "origin", f"HEAD:{branch}"], cwd=path, capture_output=True, text=True, timeout=30)
|
| 112 |
if push.returncode != 0:
|
| 113 |
print(f">>> _try_sync_repo push failed: rc={push.returncode} {push.stderr.strip()}", file=sys.stderr)
|
|
|
|
| 120 |
sys.stderr.flush()
|
| 121 |
|
| 122 |
|
| 123 |
+
def spawn(command: str, owner: str = "", repo: str = "", user_token: str = ""):
|
| 124 |
try:
|
| 125 |
parsed = json.loads(command)
|
| 126 |
if isinstance(parsed, dict):
|
| 127 |
command = parsed.get("command", "")
|
| 128 |
owner = parsed.get("owner", "")
|
| 129 |
repo = parsed.get("repo", "")
|
| 130 |
+
user_token = parsed.get("user_token", parsed.get("github_token", ""))
|
| 131 |
except Exception:
|
| 132 |
pass
|
| 133 |
|
|
|
|
| 159 |
)
|
| 160 |
output = (result.stdout + result.stderr).strip() or "(no output)"
|
| 161 |
if owner and repo:
|
| 162 |
+
threading.Thread(target=_try_sync_repo, args=(owner, repo, user_token), daemon=True).start()
|
| 163 |
return output
|
| 164 |
except subprocess.TimeoutExpired:
|
| 165 |
return "Error: command timed out\n"
|