Skip to content

Commit

Permalink
fix(webserver): use full name for repositories and strip .git from ur…
Browse files Browse the repository at this point in the history
…ls (#1978)

* fix(webserver): use full name for repositories and strip .git from urls

* Strip .git suffix from canonicalized urls too

* Add test case for stripping .git suffix
  • Loading branch information
boxbeam authored Apr 26, 2024
1 parent 4ab1959 commit 66ac5d2
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 6 deletions.
8 changes: 7 additions & 1 deletion crates/tabby-common/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ impl RepositoryConfig {
}

pub fn canonicalize_url(url: &str) -> String {
let url = url.strip_suffix(".git").unwrap_or(url);
url::Url::parse(url)
.map(|mut url| {
let _ = url.set_password(None);
Expand Down Expand Up @@ -171,7 +172,7 @@ mod tests {
let repo = RepositoryConfig {
git_url: "https://github.com/TabbyML/tabby.git".to_owned(),
};
assert!(repo.dir().ends_with("https_github.com_TabbyML_tabby.git"));
assert!(repo.dir().ends_with("https_github.com_TabbyML_tabby"));
}

#[test]
Expand Down Expand Up @@ -200,5 +201,10 @@ mod tests {
RepositoryConfig::canonicalize_url("https://github.com/TabbyML/tabby"),
"https://github.com/TabbyML/tabby"
);

assert_eq!(
RepositoryConfig::canonicalize_url("https://github.com/TabbyML/tabby.git"),
"https://github.com/TabbyML/tabby"
);
}
}
2 changes: 1 addition & 1 deletion crates/tabby/src/services/code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ fn closest_match<'a>(
.into_iter()
.filter(|elem| GitUrl::parse(&elem.git_url).is_ok_and(|x| x.name == git_search.name))
// If there're multiple matches, we pick the one with highest alphabetical order
.min_by_key(|elem| elem.git_url.as_str())
.min_by_key(|elem| elem.canonical_git_url())
.map(|x| x.git_url.as_str())
}

Expand Down
8 changes: 7 additions & 1 deletion ee/tabby-webserver/src/cron/github.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,15 @@ async fn refresh_repositories_for_provider(
.strip_prefix("git://")
.map(|url| format!("https://{url}"))
.unwrap_or(url);
let url = url.strip_suffix(".git").unwrap_or(&url);

service
.upsert_github_provided_repository(provider.id.clone(), id, repo.name, url)
.upsert_github_provided_repository(
provider.id.clone(),
id,
repo.full_name.unwrap_or(repo.name),
url.to_string(),
)
.await?;
}

Expand Down
8 changes: 5 additions & 3 deletions ee/tabby-webserver/src/cron/gitlab.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,15 @@ async fn refresh_repositories_for_provider(
};
for repo in repos {
let id = repo.id.to_string();
let url = repo.http_url_to_repo;
let url = url.strip_suffix(".git").unwrap_or(&url);

service
.upsert_gitlab_provided_repository(
provider.id.clone(),
id,
repo.name,
repo.http_url_to_repo,
repo.name_with_namespace,
url.to_string(),
)
.await?;
}
Expand All @@ -71,7 +73,7 @@ async fn refresh_repositories_for_provider(
#[derive(Deserialize)]
struct Repository {
id: u128,
name: String,
name_with_namespace: String,
http_url_to_repo: String,
}

Expand Down

0 comments on commit 66ac5d2

Please sign in to comment.