@@ -775,24 +775,24 @@ export async function listPackagesForScatter(
775775export async function getAdvisoriesByPackageId (
776776 qx : QueryExecutor ,
777777 packageId : string ,
778- opts ?: { page : number ; pageSize : number } ,
778+ opts ?: { page : number ; pageSize : number ; version ?: string ; severities ?: string [ ] } ,
779779) : Promise < { rows : AdvisoryRow [ ] ; total : number } > {
780780 const cte = `
781781 WITH advisory_data AS (
782782 SELECT
783783 a.osv_id AS "osvId",
784784 LOWER(a.severity) AS severity,
785785 CASE
786- WHEN p.latest_version IS NULL THEN NULL
786+ WHEN COALESCE($(version), p.latest_version) IS NULL THEN NULL
787787 WHEN COUNT(ar.id) = 0 THEN NULL
788788 -- TODO: text comparison is lexicographic, not semver — '1.9.0' >= '1.10.0' is TRUE here.
789789 -- Replace with a proper semver comparison function when one is available in the DB.
790790 WHEN BOOL_AND(
791791 CASE
792792 WHEN ar.fixed_version IS NULL AND ar.last_affected IS NULL THEN FALSE
793- WHEN ar.fixed_version IS NOT NULL AND p.latest_version >= ar.fixed_version THEN TRUE
793+ WHEN ar.fixed_version IS NOT NULL AND COALESCE($(version), p.latest_version) >= ar.fixed_version THEN TRUE
794794 WHEN ar.fixed_version IS NOT NULL THEN FALSE
795- WHEN ar.last_affected IS NOT NULL AND p.latest_version > ar.last_affected THEN TRUE
795+ WHEN ar.last_affected IS NOT NULL AND COALESCE($(version), p.latest_version) > ar.last_affected THEN TRUE
796796 ELSE FALSE
797797 END
798798 ) THEN 'patched'
@@ -807,25 +807,37 @@ export async function getAdvisoriesByPackageId(
807807 )
808808 `
809809
810+ const severityClause = opts ?. severities ?. length
811+ ? `WHERE severity = ANY($(severities)::text[])`
812+ : ''
810813 const paginationClause = opts ? `LIMIT $(limit) OFFSET $(offset)` : ''
814+ const params = {
815+ packageId,
816+ version : opts ?. version ?? null ,
817+ severities : opts ?. severities ?? null ,
818+ limit : opts ?. pageSize ,
819+ offset : opts ? ( opts . page - 1 ) * opts . pageSize : 0 ,
820+ }
811821
812822 const rows = ( await qx . select (
813823 `${ cte } SELECT * FROM advisory_data
824+ ${ severityClause }
814825 ORDER BY
815826 CASE severity WHEN 'critical' THEN 1 WHEN 'high' THEN 2 WHEN 'moderate' THEN 3 WHEN 'low' THEN 4 ELSE 5 END,
816827 CASE resolution WHEN 'open' THEN 1 WHEN 'patched' THEN 2 ELSE 3 END,
817828 "osvId"
818829 ${ paginationClause } ` ,
819- { packageId , limit : opts ?. pageSize , offset : opts ? ( opts . page - 1 ) * opts . pageSize : 0 } ,
830+ params ,
820831 ) ) as AdvisoryRow [ ]
821832
822833 if ( ! opts ) {
823834 return { rows, total : rows . length }
824835 }
825836
826- const countResult = ( await qx . selectOne ( `${ cte } SELECT COUNT(*) AS total FROM advisory_data` , {
827- packageId,
828- } ) ) as { total : string }
837+ const countResult = ( await qx . selectOne (
838+ `${ cte } SELECT COUNT(*) AS total FROM advisory_data ${ severityClause } ` ,
839+ params ,
840+ ) ) as { total : string }
829841
830842 return { rows, total : Number ( countResult . total ) }
831843}
0 commit comments