-
Notifications
You must be signed in to change notification settings - Fork 0
chore(go-packages):SP-4143 add support for golang packages #9
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
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| DROP TABLE IF EXISTS golang_projects; | ||
| CREATE TABLE golang_projects | ||
| ( | ||
| component text not null, | ||
| version text not null, | ||
| version_id integer not null, | ||
| version_date text not null, | ||
| is_module boolean, | ||
| is_package boolean, | ||
| license text not null, | ||
| license_id integer not null, | ||
| has_valid_go_mod_file boolean, | ||
| has_redistributable_license boolean, | ||
| has_tagged_version boolean, | ||
| has_stable_version boolean, | ||
| repository text not null, | ||
| is_indexed boolean, | ||
| purl_name text not null, | ||
| mine_id integer not null, | ||
| index_timestamp text not null, | ||
| primary key (purl_name, version) | ||
| ); | ||
|
|
||
| INSERT INTO golang_projects (component, version, version_id, version_date, license, license_id, repository, purl_name, mine_id, index_timestamp, is_indexed) VALUES ('github.com/scanoss/papi', 'v0.0.1', 5958021, '', 'MIT', 5614, 'github.com/scanoss/papi', 'github.com/scanoss/papi', 45, '2022-02-21T19:51:21.112979Z', True); | ||
| INSERT INTO golang_projects (component, version, version_id, version_date, license, license_id, repository, purl_name, mine_id, index_timestamp, is_indexed) VALUES ('google.golang.org/grpc', 'v1.19.0', 5193086, '', 'Apache-2.0', 552, 'github.com/grpc/grpc-go', 'google.golang.org/grpc', 45, '2022-05-09T20:17:02.339878Z', True); | ||
| INSERT INTO golang_projects (component, version, version_id, version_date, license, license_id, repository, purl_name, mine_id, index_timestamp, is_indexed) VALUES ('google.golang.org/grpc', 'v1.7.0', 11640350, '', '', 9999, 'github.com/grpc/grpc-go', 'google.golang.org/grpc', 45, '2023-11-24T20:17:02.339878Z', True); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,10 +20,12 @@ import ( | |
| "context" | ||
| "errors" | ||
| "fmt" | ||
| "strings" | ||
|
|
||
| "github.com/Masterminds/semver/v3" | ||
| "github.com/grpc-ecosystem/go-grpc-middleware/logging/zap/ctxzap" | ||
| "github.com/jmoiron/sqlx" | ||
| "github.com/scanoss/go-models/pkg/helpers" | ||
| purlutils "github.com/scanoss/go-purl-helper/pkg" | ||
| ) | ||
|
|
||
| // AllUrlsModel provides database access for URL information. | ||
|
|
@@ -51,6 +53,20 @@ func NewAllURLModel(db *sqlx.DB) *AllUrlsModel { | |
| } | ||
| } | ||
|
|
||
| // semverTogglePrefix returns the alternate version string by toggling the "v" prefix. | ||
| func semverTogglePrefix(version string) string { | ||
| if len(version) == 0 { | ||
| return version | ||
| } | ||
| if _, err := semver.NewVersion(version); err == nil { | ||
| if version[0] != 'v' { | ||
| return "v" + version | ||
| } | ||
| return strings.TrimLeft(version, "v") | ||
| } | ||
| return version | ||
| } | ||
|
|
||
| // GetURLsByPurlNameType retrieves all component URLs matching the specified PURL name and type. | ||
| func (m *AllUrlsModel) GetURLsByPurlNameType(ctx context.Context, purlName, purlType string) ([]AllURL, error) { | ||
| s := ctxzap.Extract(ctx).Sugar() | ||
|
|
@@ -99,7 +115,7 @@ func (m *AllUrlsModel) GetURLsByPurlNameTypeVersion(ctx context.Context, purlNam | |
| s.Error("Please specify a valid Purl Version to query") | ||
| return nil, errors.New("please specify a valid Purl Version to query") | ||
| } | ||
| semverV := helpers.SemverTogglePrefix(purlVersion) | ||
| semverV := semverTogglePrefix(purlVersion) | ||
|
|
||
| // This query is same as GetURLsByPurlNameType but adds a WHERE clause for versions | ||
| query := "SELECT component, v.version_name AS version, v.semver AS semver," + | ||
|
|
@@ -120,3 +136,66 @@ func (m *AllUrlsModel) GetURLsByPurlNameTypeVersion(ctx context.Context, purlNam | |
| s.Debugf("Found %v results for %v, %v, %v.", len(allUrls), purlType, purlName, purlVersion) | ||
| return allUrls, nil | ||
| } | ||
|
|
||
| // PickOneUrl takes the potential matching component/versions and selects the most appropriate one. | ||
| // | ||
|
|
||
| func PickOneUrl(ctx context.Context, allUrls []AllURL, purlName, purlType, purlReq string) (AllURL, error) { | ||
| s := ctxzap.Extract(ctx).Sugar() | ||
|
|
||
| if len(allUrls) == 0 { | ||
| s.Infof("No component match (in urls) found for %v, %v", purlName, purlType) | ||
| return AllURL{}, nil | ||
| } | ||
|
|
||
| var c *semver.Constraints | ||
| if len(purlReq) > 0 { | ||
| s.Debugf("Building version constraint for %v: %v", purlName, purlReq) | ||
| var err error | ||
| c, err = semver.NewConstraint(purlReq) | ||
| if err != nil { | ||
| s.Warnf("Encountered an issue parsing version constraint string '%v' (%v,%v): %v", purlReq, purlName, purlType, err) | ||
| } | ||
|
Comment on lines
+151
to
+158
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
rg -n -C2 '\bPickOneUrl\s*\('Repository: scanoss/go-models Length of output: 2047 🏁 Script executed: cat -n pkg/models/all_urls.go | sed -n '143,200p'Repository: scanoss/go-models Length of output: 2301 🏁 Script executed: cat -n pkg/models/golang_projects.go | sed -n '115,125p'Repository: scanoss/go-models Length of output: 557 🏁 Script executed: cat -n pkg/models/golang_projects.go | sed -n '170,180p'Repository: scanoss/go-models Length of output: 466 🏁 Script executed: cat -n pkg/models/golang_projects.go | sed -n '95,120p'Repository: scanoss/go-models Length of output: 1377 🏁 Script executed: cat -n pkg/models/golang_projects.go | sed -n '139,176p'Repository: scanoss/go-models Length of output: 2047 🏁 Script executed: cat -n pkg/models/golang_projects.go | sed -n '80,95p'Repository: scanoss/go-models Length of output: 1028 🏁 Script executed: cat -n pkg/models/golang_projects.go | sed -n '122,130p'Repository: scanoss/go-models Length of output: 635 Return error when version constraint parsing fails. If Proposed fix c, err = semver.NewConstraint(purlReq)
if err != nil {
s.Warnf("Encountered an issue parsing version constraint string '%v' (%v,%v): %v", purlReq, purlName, purlType, err)
+ return AllURL{}, fmt.Errorf("invalid version constraint %q: %w", purlReq, err)
}🤖 Prompt for AI Agents |
||
| } | ||
|
|
||
| zeroVersion, _ := semver.NewVersion("v0.0.0") | ||
| var bestVersion *semver.Version | ||
| var bestURL AllURL | ||
|
|
||
| s.Debugf("Checking versions...") | ||
| for _, url := range allUrls { | ||
| if len(url.SemVer) == 0 && len(url.Version) == 0 { | ||
| s.Infof("Skipping match as it doesn't have a version: %#v", url) | ||
| continue | ||
| } | ||
|
|
||
| v, err := semver.NewVersion(url.Version) | ||
| if err != nil && len(url.SemVer) > 0 { | ||
| s.Debugf("Failed to parse SemVer: '%v'. Trying Version instead: %v (%v)", url.Version, url.SemVer, err) | ||
| v, err = semver.NewVersion(url.SemVer) | ||
| } | ||
| if err != nil { | ||
| s.Warnf("Encountered an issue parsing version string '%v' (%v) for %v: %v. Using v0.0.0", url.Version, url.SemVer, url, err) | ||
| v = zeroVersion | ||
| } | ||
|
|
||
| if c != nil && !c.Check(v) { | ||
| continue | ||
| } | ||
|
|
||
| if bestVersion == nil || v.GreaterThan(bestVersion) { | ||
| bestVersion = v | ||
| bestURL = url | ||
| } | ||
| } | ||
|
|
||
| if bestVersion == nil { // TODO should we return the latest version anyway? | ||
| s.Warnf("No component match found for %v, %v after filter %v", purlName, purlType, purlReq) | ||
| return AllURL{}, nil | ||
| } | ||
|
|
||
| s.Debugf("Selected highest version: %v", bestVersion) | ||
| bestURL.URL, _ = purlutils.ProjectUrl(purlName, purlType) | ||
| s.Debugf("Selected version: %#v", bestURL) | ||
| return bestURL, nil | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Keep
0.7.0under[Unreleased]until release day.This entry is dated March 11, 2026, but the PR is still open on March 10, 2026. Pre-dating the section makes the changelog read as if
0.7.0has already shipped.🤖 Prompt for AI Agents