Spaces:
Sleeping
Sleeping
fix: inject OAuth token into git remote URL before every command; add _ensure_auth_remote helper; always disable credential helper
Browse files
app.py
CHANGED
|
@@ -31,11 +31,10 @@ def repo_path(owner: str, repo: str) -> str:
|
|
| 31 |
def set_git_config(repo_dir: str):
|
| 32 |
subprocess.run(["git", "config", "user.email", "codexa-agent@users.noreply.github.com"], cwd=repo_dir, capture_output=True)
|
| 33 |
subprocess.run(["git", "config", "user.name", "Codexa Agent"], cwd=repo_dir, capture_output=True)
|
| 34 |
-
|
| 35 |
-
subprocess.run(["git", "config", "credential.helper", ""], cwd=repo_dir, capture_output=True)
|
| 36 |
|
| 37 |
|
| 38 |
-
def init_repo(owner: str, repo: str, remote_url: str = "") -> str:
|
| 39 |
path = repo_path(owner, repo)
|
| 40 |
os.makedirs(path, exist_ok=True)
|
| 41 |
|
|
@@ -46,19 +45,20 @@ def init_repo(owner: str, repo: str, remote_url: str = "") -> str:
|
|
| 46 |
else:
|
| 47 |
remote_url = f"https://github.com/{owner}/{repo}.git"
|
| 48 |
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
|
|
|
| 53 |
|
| 54 |
if not os.path.exists(os.path.join(path, ".git")):
|
| 55 |
result = subprocess.run(
|
| 56 |
-
["git", "clone",
|
| 57 |
capture_output=True, text=True, timeout=120, cwd=path
|
| 58 |
)
|
| 59 |
if result.returncode != 0:
|
| 60 |
subprocess.run(["git", "init", "-b", "main"], cwd=path, capture_output=True)
|
| 61 |
-
subprocess.run(["git", "remote", "add", "origin",
|
| 62 |
subprocess.run(["git", "add", "."], cwd=path, capture_output=True)
|
| 63 |
subprocess.run(["git", "commit", "-m", "init"], cwd=path, capture_output=True)
|
| 64 |
|
|
@@ -106,8 +106,19 @@ def _try_sync_repo(owner: str, repo: str, user_token: str = ""):
|
|
| 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 |
-
|
| 110 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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,6 +131,21 @@ def _try_sync_repo(owner: str, repo: str, user_token: str = ""):
|
|
| 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)
|
|
@@ -141,12 +167,17 @@ def spawn(command: str, owner: str = "", repo: str = "", user_token: str = ""):
|
|
| 141 |
if not is_allowed(args):
|
| 142 |
return f"Error: command '{args[0]}' not allowed in demo container\n"
|
| 143 |
|
|
|
|
|
|
|
|
|
|
| 144 |
cwd = os.environ.get("SPACE_ROOT", "/home/user")
|
| 145 |
repo_dir = ""
|
| 146 |
if owner and repo:
|
| 147 |
rpath = repo_path(owner, repo)
|
| 148 |
if not os.path.exists(rpath):
|
| 149 |
-
init_repo(owner, repo)
|
|
|
|
|
|
|
| 150 |
repo_dir = rpath
|
| 151 |
cwd = repo_dir
|
| 152 |
try:
|
|
|
|
| 31 |
def set_git_config(repo_dir: str):
|
| 32 |
subprocess.run(["git", "config", "user.email", "codexa-agent@users.noreply.github.com"], cwd=repo_dir, capture_output=True)
|
| 33 |
subprocess.run(["git", "config", "user.name", "Codexa Agent"], cwd=repo_dir, capture_output=True)
|
| 34 |
+
subprocess.run(["git", "config", "credential.helper", ""], cwd=repo_dir, capture_output=True)
|
|
|
|
| 35 |
|
| 36 |
|
| 37 |
+
def init_repo(owner: str, repo: str, remote_url: str = "", user_token: str = "") -> str:
|
| 38 |
path = repo_path(owner, repo)
|
| 39 |
os.makedirs(path, exist_ok=True)
|
| 40 |
|
|
|
|
| 45 |
else:
|
| 46 |
remote_url = f"https://github.com/{owner}/{repo}.git"
|
| 47 |
|
| 48 |
+
token = user_token or GITHUB_TOKEN
|
| 49 |
+
if token and "github.com" in remote_url:
|
| 50 |
+
remote_url = remote_url.replace("https://", f"https://{token}@")
|
| 51 |
+
elif GITHUB_TOKEN and "github.com" in remote_url and not user_token:
|
| 52 |
+
remote_url = remote_url.replace("https://", f"https://{GITHUB_TOKEN}@")
|
| 53 |
|
| 54 |
if not os.path.exists(os.path.join(path, ".git")):
|
| 55 |
result = subprocess.run(
|
| 56 |
+
["git", "clone", remote_url, path],
|
| 57 |
capture_output=True, text=True, timeout=120, cwd=path
|
| 58 |
)
|
| 59 |
if result.returncode != 0:
|
| 60 |
subprocess.run(["git", "init", "-b", "main"], cwd=path, capture_output=True)
|
| 61 |
+
subprocess.run(["git", "remote", "add", "origin", remote_url], cwd=path, capture_output=True)
|
| 62 |
subprocess.run(["git", "add", "."], cwd=path, capture_output=True)
|
| 63 |
subprocess.run(["git", "commit", "-m", "init"], cwd=path, capture_output=True)
|
| 64 |
|
|
|
|
| 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 |
+
if "github.com" in remote_url:
|
| 110 |
+
if remote_url.startswith("git@"):
|
| 111 |
+
remote_url = "https://github.com/" + remote_url.split(":", 1)[1]
|
| 112 |
+
auth_url = remote_url.replace("https://", f"https://{token}@", 1)
|
| 113 |
+
subprocess.run(["git", "remote", "set-url", "origin", auth_url], cwd=path, capture_output=True)
|
| 114 |
+
print(f">>> _try_sync_repo set-url {auth_url.split('@')[1]}", file=sys.stderr)
|
| 115 |
+
sys.stderr.flush()
|
| 116 |
+
elif token:
|
| 117 |
+
print(f">>> _try_sync_repo skip set-url, non-github remote: {remote_url!r}", file=sys.stderr)
|
| 118 |
+
sys.stderr.flush()
|
| 119 |
+
elif not token and "github.com" in (subprocess.run(["git", "remote", "get-url", "origin"], cwd=path, capture_output=True, text=True).stdout.strip() or ""):
|
| 120 |
+
print(">>> _try_sync_repo WARNING: pushing to github.com without a token", file=sys.stderr)
|
| 121 |
+
sys.stderr.flush()
|
| 122 |
push = subprocess.run(["git", "push", "origin", f"HEAD:{branch}"], cwd=path, capture_output=True, text=True, timeout=30)
|
| 123 |
if push.returncode != 0:
|
| 124 |
print(f">>> _try_sync_repo push failed: rc={push.returncode} {push.stderr.strip()}", file=sys.stderr)
|
|
|
|
| 131 |
sys.stderr.flush()
|
| 132 |
|
| 133 |
|
| 134 |
+
def _ensure_auth_remote(path: str, user_token: str):
|
| 135 |
+
token = user_token or GITHUB_TOKEN
|
| 136 |
+
if not token:
|
| 137 |
+
return
|
| 138 |
+
try:
|
| 139 |
+
remote_url = subprocess.run(["git", "remote", "get-url", "origin"], cwd=path, capture_output=True, text=True).stdout.strip()
|
| 140 |
+
if "github.com" in remote_url:
|
| 141 |
+
if remote_url.startswith("git@"):
|
| 142 |
+
remote_url = "https://github.com/" + remote_url.split(":", 1)[1]
|
| 143 |
+
auth_url = remote_url.replace("https://", f"https://{token}@", 1)
|
| 144 |
+
subprocess.run(["git", "remote", "set-url", "origin", auth_url], cwd=path, capture_output=True)
|
| 145 |
+
except Exception:
|
| 146 |
+
pass
|
| 147 |
+
|
| 148 |
+
|
| 149 |
def spawn(command: str, owner: str = "", repo: str = "", user_token: str = ""):
|
| 150 |
try:
|
| 151 |
parsed = json.loads(command)
|
|
|
|
| 167 |
if not is_allowed(args):
|
| 168 |
return f"Error: command '{args[0]}' not allowed in demo container\n"
|
| 169 |
|
| 170 |
+
print(f">>> spawn cmd={command!r} owner={owner!r} repo={repo!r} token={bool(user_token)}", file=sys.stderr)
|
| 171 |
+
sys.stderr.flush()
|
| 172 |
+
|
| 173 |
cwd = os.environ.get("SPACE_ROOT", "/home/user")
|
| 174 |
repo_dir = ""
|
| 175 |
if owner and repo:
|
| 176 |
rpath = repo_path(owner, repo)
|
| 177 |
if not os.path.exists(rpath):
|
| 178 |
+
init_repo(owner, repo, "", user_token)
|
| 179 |
+
if user_token:
|
| 180 |
+
_ensure_auth_remote(rpath, user_token)
|
| 181 |
repo_dir = rpath
|
| 182 |
cwd = repo_dir
|
| 183 |
try:
|