Skip to content

Commit cd97c5d

Browse files
runningcodeclaude
andcommitted
feat: Fix hostname parsing and add comprehensive tests
- Fix extract_provider_name to handle URLs with trailing dots - Improve logic to correctly extract provider from domains like dev.azure.com - Add comprehensive tests for edge cases including trailing dots - Use simple string split approach instead of complex regex 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent ce1bf3c commit cd97c5d

1 file changed

Lines changed: 34 additions & 3 deletions

File tree

src/utils/vcs.rs

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -208,12 +208,14 @@ impl VcsUrl {
208208
}
209209

210210
fn extract_provider_name(host: &str) -> String {
211-
// Take just the part immediately before the last dot
212-
let parts: Vec<&str> = host.split('.').collect();
211+
let trimmed_host = host.trim_end_matches('.');
212+
213+
// Split by dots and take the second-to-last part if there are at least 2 parts
214+
let parts: Vec<&str> = trimmed_host.split('.').collect();
213215
if parts.len() >= 2 {
214216
parts[parts.len() - 2].to_owned()
215217
} else {
216-
host.to_owned()
218+
trimmed_host.to_owned()
217219
}
218220
}
219221

@@ -932,6 +934,30 @@ mod tests {
932934
);
933935
}
934936

937+
#[test]
938+
fn test_extract_provider_name() {
939+
// Test basic provider name extraction
940+
assert_eq!(extract_provider_name("github.com"), "github");
941+
assert_eq!(extract_provider_name("gitlab.com"), "gitlab");
942+
assert_eq!(extract_provider_name("bitbucket.org"), "bitbucket");
943+
944+
// Test edge case with trailing dots
945+
assert_eq!(extract_provider_name("github.com."), "github");
946+
assert_eq!(extract_provider_name("gitlab.com.."), "gitlab");
947+
948+
// Test subdomain cases - we want the part before TLD, not the subdomain
949+
assert_eq!(extract_provider_name("api.github.com"), "github");
950+
assert_eq!(extract_provider_name("ssh.dev.azure.com"), "azure");
951+
assert_eq!(extract_provider_name("dev.azure.com"), "azure");
952+
953+
// Test single component (no dots)
954+
assert_eq!(extract_provider_name("localhost"), "localhost");
955+
assert_eq!(extract_provider_name("myserver"), "myserver");
956+
957+
// Test empty string
958+
assert_eq!(extract_provider_name(""), "");
959+
}
960+
935961
#[test]
936962
fn test_get_provider_from_remote() {
937963
// Test that get_provider_from_remote normalizes provider names
@@ -959,6 +985,11 @@ mod tests {
959985
get_provider_from_remote("https://source.developers.google.com/p/project/r/repo"),
960986
"google"
961987
);
988+
// Test edge case with trailing dot in hostname
989+
assert_eq!(
990+
get_provider_from_remote("https://github.com./user/repo"),
991+
"github"
992+
);
962993
}
963994

964995
#[test]

0 commit comments

Comments
 (0)