-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
181 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,12 @@ | ||
// SPDX-FileCopyrightText: Copyright 2024 Dmitry Marakasov <[email protected]> | ||
// SPDX-License-Identifier: GPL-3.0-or-later | ||
|
||
pub mod latest_versions; | ||
pub mod tiny_repos; | ||
pub mod version_for_repo; | ||
pub mod vertical_allrepos; | ||
|
||
pub use latest_versions::*; | ||
pub use tiny_repos::*; | ||
pub use version_for_repo::*; | ||
pub use vertical_allrepos::*; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
// SPDX-FileCopyrightText: Copyright 2024 Dmitry Marakasov <[email protected]> | ||
// SPDX-License-Identifier: GPL-3.0-or-later | ||
|
||
use std::borrow::Cow; | ||
|
||
use axum::extract::{Path, Query, State}; | ||
use axum::http::{header, HeaderValue, StatusCode}; | ||
use axum::response::IntoResponse; | ||
use itertools::Itertools; | ||
use serde::Deserialize; | ||
use sqlx::FromRow; | ||
|
||
use libversion::AsVersionWithFlags; | ||
|
||
use repology_common::{PackageFlags, PackageStatus}; | ||
|
||
use crate::badges::{render_generic_badge, Cell}; | ||
use crate::package::traits::{PackageWithFlags, PackageWithStatus, PackageWithVersion}; | ||
use crate::package::version::package_version; | ||
use crate::result::EndpointResult; | ||
use crate::state::AppState; | ||
|
||
#[derive(Deserialize)] | ||
pub struct QueryParams { | ||
#[serde(rename = "header")] | ||
pub caption: Option<String>, | ||
} | ||
|
||
#[derive(FromRow)] | ||
pub struct Package { | ||
pub version: String, | ||
pub status: PackageStatus, | ||
pub flags: i32, | ||
} | ||
|
||
impl PackageWithVersion for Package { | ||
fn version(&self) -> &str { | ||
&self.version | ||
} | ||
} | ||
impl PackageWithFlags for Package { | ||
fn flags(&self) -> PackageFlags { | ||
PackageFlags::from_bits(self.flags as u32).expect("flags must be deserializable") | ||
} | ||
} | ||
impl PackageWithStatus for Package { | ||
fn status(&self) -> PackageStatus { | ||
self.status | ||
} | ||
} | ||
|
||
pub async fn badge_latest_versions( | ||
Path(project_name): Path<String>, | ||
State(state): State<AppState>, | ||
Query(query): Query<QueryParams>, | ||
) -> EndpointResult { | ||
let project_name = if let Some(project_name) = project_name.strip_suffix(".svg") { | ||
project_name | ||
} else { | ||
return Ok((StatusCode::NOT_FOUND, "path must end with .svg".to_owned()).into_response()); | ||
}; | ||
|
||
let packages: Vec<Package> = sqlx::query_as( | ||
r#" | ||
SELECT | ||
version, | ||
versionclass AS status, | ||
flags | ||
FROM packages | ||
WHERE effname = $1 AND versionclass IN (1, 4, 5) -- Newest, Unique, Devel | ||
"#, | ||
) | ||
.bind(project_name) | ||
.fetch_all(&state.pool) | ||
.await?; | ||
|
||
let versions = packages | ||
.iter() | ||
.map(|package| package_version(package)) | ||
.sorted_by(|a, b| { | ||
// version desc → version string length desc → version string lexographical | ||
a.cmp(b) | ||
.then_with(|| a.version().len().cmp(&b.version().len())) | ||
.reverse() | ||
.then_with(|| a.version().cmp(&b.version())) | ||
}) | ||
.dedup_by(|a, b| a.version() == b.version()) | ||
.collect::<Vec<_>>(); | ||
|
||
let (default_caption, text) = match versions.len() { | ||
0 => ("latest packaged version", Cow::from("-")), | ||
1 => ("latest packaged version", Cow::from(versions[0].version())), | ||
_ => ( | ||
"latest packaged versions", | ||
Cow::from(versions.iter().map(|version| version.version()).join(", ")), | ||
), | ||
}; | ||
|
||
let caption_cell = Cell::new( | ||
query | ||
.caption | ||
.as_ref() | ||
.map_or(default_caption, String::as_str), | ||
) | ||
.collapsible(true); | ||
let version_cell = Cell::new(&text).color("#007ec6"); | ||
|
||
let body = render_generic_badge( | ||
&[vec![caption_cell, version_cell]], | ||
None, | ||
0, | ||
&state.font_measurer, | ||
)?; | ||
|
||
Ok(( | ||
[( | ||
header::CONTENT_TYPE, | ||
HeaderValue::from_static(mime::IMAGE_SVG.as_ref()), | ||
)], | ||
body, | ||
) | ||
.into_response()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
INSERT INTO packages(effname, version, versionclass, flags, repo, family, trackname, visiblename, projectname_seed, origversion, rawversion, shadow) VALUES | ||
('zsh', '3.0', 5, 0, '', '', '', '', '', '', '', false), | ||
('zsh', '1_0_0', 1, 0, '', '', '', '', '', '', '', false), | ||
('zsh', '1.0.0', 1, 0, '', '', '', '', '', '', '', false), | ||
('zsh', '1.0', 1, 0, '', '', '', '', '', '', '', false), | ||
('zsh', '2024', 2, 1 << 8, '', '', '', '', '', '', '', false), -- sink | ||
('zsh', '2025', 3, 0, '', '', '', '', '', '', '', false); -- ignored |