-
Notifications
You must be signed in to change notification settings - Fork 29
API Endpoint: Get Package Count by Ecosystem #1638
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,6 +5,7 @@ import ( | |
| "net/url" | ||
| "strings" | ||
|
|
||
| "github.com/l3montree-dev/devguard/config" | ||
| "github.com/l3montree-dev/devguard/database/models" | ||
| "github.com/l3montree-dev/devguard/dtos" | ||
| "github.com/l3montree-dev/devguard/normalize" | ||
|
|
@@ -16,14 +17,16 @@ import ( | |
| ) | ||
|
|
||
| type VulnDBController struct { | ||
| cveRepository shared.CveRepository | ||
| maliciousPackageChecker shared.MaliciousPackageChecker | ||
| cveRepository shared.CveRepository | ||
| maliciousPackageChecker shared.MaliciousPackageChecker | ||
| affectedComponentRepository shared.AffectedComponentRepository | ||
| } | ||
|
|
||
| func NewVulnDBController(cveRepository shared.CveRepository, maliciousPackageChecker shared.MaliciousPackageChecker) *VulnDBController { | ||
| func NewVulnDBController(cveRepository shared.CveRepository, maliciousPackageChecker shared.MaliciousPackageChecker, affectedComponentRepository shared.AffectedComponentRepository) *VulnDBController { | ||
| return &VulnDBController{ | ||
| cveRepository: cveRepository, | ||
| maliciousPackageChecker: maliciousPackageChecker, | ||
| cveRepository: cveRepository, | ||
| maliciousPackageChecker: maliciousPackageChecker, | ||
| affectedComponentRepository: affectedComponentRepository, | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -150,3 +153,47 @@ func (c VulnDBController) PURLInspect(ctx shared.Context) error { | |
| MaliciousPackage: maliciousPackage, | ||
| }) | ||
| } | ||
|
|
||
| type ecosystemRow struct { | ||
| Ecosystem string `gorm:"ecosystem" json:"ecosystem"` | ||
| Count int `gorm:"count" json:"count"` | ||
| } | ||
|
|
||
| // return the number of affected packages by ecosystem | ||
| func (c VulnDBController) GetEcosystemDistribution(ctx shared.Context) error { | ||
| results := make([]ecosystemRow, 1024) | ||
|
Hubtrick-Git marked this conversation as resolved.
|
||
|
|
||
| // static sql to get amount of packages by ecosystem | ||
| sql := `SELECT ecosystem, COUNT(*) FROM affected_components GROUP BY ecosystem;` | ||
| err := c.affectedComponentRepository.GetDB(nil).Raw(sql).Find(&results).Error | ||
|
Comment on lines
+167
to
+168
|
||
| if err != nil { | ||
| return err | ||
|
Hubtrick-Git marked this conversation as resolved.
Outdated
|
||
| } | ||
|
|
||
| // since ecosystem have tags behind the : character we want to group them by their prefix | ||
| jsonResults := buildResultsJSON(results) | ||
|
|
||
| return ctx.String(200, jsonResults) | ||
| } | ||
|
Hubtrick-Git marked this conversation as resolved.
|
||
|
|
||
| // group ecosystem by prefix ecosystem string and return the equivalent json encoding | ||
| func buildResultsJSON(rows []ecosystemRow) string { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just marshal the map 🙂 |
||
| // map to deduplicate ecosystem with different tags | ||
| aggregatedResults := make(map[string]int) | ||
|
|
||
| // fill the map with the value of the rows | ||
| for _, row := range rows { | ||
| before, _, _ := strings.Cut(row.Ecosystem, ":") | ||
| aggregatedResults[before] += row.Count | ||
| } | ||
|
|
||
| // build the json encoding | ||
| jsonString := "{\n" | ||
| for ecosystem, count := range aggregatedResults { | ||
| jsonString += fmt.Sprintf("%s\"%s\": %d,\n", config.PrettyJSONIndent, ecosystem, count) | ||
| } | ||
|
|
||
| jsonString, _ = strings.CutSuffix(jsonString, ",\n") | ||
| jsonString += "\n}" | ||
| return jsonString | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.