Skip to content

Commit 0e315d6

Browse files
committed
use unit type for implementing Display on Vec<String> for transaction IDs
1 parent 80f4c79 commit 0e315d6

File tree

5 files changed

+38
-14
lines changed

5 files changed

+38
-14
lines changed

scenarios/full-20250323.json5

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@
88
"sbom_by_package": "pkg:maven/io.quarkus/quarkus-smallrye-metrics@2.13.8.Final-redhat-00004?type=jar&repository_url=https%3a%2f%2fmaven.repository.redhat.com%2fga%2f",
99
"sbom_license_ids": "urn:uuid:0195baea-42e3-7842-a0e3-4c7874263954",
1010
"analyze_purl": "pkg:rpm/redhat/eap7-activemq-artemis-native@1.0.2-3.redhat_00004.1.el7eap?arch=noarch&epoch=1",
11-
"get_recommendations": "pkg:rpm/redhat/eap7-activemq-artemis-native@1.0.2"
11+
"get_recommendations": ["pkg:rpm/redhat/eap7-activemq-artemis-native@1.0.2, pkg:maven/io.quarkus/quarkus-smallrye-metrics@2.13.8"]
1212
}

scenarios/full-20250604.json5

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@
99
"sbom_license_ids": "urn:uuid:01973124-d4ab-7163-b104-331632a21144",
1010
"analyze_purl": "pkg:rpm/redhat/eap7-activemq-artemis-native@1.0.2-3.redhat_00004.1.el7eap?arch=noarch&epoch=1",
1111
"get_purl_details": "b00df2ca-df21-5c10-8874-304e9c54e2bd",
12-
"get_recommendations": "pkg:rpm/redhat/eap7-activemq-artemis-native@1.0.2"
12+
"get_recommendations": ["pkg:rpm/redhat/eap7-activemq-artemis-native@1.0.2, pkg:maven/io.quarkus/quarkus-smallrye-metrics@2.13.8"]
1313
}

src/restapi.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ use goose::goose::{GooseUser, TransactionResult};
22
use serde_json::json;
33
use urlencoding::encode;
44

5+
use crate::utils::DisplayVec;
6+
57
pub async fn list_advisory(user: &mut GooseUser) -> TransactionResult {
68
let _response = user.get("/api/v2/advisory").await?;
79

@@ -203,12 +205,15 @@ pub async fn search_purls_by_license(user: &mut GooseUser) -> TransactionResult
203205
Ok(())
204206
}
205207

206-
pub async fn get_recommendations(purl: String, user: &mut GooseUser) -> TransactionResult {
208+
pub async fn get_recommendations(
209+
purls: DisplayVec<String>,
210+
user: &mut GooseUser,
211+
) -> TransactionResult {
207212
let _response = user
208213
.post_json(
209214
"/api/v2/purl/recommend",
210215
&json!({
211-
"purls": [purl]
216+
"purls": purls
212217
}),
213218
)
214219
.await?;

src/scenario/mod.rs

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
mod purl;
22

3-
use crate::scenario::purl::CanonicalPurl;
3+
use crate::{scenario::purl::CanonicalPurl, utils::DisplayVec};
44
use anyhow::{Context, anyhow};
55
use serde_json::Value;
66
use sqlx::{Executor, Row, postgres::PgRow};
@@ -92,8 +92,7 @@ pub(crate) struct Scenario {
9292
#[serde(with = "required")]
9393
pub get_purl_details: Option<String>,
9494

95-
#[serde(with = "required")]
96-
pub get_recommendations: Option<String>,
95+
pub get_recommendations: Option<DisplayVec<String>>,
9796
}
9897

9998
impl Scenario {
@@ -164,6 +163,12 @@ impl Loader {
164163
.ok_or_else(|| anyhow!("nothing found"))
165164
}
166165

166+
async fn find_rows(&self, sql: &str) -> anyhow::Result<Vec<PgRow>> {
167+
let mut db = crate::db::connect(&self.db).await?;
168+
169+
Ok(db.fetch_all(sql).await?)
170+
}
171+
167172
/// get the SHA256 of the largest SBOM (by number of packages)
168173
pub async fn large_sbom(&self) -> anyhow::Result<(String, String)> {
169174
// get the largest SBOM in the database
@@ -283,23 +288,27 @@ LIMIT 1;
283288
}
284289

285290
// A purl whose version matches redhat-[0-9]+$ regex
286-
pub async fn purl_with_recommendations(&self) -> anyhow::Result<String> {
287-
self.find_row(
291+
pub async fn purl_with_recommendations(&self) -> anyhow::Result<DisplayVec<String>> {
292+
self.find_rows(
288293
r#"
289294
SELECT
290295
purl AS result
291296
FROM
292297
qualified_purl
293298
WHERE
294299
purl->>'version' ~ 'redhat-[0-9]+$'
295-
LIMIT 1;
300+
LIMIT 10;
296301
"#,
297302
)
298303
.await
299-
.and_then(|row| {
300-
let value: Value = row.try_get("result")?;
301-
let purl: CanonicalPurl = serde_json::from_value(value)?;
302-
Ok::<String, anyhow::Error>(purl.to_string())
304+
.and_then(|rows| {
305+
let mut result = vec![];
306+
for row in rows {
307+
let value: Value = row.try_get("result")?;
308+
let purl: CanonicalPurl = serde_json::from_value(value)?;
309+
result.push(purl.to_string());
310+
}
311+
Ok(DisplayVec(result))
303312
})
304313
}
305314
}

src/utils.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,16 @@ pub fn truncate_middle(s: impl Display, max_len: usize) -> String {
1313
}
1414
}
1515

16+
#[derive(Clone, Debug, PartialEq, Eq, serde::Deserialize, serde::Serialize)]
17+
pub struct DisplayVec<T>(pub Vec<T>);
18+
19+
impl<T: std::fmt::Display> std::fmt::Display for DisplayVec<T> {
20+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
21+
let strs: Vec<String> = self.0.iter().map(|item| item.to_string()).collect();
22+
write!(f, "{}", strs.join(","))
23+
}
24+
}
25+
1626
#[cfg(test)]
1727
mod test {
1828
use super::*;

0 commit comments

Comments
 (0)