|
1 | 1 | mod purl; |
2 | 2 |
|
3 | | -use crate::scenario::purl::CanonicalPurl; |
| 3 | +use crate::{scenario::purl::CanonicalPurl, utils::DisplayVec}; |
4 | 4 | use anyhow::{Context, anyhow}; |
5 | 5 | use serde_json::Value; |
6 | 6 | use sqlx::{Executor, Row, postgres::PgRow}; |
@@ -92,8 +92,7 @@ pub(crate) struct Scenario { |
92 | 92 | #[serde(with = "required")] |
93 | 93 | pub get_purl_details: Option<String>, |
94 | 94 |
|
95 | | - #[serde(with = "required")] |
96 | | - pub get_recommendations: Option<String>, |
| 95 | + pub get_recommendations: Option<DisplayVec<String>>, |
97 | 96 | } |
98 | 97 |
|
99 | 98 | impl Scenario { |
@@ -164,6 +163,12 @@ impl Loader { |
164 | 163 | .ok_or_else(|| anyhow!("nothing found")) |
165 | 164 | } |
166 | 165 |
|
| 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 | + |
167 | 172 | /// get the SHA256 of the largest SBOM (by number of packages) |
168 | 173 | pub async fn large_sbom(&self) -> anyhow::Result<(String, String)> { |
169 | 174 | // get the largest SBOM in the database |
@@ -283,23 +288,27 @@ LIMIT 1; |
283 | 288 | } |
284 | 289 |
|
285 | 290 | // 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( |
288 | 293 | r#" |
289 | 294 | SELECT |
290 | 295 | purl AS result |
291 | 296 | FROM |
292 | 297 | qualified_purl |
293 | 298 | WHERE |
294 | 299 | purl->>'version' ~ 'redhat-[0-9]+$' |
295 | | -LIMIT 1; |
| 300 | +LIMIT 10; |
296 | 301 | "#, |
297 | 302 | ) |
298 | 303 | .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)) |
303 | 312 | }) |
304 | 313 | } |
305 | 314 | } |
|
0 commit comments