Skip to content

Commit

Permalink
sort
Browse files Browse the repository at this point in the history
  • Loading branch information
fwilhe committed Sep 12, 2024
1 parent bc3d322 commit 290d3b8
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 43 deletions.
6 changes: 5 additions & 1 deletion api-examples/Get CVEs by Gardenlinux Version.bru
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ meta {
}

get {
url: http://{{hostname}}:{{port}}/v1/cves/gardenlinux/1592.0
url: http://{{hostname}}:{{port}}/v1/cves/gardenlinux/1592.0?sortBy=cveId
body: none
auth: none
}

params:query {
sortBy: cveId
}
7 changes: 6 additions & 1 deletion api-examples/List Packages in Distro.bru
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@ meta {
}

get {
url: http://{{hostname}}:{{port}}/v1/packages/distro/gardenlinux/1592.0
url: http://{{hostname}}:{{port}}/v1/packages/distro/gardenlinux/1592.0?sortBy=sourcePackageName&sortOrder=ASC
body: none
auth: none
}

params:query {
sortBy: sourcePackageName
sortOrder: ASC
}
6 changes: 5 additions & 1 deletion api-examples/List Sorted.bru
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ meta {
}

get {
url: http://{{hostname}}:{{port}}/v1/packages/distro/gardenlinux/1592.0
url: http://{{hostname}}:{{port}}/v1/foo?sortBy=isVulnerable
body: none
auth: none
}

params:query {
sortBy: isVulnerable
}
27 changes: 18 additions & 9 deletions src/main/java/io/gardenlinux/glvd/GlvdController.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@ public GlvdController(@Nonnull GlvdService glvdService) {
@GetMapping("/cves/{distro}/{gardenlinuxVersion}")
ResponseEntity<List<SourcePackageCve>> getCveDistro(
@PathVariable final String gardenlinuxVersion,
@RequestParam(defaultValue = "cveId") final String sortBy
@RequestParam(defaultValue = "cveId") final String sortBy,
@RequestParam(defaultValue = "ASC") final String sortOrder
) {
return ResponseEntity.ok().body(glvdService.getCveForDistribution(gardenlinuxVersion, sortBy));
return ResponseEntity.ok().body(glvdService.getCveForDistribution(gardenlinuxVersion, sortBy, sortOrder));
}

@GetMapping("/cves/{distro}/{gardenlinuxVersion}/packages/{packageList}")
Expand All @@ -36,14 +37,21 @@ ResponseEntity<List<SourcePackageCve>> getCvePackages(
}

@GetMapping("/packages/distro/{distro}/{gardenlinuxVersion}")
ResponseEntity<List<SourcePackage>> packagesForDistro(@PathVariable final String gardenlinuxVersion) {
return ResponseEntity.ok(glvdService.getPackagesForDistro(gardenlinuxVersion));
ResponseEntity<List<SourcePackage>> packagesForDistro(
@PathVariable final String gardenlinuxVersion,
@RequestParam(defaultValue = "sourcePackageName") final String sortBy,
@RequestParam(defaultValue = "ASC") final String sortOrder
) {
return ResponseEntity.ok(glvdService.getPackagesForDistro(gardenlinuxVersion, sortBy, sortOrder));
}

@GetMapping("/packages/{sourcePackage}")
ResponseEntity<List<SourcePackageCve>> packageWithVulnerabilities(@PathVariable final String sourcePackage,
@RequestParam(defaultValue = "cveId") final String sortBy) {
return ResponseEntity.ok(glvdService.getPackageWithVulnerabilities(sourcePackage, sortBy));
ResponseEntity<List<SourcePackageCve>> packageWithVulnerabilities(
@PathVariable final String sourcePackage,
@RequestParam(defaultValue = "cveId") final String sortBy,
@RequestParam(defaultValue = "ASC") final String sortOrder
) {
return ResponseEntity.ok(glvdService.getPackageWithVulnerabilities(sourcePackage, sortBy, sortOrder));
}

@GetMapping("/packages/{sourcePackage}/{sourcePackageVersion}")
Expand All @@ -59,9 +67,10 @@ ResponseEntity<List<SourcePackageCve>> packageWithVulnerabilitiesByVersion(
ResponseEntity<List<SourcePackageCve>> packagesByVulnerability(
@PathVariable final String gardenlinuxVersion,
@PathVariable final String cveId,
@RequestParam(defaultValue = "cveId") final String sortBy
@RequestParam(defaultValue = "cveId") final String sortBy,
@RequestParam(defaultValue = "ASC") final String sortOrder
) {
return ResponseEntity.ok(glvdService.getPackagesByVulnerability(gardenlinuxVersion, cveId, sortBy));
return ResponseEntity.ok(glvdService.getPackagesByVulnerability(gardenlinuxVersion, cveId, sortBy, sortOrder));
}

}
21 changes: 9 additions & 12 deletions src/main/java/io/gardenlinux/glvd/GlvdService.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,31 +21,28 @@ public GlvdService(@Nonnull SourcePackageCveRepository sourcePackageCveRepositor
this.sourcePackageRepository = sourcePackageRepository;
}

public List<SourcePackageCve> getCveForDistribution(String gardenlinuxVersion, String sortBy) {
return sourcePackageCveRepository.findByGardenlinuxVersion(gardenlinuxVersion, Sort.by(sortBy));
public List<SourcePackageCve> getCveForDistribution(String gardenlinuxVersion, String sortBy, String sortOrder) {
return sourcePackageCveRepository.findByGardenlinuxVersion(gardenlinuxVersion, Sort.by(Sort.Direction.valueOf(sortOrder), sortBy));
}

public List<SourcePackageCve> getCveForPackages(String gardenlinuxVersion, String packages) {
return sourcePackageCveRepository.findBySourcePackageNameInAndGardenlinuxVersion("{"+packages+"}", gardenlinuxVersion);
}

public List<SourcePackage> getPackagesForDistro(String gardenlinuxVersion) {
return sourcePackageRepository.findByGardenlinuxVersion(gardenlinuxVersion);
public List<SourcePackage> getPackagesForDistro(String gardenlinuxVersion, String sortBy, String sortOrder) {
return sourcePackageRepository.findByGardenlinuxVersion(gardenlinuxVersion, Sort.by(Sort.Direction.valueOf(sortOrder), sortBy));
}

public List<SourcePackageCve> getPackageWithVulnerabilities(String sourcePackage, String sortBy) {
return sourcePackageCveRepository.findBySourcePackageName(sourcePackage, Sort.by(sortBy));
public List<SourcePackageCve> getPackageWithVulnerabilities(String sourcePackage, String sortBy, String sortOrder) {
return sourcePackageCveRepository.findBySourcePackageName(sourcePackage, Sort.by(Sort.Direction.valueOf(sortOrder), sortBy));
}

public List<SourcePackageCve> getPackageWithVulnerabilitiesByVersion(String sourcePackage, String sourcePackageVersion, String sortBy) {
return sourcePackageCveRepository.findBySourcePackageNameAndSourcePackageVersion(sourcePackage, sourcePackageVersion, Sort.by(sortBy));
return sourcePackageCveRepository.findBySourcePackageNameAndSourcePackageVersion(sourcePackage, sourcePackageVersion, Sort.by(Sort.Direction.DESC, sortBy));
}

public List<SourcePackageCve> getPackagesByVulnerability(String gardenlinuxVersion, String cveId, String sortBy) {
return sourcePackageCveRepository.findByCveIdAndGardenlinuxVersion(cveId, gardenlinuxVersion, Sort.by(sortBy));
public List<SourcePackageCve> getPackagesByVulnerability(String gardenlinuxVersion, String cveId, String sortBy, String sortOrder) {
return sourcePackageCveRepository.findByCveIdAndGardenlinuxVersion(cveId, gardenlinuxVersion, Sort.by(Sort.Direction.valueOf(sortOrder), sortBy));
}

public List<SourcePackageCve> getSorted(String sortBy) {
return sourcePackageCveRepository.findAll(Sort.by(sortBy));
}
}
10 changes: 7 additions & 3 deletions src/main/java/io/gardenlinux/glvd/UiController.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ public UiController(@Nonnull GlvdService glvdService) {
@GetMapping("/getPackagesForDistro")
public String getPackagesForDistro(
@RequestParam(name = "gardenlinuxVersion", required = true) String gardenlinuxVersion,
@RequestParam(defaultValue = "cveId") final String sortBy,
@RequestParam(defaultValue = "ASC") final String sortOrder,
Model model) {
var packages = glvdService.getPackagesForDistro(gardenlinuxVersion);
var packages = glvdService.getPackagesForDistro(gardenlinuxVersion, sortBy, sortOrder);
model.addAttribute("packages", packages);
model.addAttribute("gardenlinuxVersion", gardenlinuxVersion);
return "getPackagesForDistro";
Expand All @@ -30,9 +32,10 @@ public String getPackagesForDistro(
public String getCveForDistribution(
@RequestParam(name = "gardenlinuxVersion", required = true) String gardenlinuxVersion,
@RequestParam(defaultValue = "cveId") final String sortBy,
@RequestParam(defaultValue = "ASC") final String sortOrder,
Model model
) {
var sourcePackageCves = glvdService.getCveForDistribution(gardenlinuxVersion, sortBy);
var sourcePackageCves = glvdService.getCveForDistribution(gardenlinuxVersion, sortBy, sortOrder);
model.addAttribute("sourcePackageCves", sourcePackageCves);
model.addAttribute("gardenlinuxVersion", gardenlinuxVersion);
return "getCveForDistribution";
Expand All @@ -56,9 +59,10 @@ public String getPackagesByVulnerability(
@RequestParam(name = "gardenlinuxVersion", required = true) String gardenlinuxVersion,
@RequestParam(name = "cveId", required = true) String cveId,
@RequestParam(defaultValue = "cveId") final String sortBy,
@RequestParam(defaultValue = "ASC") final String sortOrder,
Model model
) {
var sourcePackageCves = glvdService.getPackagesByVulnerability(gardenlinuxVersion, cveId, sortBy);
var sourcePackageCves = glvdService.getPackagesByVulnerability(gardenlinuxVersion, cveId, sortBy, sortOrder);
model.addAttribute("sourcePackageCves", sourcePackageCves);
model.addAttribute("gardenlinuxVersion", gardenlinuxVersion);
model.addAttribute("cveId", cveId);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.gardenlinux.glvd.db;

import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
Expand All @@ -17,20 +18,9 @@ public interface SourcePackageCveRepository extends JpaRepository<SourcePackageC

// would be nice if we did not need a native query here
// is this possible in any other way?
@Query(value = "select * from sourcepackagecve where source_package_name = ANY(:source_package_names ::TEXT[]) AND gardenlinux_version = :gardenlinux_version", nativeQuery = true)
List<SourcePackageCve> findBySourcePackageNameInAndGardenlinuxVersion(@Param("source_package_names") String source_package_names, @Param("gardenlinux_version") String gardenlinux_version);

@Query(value = """
SELECT
debsrc.deb_source AS source_package_name
FROM
dist_cpe
INNER JOIN debsrc ON
(debsrc.dist_id = dist_cpe.id)
WHERE
dist_cpe.cpe_product = :distro
AND dist_cpe.cpe_version = :distroVersion
ORDER BY
debsrc.deb_source""", nativeQuery = true)
List<String> packagesForDistribution(@Param("distro") String distro, @Param("distroVersion") String distroVersion);
SELECT * FROM sourcepackagecve
WHERE source_package_name = ANY(:source_package_names ::TEXT[]) AND gardenlinux_version = :gardenlinux_version
""", nativeQuery = true)
List<SourcePackageCve> findBySourcePackageNameInAndGardenlinuxVersion(@Param("source_package_names") String source_package_names, @Param("gardenlinux_version") String gardenlinux_version);
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package io.gardenlinux.glvd.db;

import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.repository.query.Param;

import java.util.List;

public interface SourcePackageRepository extends JpaRepository<SourcePackage, String> {
List<SourcePackage> findByGardenlinuxVersion(@Param("gardenlinux_version") String gardenlinux_version);
List<SourcePackage> findByGardenlinuxVersion(@Param("gardenlinux_version") String gardenlinux_version, Sort by);
}

0 comments on commit 290d3b8

Please sign in to comment.