Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Gitlab custom domains (self-hosted) repo model. #213

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 19 additions & 6 deletions src/models/repo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,11 @@ impl fmt::Display for RepoPath {
}
}

#[allow(clippy::similar_names)]
#[derive(Clone, Debug, Hash, PartialEq, Eq)]
pub enum RepoSite {
Github,
Gitlab,
Gitlab(Option<GiteaDomain>),
Bitbucket,
Sourcehut,
Codeberg,
Expand All @@ -63,7 +64,8 @@ impl RepoSite {
pub fn to_base_uri(&self) -> &str {
match self {
RepoSite::Github => "https://github.com",
RepoSite::Gitlab => "https://gitlab.com",
RepoSite::Gitlab(None) => "https://gitlab.com",
RepoSite::Gitlab(Some(domain)) => domain.as_ref(),
RepoSite::Bitbucket => "https://bitbucket.org",
RepoSite::Sourcehut => "https://git.sr.ht",
RepoSite::Codeberg => "https://codeberg.org",
Expand All @@ -74,7 +76,8 @@ impl RepoSite {
pub fn to_usercontent_base_uri(&self) -> &str {
match self {
RepoSite::Github => "https://raw.githubusercontent.com",
RepoSite::Gitlab => "https://gitlab.com",
RepoSite::Gitlab(None) => "https://gitlab.com",
RepoSite::Gitlab(Some(domain)) => domain.as_ref(),
RepoSite::Bitbucket => "https://bitbucket.org",
RepoSite::Sourcehut => "https://git.sr.ht",
RepoSite::Codeberg => "https://codeberg.org",
Expand All @@ -85,7 +88,7 @@ impl RepoSite {
pub fn to_usercontent_repo_suffix(&self) -> &'static str {
match self {
RepoSite::Github => "HEAD",
RepoSite::Gitlab | RepoSite::Bitbucket => "raw/HEAD",
RepoSite::Gitlab(_) | RepoSite::Bitbucket => "raw/HEAD",
RepoSite::Sourcehut => "blob/HEAD",
RepoSite::Codeberg | RepoSite::Gitea(_) => "raw",
}
Expand All @@ -99,12 +102,13 @@ impl FromStr for RepoSite {
if let Some((site, domain)) = input.split_once('/') {
match site {
"gitea" => Ok(RepoSite::Gitea(domain.parse()?)),
"gitlab" => Ok(RepoSite::Gitlab(Some(domain.parse()?))),
_ => Err(anyhow!("unknown repo site identifier")),
}
} else {
match input {
"github" => Ok(RepoSite::Github),
"gitlab" => Ok(RepoSite::Gitlab),
"gitlab" => Ok(RepoSite::Gitlab(None)),
"bitbucket" => Ok(RepoSite::Bitbucket),
"sourcehut" => Ok(RepoSite::Sourcehut),
"codeberg" => Ok(RepoSite::Codeberg),
Expand All @@ -118,7 +122,8 @@ impl fmt::Display for RepoSite {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
RepoSite::Github => write!(f, "github"),
RepoSite::Gitlab => write!(f, "gitlab"),
RepoSite::Gitlab(None) => write!(f, "gitlab"),
RepoSite::Gitlab(Some(s)) => write!(f, "gitlab/{s}"),
RepoSite::Bitbucket => write!(f, "bitbucket"),
RepoSite::Sourcehut => write!(f, "sourcehut"),
RepoSite::Codeberg => write!(f, "codeberg"),
Expand Down Expand Up @@ -267,5 +272,13 @@ mod tests {
let exp = format!("https://example.com/git/deps-rs/deps.rs/raw/{expected}");
assert_eq!(out.to_string(), exp);
}

for (input, expected) in &paths {
let repo = RepoPath::from_parts("gitlab/gitlab.com", "deps-rs", "deps.rs").unwrap();
let out = repo.to_usercontent_file_url(RelativePath::new(input));

let exp = format!("https://gitlab.com/deps-rs/deps.rs/raw/HEAD/{expected}");
assert_eq!(out.to_string(), exp);
}
}
}
2 changes: 1 addition & 1 deletion src/server/views/html/status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ fn dependency_table(title: &str, deps: &IndexMap<CrateName, AnalyzedDependency>)
fn get_site_icon(site: &RepoSite) -> (FaType, &'static str) {
match *site {
RepoSite::Github => (FaType::Brands, "github"),
RepoSite::Gitlab => (FaType::Brands, "gitlab"),
RepoSite::Gitlab(_) => (FaType::Brands, "gitlab"),
RepoSite::Bitbucket => (FaType::Brands, "bitbucket"),
// FIXME: There is no brands/{sourcehut, codeberg, gitea} icon, so just use an
// icon which looks close enough.
Expand Down