Skip to content

Commit aa66c8b

Browse files
fix: normalize url
1 parent b5d5f87 commit aa66c8b

File tree

2 files changed

+91
-24
lines changed

2 files changed

+91
-24
lines changed

extensions/vscode/src/extension/VsCodeMessenger.ts

Lines changed: 40 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,43 @@ export class VsCodeMessenger {
429429
const configHandler = await configHandlerPromise;
430430
const { agentSessionId } = msg.data;
431431

432+
// Helper function to normalize repository URLs
433+
const normalizeRepoUrl = (url: string): string => {
434+
if (!url) return "";
435+
436+
let normalized = url.trim();
437+
438+
// Convert SSH to HTTPS: git@github.com:owner/repo.git -> https://github.com/owner/repo
439+
if (normalized.startsWith("[email protected]:")) {
440+
normalized = normalized.replace(
441+
442+
"https://github.com/",
443+
);
444+
}
445+
446+
// Convert shorthand owner/repo to full URL
447+
if (
448+
normalized.includes("/") &&
449+
!normalized.startsWith("http") &&
450+
!normalized.startsWith("git@")
451+
) {
452+
normalized = `https://github.com/${normalized}`;
453+
}
454+
455+
// Remove .git suffix
456+
if (normalized.endsWith(".git")) {
457+
normalized = normalized.slice(0, -4);
458+
}
459+
460+
// Remove trailing slash
461+
if (normalized.endsWith("/")) {
462+
normalized = normalized.slice(0, -1);
463+
}
464+
465+
// Normalize to lowercase
466+
return normalized.toLowerCase();
467+
};
468+
432469
try {
433470
// First, fetch the agent session to get repo URL and branch
434471
const agentSession =
@@ -459,20 +496,14 @@ export class VsCodeMessenger {
459496
return;
460497
}
461498

499+
const normalizedAgentRepo = normalizeRepoUrl(repoUrl);
500+
462501
// Find the workspace that matches the agent's repo URL
463502
let matchingWorkspace: string | null = null;
464503
for (const workspaceDir of workspaceDirs) {
465504
const repoName = await this.ide.getRepoName(workspaceDir);
466505
if (repoName) {
467-
// Normalize repo URLs for comparison
468-
const normalizedRepoName =
469-
repoName.includes("/") && !repoName.startsWith("http")
470-
? `https://github.com/${repoName}`
471-
: repoName;
472-
const normalizedAgentRepo =
473-
repoUrl.includes("/") && !repoUrl.startsWith("http")
474-
? `https://github.com/${repoUrl}`
475-
: repoUrl;
506+
const normalizedRepoName = normalizeRepoUrl(repoName);
476507

477508
if (normalizedRepoName === normalizedAgentRepo) {
478509
matchingWorkspace = workspaceDir;

gui/src/components/BackgroundMode/AgentsList.tsx

Lines changed: 51 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,40 @@ interface AgentsListProps {
2020
isCreatingAgent?: boolean;
2121
}
2222

23+
// Robust URL normalization function
24+
const normalizeRepoUrl = (url: string | undefined | null): string => {
25+
if (!url) return "";
26+
27+
let normalized = url.trim();
28+
29+
// Convert SSH to HTTPS: git@github.com:owner/repo.git -> https://github.com/owner/repo
30+
if (normalized.startsWith("[email protected]:")) {
31+
normalized = normalized.replace("[email protected]:", "https://github.com/");
32+
}
33+
34+
// Convert shorthand owner/repo to full URL
35+
if (
36+
normalized.includes("/") &&
37+
!normalized.startsWith("http") &&
38+
!normalized.startsWith("git@")
39+
) {
40+
normalized = `https://github.com/${normalized}`;
41+
}
42+
43+
// Remove .git suffix
44+
if (normalized.endsWith(".git")) {
45+
normalized = normalized.slice(0, -4);
46+
}
47+
48+
// Remove trailing slash
49+
if (normalized.endsWith("/")) {
50+
normalized = normalized.slice(0, -1);
51+
}
52+
53+
// Normalize to lowercase
54+
return normalized.toLowerCase();
55+
};
56+
2357
export function AgentsList({ isCreatingAgent = false }: AgentsListProps) {
2458
const { session } = useAuth();
2559
const ideMessenger = useContext(IdeMessengerContext);
@@ -44,16 +78,14 @@ export function AgentsList({ isCreatingAgent = false }: AgentsListProps) {
4478
dir,
4579
});
4680
if (repoNameResult.status === "success" && repoNameResult.content) {
47-
// Normalize repo URL
48-
const repoName = repoNameResult.content;
49-
const normalizedUrl =
50-
repoName.includes("/") && !repoName.startsWith("http")
51-
? `https://github.com/${repoName}`
52-
: repoName;
53-
repoUrls.push(normalizedUrl);
81+
const normalizedUrl = normalizeRepoUrl(repoNameResult.content);
82+
if (normalizedUrl) {
83+
repoUrls.push(normalizedUrl);
84+
}
5485
}
5586
}
5687
setWorkspaceRepoUrls(repoUrls);
88+
console.log("Workspace repo URLs:", repoUrls);
5789
}
5890
} catch (err) {
5991
console.error("Failed to fetch workspace repos:", err);
@@ -113,14 +145,18 @@ export function AgentsList({ isCreatingAgent = false }: AgentsListProps) {
113145
}, [session, ideMessenger, currentOrg]);
114146

115147
// Helper function to check if an agent's repo matches any workspace repo
116-
const isAgentInCurrentWorkspace = (agentRepoUrl: string): boolean => {
117-
const normalizedAgentRepo =
118-
agentRepoUrl.includes("/") && !agentRepoUrl.startsWith("http")
119-
? `https://github.com/${agentRepoUrl}`
120-
: agentRepoUrl;
148+
const isAgentInCurrentWorkspace = (agent: Agent): boolean => {
149+
// Get all possible agent repo URLs (both repoUrl and metadata.github_repo)
150+
const agentUrls = [agent.repoUrl, agent.metadata?.github_repo]
151+
.filter(Boolean)
152+
.map(normalizeRepoUrl)
153+
.filter((url) => url !== "");
154+
155+
console.log("Agent URLs:", agentUrls, "Workspace URLs:", workspaceRepoUrls);
121156

122-
return workspaceRepoUrls.some(
123-
(workspaceUrl) => workspaceUrl === normalizedAgentRepo,
157+
// Check if any of the agent URLs match any workspace URL
158+
return workspaceRepoUrls.some((workspaceUrl) =>
159+
agentUrls.some((agentUrl) => agentUrl === workspaceUrl),
124160
);
125161
};
126162

@@ -160,7 +196,7 @@ export function AgentsList({ isCreatingAgent = false }: AgentsListProps) {
160196
</div>
161197
<div className="flex flex-col gap-1 px-2">
162198
{agents.map((agent) => {
163-
const isInWorkspace = isAgentInCurrentWorkspace(agent.repoUrl);
199+
const isInWorkspace = isAgentInCurrentWorkspace(agent);
164200
const canOpenLocally = isInWorkspace;
165201

166202
return (

0 commit comments

Comments
 (0)