Skip to content

Commit bbb69bd

Browse files
committed
feat: adding gc endpoint
1 parent 89bf882 commit bbb69bd

File tree

3 files changed

+51
-1
lines changed

3 files changed

+51
-1
lines changed

modules/fundamental/src/purl/endpoints/mod.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use crate::{
99
use actix_web::{HttpResponse, Responder, get, web};
1010
use sea_orm::prelude::Uuid;
1111
use std::str::FromStr;
12-
use trustify_auth::{ReadSbom, authorizer::Require};
12+
use trustify_auth::{DeleteSbom, ReadSbom, authorizer::Require};
1313
use trustify_common::{
1414
db::Database, db::query::Query, id::IdError, model::Paginated, model::PaginatedResults,
1515
purl::Purl,
@@ -25,6 +25,7 @@ pub fn configure(config: &mut utoipa_actix_web::service_config::ServiceConfig, d
2525
.app_data(web::Data::new(purl_service))
2626
.service(base::get_base_purl)
2727
.service(base::all_base_purls)
28+
.service(gc)
2829
.service(get)
2930
.service(all);
3031
}
@@ -81,5 +82,22 @@ pub async fn all(
8182
Ok(HttpResponse::Ok().json(service.purls(search, paginated, db.as_ref()).await?))
8283
}
8384

85+
#[utoipa::path(
86+
operation_id = "garbageCollect",
87+
tag = "purl",
88+
responses(
89+
(status = 200, description = "Performs garbage collection for orphaned packages", body = String),
90+
),
91+
)]
92+
#[get("/v2/purl/gc")]
93+
pub async fn gc(
94+
service: web::Data<PurlService>,
95+
db: web::Data<Database>,
96+
_: Require<DeleteSbom>,
97+
) -> actix_web::Result<impl Responder> {
98+
let result = service.gc_purls(db.as_ref()).await?;
99+
Ok(HttpResponse::Ok().body(result.to_string()))
100+
}
101+
84102
#[cfg(test)]
85103
mod test;

modules/fundamental/src/purl/endpoints/test.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use crate::purl::model::details::base_purl::BasePurlDetails;
22
use crate::purl::model::summary::base_purl::BasePurlSummary;
33
use crate::purl::model::summary::purl::PurlSummary;
44
use crate::test::caller;
5+
use actix_web::http::StatusCode;
56
use actix_web::test::TestRequest;
67
use serde_json::{Value, json};
78
use std::str::FromStr;
@@ -310,3 +311,22 @@ async fn test_purl_license_details(ctx: &TrustifyContext) -> Result<(), anyhow::
310311
assert!(expected_result.contains_subset(response.clone()));
311312
Ok(())
312313
}
314+
315+
#[test_context(TrustifyContext)]
316+
#[test(actix_web::test)]
317+
async fn garbage_collect(ctx: &TrustifyContext) -> Result<(), anyhow::Error> {
318+
setup(&ctx.db, &ctx.graph).await?;
319+
let app = caller(ctx).await?;
320+
321+
let uri = "/api/v2/purl/gc";
322+
let request = TestRequest::get().uri(uri).to_request();
323+
let response = app.call_service(request).await;
324+
325+
assert_eq!(response.status(), StatusCode::OK);
326+
327+
let body_bytes = actix_web::test::read_body(response).await;
328+
let body_str = std::str::from_utf8(&body_bytes)?;
329+
assert_eq!(body_str, "8");
330+
331+
Ok(())
332+
}

openapi.yaml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1976,6 +1976,18 @@ paths:
19761976
application/json:
19771977
schema:
19781978
$ref: '#/components/schemas/BasePurlDetails'
1979+
/api/v2/purl/gc:
1980+
get:
1981+
tags:
1982+
- purl
1983+
operationId: garbageCollect
1984+
responses:
1985+
'200':
1986+
description: Performs garbage collection for orphaned packages
1987+
content:
1988+
text/plain:
1989+
schema:
1990+
type: string
19791991
/api/v2/purl/{key}:
19801992
get:
19811993
tags:

0 commit comments

Comments
 (0)