Skip to content

Commit 20aa3bb

Browse files
committed
https parsing in URL sanatizing for git repo
1 parent 5c9b476 commit 20aa3bb

File tree

2 files changed

+83
-1
lines changed

2 files changed

+83
-1
lines changed

src/utils/__tests__/git.spec.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
extractRepositoryName,
1313
getWorkspaceGitInfo,
1414
GitRepositoryInfo,
15+
convertGitUrlToHttps,
1516
} from "../git"
1617
import { truncateOutput } from "../../integrations/misc/extract-text"
1718

@@ -562,6 +563,43 @@ describe("getGitRepositoryInfo", () => {
562563
})
563564
})
564565

566+
describe("convertGitUrlToHttps", () => {
567+
it("should leave HTTPS URLs unchanged", () => {
568+
const url = "https://github.com/RooCodeInc/Roo-Code.git"
569+
const converted = convertGitUrlToHttps(url)
570+
571+
expect(converted).toBe("https://github.com/RooCodeInc/Roo-Code.git")
572+
})
573+
574+
it("should convert SSH URLs to HTTPS format", () => {
575+
const url = "[email protected]:RooCodeInc/Roo-Code.git"
576+
const converted = convertGitUrlToHttps(url)
577+
578+
expect(converted).toBe("https://github.com/RooCodeInc/Roo-Code.git")
579+
})
580+
581+
it("should convert SSH URLs with ssh:// prefix to HTTPS format", () => {
582+
const url = "ssh://[email protected]/RooCodeInc/Roo-Code.git"
583+
const converted = convertGitUrlToHttps(url)
584+
585+
expect(converted).toBe("https://github.com/RooCodeInc/Roo-Code.git")
586+
})
587+
588+
it("should handle URLs without git@ prefix", () => {
589+
const url = "ssh://github.com/RooCodeInc/Roo-Code.git"
590+
const converted = convertGitUrlToHttps(url)
591+
592+
expect(converted).toBe("https://github.com/RooCodeInc/Roo-Code.git")
593+
})
594+
595+
it("should handle invalid URLs gracefully", () => {
596+
const url = "not-a-valid-url"
597+
const converted = convertGitUrlToHttps(url)
598+
599+
expect(converted).toBe("not-a-valid-url")
600+
})
601+
})
602+
565603
describe("sanitizeGitUrl", () => {
566604
it("should sanitize HTTPS URLs with credentials", () => {
567605
const url = "https://username:[email protected]/RooCodeInc/Roo-Code.git"

src/utils/git.ts

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ export async function getGitRepositoryInfo(workspaceRoot: string): Promise<GitRe
5151

5252
if (urlMatch && urlMatch[1]) {
5353
const url = urlMatch[1].trim()
54-
gitInfo.repositoryUrl = sanitizeGitUrl(url)
54+
// Sanitize the URL and convert to HTTPS format for telemetry
55+
gitInfo.repositoryUrl = convertGitUrlToHttps(sanitizeGitUrl(url))
5556
const repositoryName = extractRepositoryName(url)
5657
if (repositoryName) {
5758
gitInfo.repositoryName = repositoryName
@@ -88,6 +89,49 @@ export async function getGitRepositoryInfo(workspaceRoot: string): Promise<GitRe
8889
}
8990
}
9091

92+
/**
93+
* Sanitizes a git URL to remove sensitive information like tokens
94+
* @param url The original git URL
95+
* @returns Sanitized URL
96+
*/
97+
/**
98+
* Converts a git URL to HTTPS format
99+
* @param url The git URL to convert
100+
* @returns The URL in HTTPS format, or the original URL if conversion is not possible
101+
*/
102+
export function convertGitUrlToHttps(url: string): string {
103+
try {
104+
// Already HTTPS, just return it
105+
if (url.startsWith("https://")) {
106+
return url
107+
}
108+
109+
// Handle SSH format: git@github.com:user/repo.git -> https://github.com/user/repo.git
110+
if (url.startsWith("git@")) {
111+
const match = url.match(/git@([^:]+):(.+)/)
112+
if (match && match.length === 3) {
113+
const [, host, path] = match
114+
return `https://${host}/${path}`
115+
}
116+
}
117+
118+
// Handle SSH with protocol: ssh://git@github.com/user/repo.git -> https://github.com/user/repo.git
119+
if (url.startsWith("ssh://")) {
120+
const match = url.match(/ssh:\/\/(?:git@)?([^\/]+)\/(.+)/)
121+
if (match && match.length === 3) {
122+
const [, host, path] = match
123+
return `https://${host}/${path}`
124+
}
125+
}
126+
127+
// Return original URL if we can't convert it
128+
return url
129+
} catch {
130+
// If parsing fails, return original
131+
return url
132+
}
133+
}
134+
91135
/**
92136
* Sanitizes a git URL to remove sensitive information like tokens
93137
* @param url The original git URL

0 commit comments

Comments
 (0)