-
Notifications
You must be signed in to change notification settings - Fork 1
chore:SP-3888 Implement multithreading support for the local vulnerab… #30
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 all commits
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 | ||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -30,8 +30,7 @@ import ( | |||||||||||||||||||||
| ) | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| type VersionModel struct { | ||||||||||||||||||||||
| ctx context.Context | ||||||||||||||||||||||
| conn *sqlx.Conn | ||||||||||||||||||||||
| db *sqlx.DB | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| type Version struct { | ||||||||||||||||||||||
|
|
@@ -48,18 +47,18 @@ type PurlVersion struct { | |||||||||||||||||||||
| // TODO add cache for versions already searched for? | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| // NewVersionModel creates a new instance of the Version Model. | ||||||||||||||||||||||
| func NewVersionModel(ctx context.Context, conn *sqlx.Conn) *VersionModel { | ||||||||||||||||||||||
| return &VersionModel{ctx: ctx, conn: conn} | ||||||||||||||||||||||
| func NewVersionModel(db *sqlx.DB) *VersionModel { | ||||||||||||||||||||||
| return &VersionModel{db: db} | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| // GetVersionByName gets the given version from the versions table. | ||||||||||||||||||||||
| func (m *VersionModel) GetVersionByName(name string, create bool) (Version, error) { | ||||||||||||||||||||||
| func (m *VersionModel) GetVersionByName(ctx context.Context, name string, create bool) (Version, error) { | ||||||||||||||||||||||
| if len(name) == 0 { | ||||||||||||||||||||||
| zlog.S.Error("Please specify a valid Version Name to query") | ||||||||||||||||||||||
| return Version{}, errors.New("please specify a valid Version Name to query") | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| var version Version | ||||||||||||||||||||||
| err := m.conn.QueryRowxContext(m.ctx, | ||||||||||||||||||||||
| err := m.db.QueryRowxContext(ctx, | ||||||||||||||||||||||
| "SELECT id, version_name, semver FROM versions"+ | ||||||||||||||||||||||
| " WHERE version_name = $1", | ||||||||||||||||||||||
| name).StructScan(&version) | ||||||||||||||||||||||
|
|
@@ -68,28 +67,28 @@ func (m *VersionModel) GetVersionByName(name string, create bool) (Version, erro | |||||||||||||||||||||
| return Version{}, fmt.Errorf("failed to query the versions table: %v", err) | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| if create && len(version.VersionName) == 0 { // No version found and requested to create an entry | ||||||||||||||||||||||
| return m.saveVersion(name) | ||||||||||||||||||||||
| return m.saveVersion(ctx, name) | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| return version, nil | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| // saveVersion writes the given version name to the versions table. | ||||||||||||||||||||||
| func (m *VersionModel) saveVersion(name string) (Version, error) { | ||||||||||||||||||||||
| func (m *VersionModel) saveVersion(ctx context.Context, name string) (Version, error) { | ||||||||||||||||||||||
| if len(name) == 0 { | ||||||||||||||||||||||
| zlog.S.Error("Please specify a valid version Name to save") | ||||||||||||||||||||||
| return Version{}, errors.New("please specify a valid Version Name to save") | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| zlog.S.Debugf("Attempting to save '%v' to the versions table...", name) | ||||||||||||||||||||||
| var version Version | ||||||||||||||||||||||
| err := m.conn.QueryRowxContext(m.ctx, | ||||||||||||||||||||||
| err := m.db.QueryRowxContext(ctx, | ||||||||||||||||||||||
| "INSERT INTO versions (version_name, semver) VALUES($1, $2)"+ | ||||||||||||||||||||||
| " RETURNING id, version_name, semver", | ||||||||||||||||||||||
| name, "", false, false, | ||||||||||||||||||||||
| ).StructScan(&version) | ||||||||||||||||||||||
|
Comment on lines
+84
to
88
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. 🧩 Analysis chain🏁 Script executed: #!/bin/bash
# First, read the actual file content around the specified lines
cat -n pkg/models/versions.go | sed -n '80,95p'Repository: scanoss/vulnerabilities Length of output: 795 🏁 Script executed: #!/bin/bash
# Search for other INSERT statements into versions table to understand schema
rg -n "INSERT INTO versions" --type=go -B 2 -A 3Repository: scanoss/vulnerabilities Length of output: 434 🏁 Script executed: #!/bin/bash
# Search for versions table schema definition (likely in migrations or schema files)
fd -type f \( -name "*.sql" -o -name "*migration*" -o -name "*schema*" \) | head -20Repository: scanoss/vulnerabilities Length of output: 237 🏁 Script executed: #!/bin/bash
# Search for Version struct definition
rg -n "type Version struct" --type=go -A 10Repository: scanoss/vulnerabilities Length of output: 575 🏁 Script executed: #!/bin/bash
# Search for other methods that interact with versions table
rg -n "versions" pkg/models/versions.go | head -20Repository: scanoss/vulnerabilities Length of output: 783 Fix parameter count mismatch in SQL statement. The INSERT statement has 2 placeholders ( Fix err := m.db.QueryRowxContext(ctx,
"INSERT INTO versions (version_name, semver) VALUES($1, $2)"+
" RETURNING id, version_name, semver",
- name, "", false, false,
+ name, "",
).StructScan(&version)📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||
| zlog.S.Errorf("Error: Failed to insert new version name into versions table for %v: %v", name, err) | ||||||||||||||||||||||
| return m.GetVersionByName(name, false) // Search one more time for it, just in case someone else added it | ||||||||||||||||||||||
| return m.GetVersionByName(ctx, name, false) // Search one more time for it, just in case someone else added it | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| return version, nil | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -50,7 +50,7 @@ func TestGetVulnsByPurl(t *testing.T) { | |
| t.Fatalf("failed to load SQL test data: %v", err) | ||
| } | ||
|
|
||
| cpeModel := NewVulnsForPurlModel(ctx, conn) | ||
| cpeModel := NewVulnsForPurlModel(db) | ||
|
|
||
| type inputGetVulnsForPurl struct { | ||
| purl string | ||
|
|
@@ -75,7 +75,7 @@ func TestGetVulnsByPurl(t *testing.T) { | |
| } | ||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| got, err := cpeModel.GetVulnsByPurl(tt.input.purl, tt.input.requirement) | ||
| got, err := cpeModel.GetVulnsByPurl(ctx, tt.input.purl, tt.input.requirement) | ||
| if (err != nil) != tt.wantErr { | ||
| t.Errorf("cpeModel.GetCpeByPurl() error = %v, wantErr %v", err, tt.wantErr) | ||
| return | ||
|
|
@@ -102,26 +102,14 @@ func TestGetVulnsByPurlName(t *testing.T) { | |
| } | ||
| db.SetMaxOpenConns(1) | ||
| defer CloseDB(db) | ||
|
|
||
| conn, err := db.Connx(ctx) // Get a connection from the pool | ||
| if err != nil { | ||
| t.Fatalf("an error '%s' was not expected when opening a stub database connection", err) | ||
| } | ||
| defer CloseConn(conn) | ||
| err = LoadTestSQLData(db, ctx, conn) | ||
| err = LoadTestSQLData(db, ctx, nil) | ||
| if err != nil { | ||
| t.Fatalf("failed to load SQL test data: %v", err) | ||
| } | ||
|
|
||
| cpeModel := NewVulnsForPurlModel(ctx, conn) | ||
|
|
||
| _, err = cpeModel.GetVulnsByPurlName("") | ||
| if err == nil { | ||
| t.Errorf("Error was expected because purl is empty in cpeModel.GetVulnsByPurlName()") | ||
| } | ||
| cpeModel := NewVulnsForPurlModel(db) | ||
|
|
||
| CloseConn(conn) | ||
| _, err = cpeModel.GetVulnsByPurlName("pkg:github/hapijs/call") | ||
| _, err = cpeModel.GetVulnsByPurlName(ctx, "") | ||
| if err == nil { | ||
| t.Errorf("Error was expected because purl is empty in cpeModel.GetVulnsByPurlName()") | ||
| } | ||
|
|
@@ -151,15 +139,15 @@ func TestGetVulnsByPurlVersion(t *testing.T) { | |
| t.Fatalf("failed to load SQL test data: %v", err) | ||
| } | ||
|
|
||
| cpeModel := NewVulnsForPurlModel(ctx, conn) | ||
| cpeModel := NewVulnsForPurlModel(db) | ||
|
|
||
| _, err = cpeModel.GetVulnsByPurlVersion("", "") | ||
| _, err = cpeModel.GetVulnsByPurlVersion(ctx, "", "") | ||
| if err == nil { | ||
| t.Errorf("Error was expected because purl is empty in cpeModel.GetVulnsByPurlVersion()") | ||
| } | ||
|
|
||
| CloseConn(conn) | ||
| _, err = cpeModel.GetVulnsByPurlVersion("pkg:github/hapijs/call", "1.0.0") | ||
| _, err = cpeModel.GetVulnsByPurlVersion(ctx, "pkg:github/hapijs/call", "1.0.0") | ||
| if err == nil { | ||
| t.Errorf("Error was expected because purl is empty in cpeModel.GetVulnsByPurlVersion()") | ||
| } | ||
|
Comment on lines
149
to
153
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. Intentional connection close tests error handling. The test intentionally closes the connection at line 149 before calling The test logic may need adjustment since:
Consider either:
🤖 Prompt for AI Agents |
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.