Skip to content

Commit

Permalink
remove provider specific repository icons
Browse files Browse the repository at this point in the history
  • Loading branch information
syphar committed Dec 16, 2023
1 parent 2f67be0 commit 41431c6
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 84 deletions.
1 change: 0 additions & 1 deletion src/db/add_package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,6 @@ pub(crate) async fn add_package_into_database(
&metadata_pkg.name,
&metadata_pkg.version,
&metadata_pkg.version,
None,
)
.await
.context("error when fetching crate-details")?
Expand Down
33 changes: 0 additions & 33 deletions src/repositories/updater.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,16 +250,6 @@ impl RepositoryStatsUpdater {
Ok(())
}

pub fn get_icon_name(&self, host: &str) -> &'static str {
for updater in &self.updaters {
if updater.host() == host {
return updater.icon();
}
}
// The default icon in case it doesn't match any of the "known" ones.
"code-branch"
}

async fn store_repository(
&self,
conn: &mut sqlx::PgConnection,
Expand Down Expand Up @@ -346,7 +336,6 @@ pub struct RepositoryName<'a> {
#[cfg(test)]
mod test {
use super::*;
use crate::context::Context;

#[test]
fn test_repository_name() {
Expand Down Expand Up @@ -423,26 +412,4 @@ mod test {
// Unknown host
assert_name("https://git.sr.ht/~ireas/merge-rs", None);
}

#[test]
fn test_icon_name() {
crate::test::wrapper(|env| {
let mut config = env.base_config();
config.github_accesstoken = Some("qsjdnfqdq".to_owned());
let updater = RepositoryStatsUpdater::new(&config, env.pool()?);

assert_eq!(updater.get_icon_name(""), "code-branch");
assert_eq!(updater.get_icon_name("random"), "code-branch");
assert_eq!(updater.get_icon_name("github"), "code-branch");
assert_eq!(updater.get_icon_name("github.com"), "github");
assert_eq!(updater.get_icon_name("gitlab"), "code-branch");
assert_eq!(updater.get_icon_name("gitlab.com"), "gitlab");
assert_eq!(updater.get_icon_name("gitlab.freedesktop.org"), "gitlab");
assert_eq!(
updater.get_icon_name("a.gitlab.freedesktop.org"),
"code-branch"
);
Ok(())
});
}
}
41 changes: 15 additions & 26 deletions src/web/crate_details.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use crate::web::axum_cached_redirect;
use crate::web::rustdoc::RustdocHtmlParams;
use crate::{
impl_axum_webpage,
repositories::RepositoryStatsUpdater,
storage::PathNotFoundError,
web::{
cache::CachePolicy,
Expand Down Expand Up @@ -74,7 +73,6 @@ struct RepositoryMetadata {
forks: i32,
issues: i32,
name: Option<String>,
icon: &'static str,
}

fn optional_markdown<S>(markdown: &Option<String>, serializer: S) -> Result<S::Ok, S::Error>
Expand Down Expand Up @@ -104,7 +102,6 @@ impl CrateDetails {
name: &str,
version: &str,
version_or_latest: &str,
up: Option<&RepositoryStatsUpdater>,
) -> Result<Option<CrateDetails>, anyhow::Error> {
let krate = match sqlx::query!(
r#"SELECT
Expand Down Expand Up @@ -168,12 +165,11 @@ impl CrateDetails {
// get releases, sorted by semver
let releases = releases_for_crate(conn, krate.crate_id).await?;

let repository_metadata = krate.repo_host.map(|host| RepositoryMetadata {
let repository_metadata = krate.repo_host.map(|_| RepositoryMetadata {
issues: krate.repo_issues.unwrap(),
stars: krate.repo_stars.unwrap(),
forks: krate.repo_forks.unwrap(),
name: krate.repo_name,
icon: up.map_or("code-branch", |u| u.get_icon_name(&host)),
});

let metadata = MetaData {
Expand Down Expand Up @@ -386,7 +382,6 @@ pub(crate) async fn crate_details_handler(
Path(params): Path<CrateDetailHandlerParams>,
Extension(storage): Extension<Arc<AsyncStorage>>,
mut conn: DbConnection,
Extension(repository_stats_updater): Extension<Arc<RepositoryStatsUpdater>>,
) -> AxumResult<AxumResponse> {
// this handler must always called with a crate name
if params.version.is_none() {
Expand All @@ -413,15 +408,9 @@ pub(crate) async fn crate_details_handler(
}
};

let mut details = CrateDetails::new(
&mut conn,
&params.name,
&version,
&version_or_latest,
Some(&repository_stats_updater),
)
.await?
.ok_or(AxumNope::VersionNotFound)?;
let mut details = CrateDetails::new(&mut conn, &params.name, &version, &version_or_latest)
.await?
.ok_or(AxumNope::VersionNotFound)?;

match details.fetch_readme(&storage).await {
Ok(readme) => details.readme = readme.or(details.readme),
Expand Down Expand Up @@ -712,7 +701,7 @@ mod tests {
expected_last_successful_build: Option<&str>,
) -> Result<(), Error> {
let mut conn = db.async_conn().await;
let details = CrateDetails::new(&mut conn, package, version, version, None)
let details = CrateDetails::new(&mut conn, package, version, version)
.await
.with_context(|| anyhow::anyhow!("could not fetch crate details"))?
.unwrap();
Expand Down Expand Up @@ -880,7 +869,7 @@ mod tests {

let details = env.runtime().block_on(async move {
let mut conn = db.async_conn().await;
CrateDetails::new(&mut conn, "foo", "0.2.0", "0.2.0", None)
CrateDetails::new(&mut conn, "foo", "0.2.0", "0.2.0")
.await
.unwrap()
.unwrap()
Expand Down Expand Up @@ -1001,7 +990,7 @@ mod tests {
for version in &["0.0.1", "0.0.2", "0.0.3"] {
let details = env.runtime().block_on(async move {
let mut conn = db.async_conn().await;
CrateDetails::new(&mut conn, "foo", version, version, None)
CrateDetails::new(&mut conn, "foo", version, version)
.await
.unwrap()
.unwrap()
Expand Down Expand Up @@ -1031,7 +1020,7 @@ mod tests {
for version in &["0.0.1", "0.0.2", "0.0.3-pre.1"] {
let details = env.runtime().block_on(async move {
let mut conn = db.async_conn().await;
CrateDetails::new(&mut conn, "foo", version, version, None)
CrateDetails::new(&mut conn, "foo", version, version)
.await
.unwrap()
.unwrap()
Expand Down Expand Up @@ -1062,7 +1051,7 @@ mod tests {
for version in &["0.0.1", "0.0.2", "0.0.3"] {
let details = env.runtime().block_on(async move {
let mut conn = db.async_conn().await;
CrateDetails::new(&mut conn, "foo", version, version, None)
CrateDetails::new(&mut conn, "foo", version, version)
.await
.unwrap()
.unwrap()
Expand Down Expand Up @@ -1101,7 +1090,7 @@ mod tests {
for version in &["0.0.1", "0.0.2", "0.0.3"] {
let details = env.runtime().block_on(async move {
let mut conn = db.async_conn().await;
CrateDetails::new(&mut conn, "foo", version, version, None)
CrateDetails::new(&mut conn, "foo", version, version)
.await
.unwrap()
.unwrap()
Expand Down Expand Up @@ -1161,7 +1150,7 @@ mod tests {

let details = env.runtime().block_on(async move {
let mut conn = db.async_conn().await;
CrateDetails::new(&mut conn, "foo", "0.0.1", "0.0.1", None)
CrateDetails::new(&mut conn, "foo", "0.0.1", "0.0.1")
.await
.unwrap()
.unwrap()
Expand All @@ -1187,7 +1176,7 @@ mod tests {

let details = env.runtime().block_on(async move {
let mut conn = db.async_conn().await;
CrateDetails::new(&mut conn, "foo", "0.0.1", "0.0.1", None)
CrateDetails::new(&mut conn, "foo", "0.0.1", "0.0.1")
.await
.unwrap()
.unwrap()
Expand All @@ -1214,7 +1203,7 @@ mod tests {

let details = env.runtime().block_on(async move {
let mut conn = db.async_conn().await;
CrateDetails::new(&mut conn, "foo", "0.0.1", "0.0.1", None)
CrateDetails::new(&mut conn, "foo", "0.0.1", "0.0.1")
.await
.unwrap()
.unwrap()
Expand All @@ -1236,7 +1225,7 @@ mod tests {

let details = env.runtime().block_on(async move {
let mut conn = db.async_conn().await;
CrateDetails::new(&mut conn, "foo", "0.0.1", "0.0.1", None)
CrateDetails::new(&mut conn, "foo", "0.0.1", "0.0.1")
.await
.unwrap()
.unwrap()
Expand Down Expand Up @@ -1625,7 +1614,7 @@ mod tests {

let details = env.runtime().block_on(async move {
let mut conn = env.async_db().await.async_conn().await;
CrateDetails::new(&mut conn, "dummy", "0.5.0", "0.5.0", None)
CrateDetails::new(&mut conn, "dummy", "0.5.0", "0.5.0")
.await
.unwrap()
.unwrap()
Expand Down
1 change: 0 additions & 1 deletion src/web/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,6 @@ fn apply_middleware(
.layer(Extension(context.config()?))
.layer(Extension(context.storage()?))
.layer(Extension(async_storage))
.layer(Extension(context.repository_stats_updater()?))
.layer(option_layer(template_data.map(Extension)))
.layer(middleware::from_fn(csp::csp_middleware))
.layer(option_layer(has_templates.then_some(middleware::from_fn(
Expand Down
29 changes: 7 additions & 22 deletions src/web/rustdoc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

use crate::{
db::Pool,
repositories::RepositoryStatsUpdater,
storage::rustdoc_archive_path,
utils,
web::{
Expand Down Expand Up @@ -175,7 +174,7 @@ pub(crate) async fn rustdoc_redirector_handler(
if target.ends_with(".js") {
// this URL is actually from a crate-internal path, serve it there instead
rendering_time.step("serve JS for crate");
let krate = CrateDetails::new(&mut conn, &crate_name, &version, &version, None)
let krate = CrateDetails::new(&mut conn, &crate_name, &version, &version)
.await?
.ok_or(AxumNope::ResourceNotFound)?;

Expand Down Expand Up @@ -362,7 +361,6 @@ pub(crate) async fn rustdoc_html_server_handler(
Extension(storage): Extension<Arc<AsyncStorage>>,
Extension(config): Extension<Arc<Config>>,
Extension(csp): Extension<Arc<Csp>>,
Extension(updater): Extension<Arc<RepositoryStatsUpdater>>,
uri: Uri,
) -> AxumResult<AxumResponse> {
let mut rendering_time = RenderingTimesRecorder::new(&metrics.rustdoc_rendering_times);
Expand Down Expand Up @@ -444,15 +442,9 @@ pub(crate) async fn rustdoc_html_server_handler(

// Get the crate's details from the database
// NOTE: we know this crate must exist because we just checked it above (or else `match_version` is buggy)
let krate = CrateDetails::new(
&mut conn,
&params.name,
&version,
&version_or_latest,
Some(&updater),
)
.await?
.ok_or(AxumNope::ResourceNotFound)?;
let krate = CrateDetails::new(&mut conn, &params.name, &version, &version_or_latest)
.await?
.ok_or(AxumNope::ResourceNotFound)?;

if !krate.rustdoc_status {
rendering_time.step("redirect to crate");
Expand Down Expand Up @@ -748,7 +740,6 @@ pub(crate) async fn target_redirect_handler(
Path((name, version, req_path)): Path<(String, String, String)>,
mut conn: DbConnection,
Extension(storage): Extension<Arc<AsyncStorage>>,
Extension(updater): Extension<Arc<RepositoryStatsUpdater>>,
) -> AxumResult<impl IntoResponse> {
let release_found = match_version(&mut conn, &name, Some(&version)).await?;
let (version, version_or_latest, is_latest_url) = match release_found.version {
Expand All @@ -758,15 +749,9 @@ pub(crate) async fn target_redirect_handler(
MatchSemver::Semver(_) => return Err(AxumNope::VersionNotFound),
};

let crate_details = CrateDetails::new(
&mut conn,
&name,
&version,
&version_or_latest,
Some(&updater),
)
.await?
.ok_or(AxumNope::VersionNotFound)?;
let crate_details = CrateDetails::new(&mut conn, &name, &version, &version_or_latest)
.await?
.ok_or(AxumNope::VersionNotFound)?;

// We're trying to find the storage location
// for the requested path in the target-redirect.
Expand Down
2 changes: 1 addition & 1 deletion templates/crate/details.html
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
{# If the repo link is for github or gitlab, show some stats #}
{# TODO: add support for hosts besides github and gitlab (#35) #}
{%- if details.repository_metadata -%}
{{ details.repository_metadata.icon | fab }}
{{ "code-branch" | fab }}
{% if details.repository_metadata.name %}
{{details.repository_metadata.name}}
{% else %}
Expand Down

0 comments on commit 41431c6

Please sign in to comment.