Commit 442fe31510b7

Vincent Demeester <vincent@sbr.pm>
2026-02-16 15:58:15
fix(github): used origin remote for fork owner detection
gh repo view returned upstream owner instead of fork owner, causing PR creation to fail when pushing from forks to upstream repositories.
1 parent d70051c
Changed files (2)
dots
pi
agent
extensions
github
dots/pi/agent/extensions/github/actions/pr.ts
@@ -320,13 +320,24 @@ export async function handlePRCreate(
 				const branch = branchResult.stdout.trim();
 				const defaultBranches = ["main", "master"];
 				if (branch && !defaultBranches.includes(branch)) {
-					// Detect fork owner so --head is <owner>:<branch>
+					// Detect fork owner from the "origin" remote URL.
+					// `gh repo view` returns the *upstream* repo owner (the
+					// GH default repo), not the fork, so we parse origin
+					// instead — that's where the branch was pushed.
 					const ownerResult = await pi.exec(
-						"gh", ["repo", "view", "--json", "owner", "-q", ".owner.login"],
-						{ cwd: gitCwd, timeout: 10000 },
+						"git", ["remote", "get-url", "origin"],
+						{ cwd: gitCwd, timeout: 5000 },
 					);
 					if (ownerResult.code === 0 && ownerResult.stdout.trim()) {
-						args.push("--head", `${ownerResult.stdout.trim()}:${branch}`);
+						const originUrl = ownerResult.stdout.trim();
+						// Extract owner from SSH (git@github.com:owner/repo) or HTTPS (github.com/owner/repo)
+						const match = originUrl.match(/[:/]([^/]+)\/[^/]+(?:\.git)?$/);
+						const forkOwner = match?.[1];
+						if (forkOwner) {
+							args.push("--head", `${forkOwner}:${branch}`);
+						} else {
+							args.push("--head", branch);
+						}
 					} else {
 						args.push("--head", branch);
 					}
dots/pi/agent/extensions/github/package.json
@@ -6,6 +6,7 @@
     "test": "bun test github.test.ts"
   },
   "devDependencies": {
+    "@mariozechner/pi-coding-agent": "*",
     "bun-types": "^1.0.0"
   }
 }