Skip to content
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

fix(swift): try to use branch to resolve version #6168

Merged
merged 4 commits into from
Feb 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions pkg/dependency/parser/swift/swift/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ import (
"strings"

"github.com/liamg/jfather"
"github.com/samber/lo"
"golang.org/x/xerrors"

dio "github.com/aquasecurity/trivy/pkg/dependency/parser/io"
"github.com/aquasecurity/trivy/pkg/dependency/parser/log"
"github.com/aquasecurity/trivy/pkg/dependency/parser/types"
"github.com/aquasecurity/trivy/pkg/dependency/parser/utils"
)
Expand Down Expand Up @@ -37,10 +39,21 @@ func (Parser) Parse(r dio.ReadSeekerAt) ([]types.Library, []types.Dependency, er
}
for _, pin := range pins {
name := libraryName(pin, lockFile.Version)

// Skip packages for which we cannot resolve the version
if pin.State.Version == "" && pin.State.Branch == "" {
log.Logger.Warnf("Unable to resolve %q. Both the version and branch fields are empty.", name)
continue
}

// A Pin can be resolved using `branch` without `version`.
// e.g. https://github.com/element-hq/element-ios/blob/6a9bcc88ea37147efba8f0a7bcf3ec187f4a4011/Riot.xcworkspace/xcshareddata/swiftpm/Package.resolved#L84-L92
version := lo.Ternary(pin.State.Version != "", pin.State.Version, pin.State.Branch)

libs = append(libs, types.Library{
ID: utils.PackageID(name, pin.State.Version),
ID: utils.PackageID(name, version),
Name: name,
Version: pin.State.Version,
Version: version,
Locations: []types.Location{
{
StartLine: pin.StartLine,
Expand Down
12 changes: 6 additions & 6 deletions pkg/dependency/parser/swift/swift/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,6 @@ func TestParser_Parse(t *testing.T) {
},
},
},
// docker run -it --rm swift@sha256:45e5e44ed4873063795f150182437f4dbe7d5527ba5655979d7d11e0829179a7
// mkdir app && cd app
// swift package init
// ## add new deps: ##
// sed -i 's/],/],\ndependencies: [\n.package(url: "https:\/\/github.com\/ReactiveCocoa\/ReactiveSwift", from: "7.0.0"),\n.package(url: "https:\/\/github.com\/Quick\/Quick.git", from: "7.0.0"),\n.package(url: "https:\/\/github.com\/Quick\/Nimble.git", .exact("9.2.1")),\n],/' Package.swift
// swift package update
{
name: "happy path v2",
inputFile: "testdata/happy-v2-Package.resolved",
Expand All @@ -65,6 +59,12 @@ func TestParser_Parse(t *testing.T) {
Version: "7.1.1",
Locations: []types.Location{{StartLine: 39, EndLine: 47}},
},
{
ID: "github.com/element-hq/swift-ogg@0.0.1",
Name: "github.com/element-hq/swift-ogg",
Version: "0.0.1",
Locations: []types.Location{{StartLine: 48, EndLine: 56}},
},
{
ID: "github.com/mattgallagher/CwlCatchException@2.1.2",
Name: "github.com/mattgallagher/CwlCatchException",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,15 @@
"revision" : "40c465af19b993344e84355c00669ba2022ca3cd",
"version" : "7.1.1"
}
},
{
"identity" : "swift-ogg",
"kind" : "remoteSourceControl",
"location" : "https://github.com/element-hq/swift-ogg",
"state" : {
"branch" : "0.0.1",
"revision" : "e9a9e7601da662fd8b97d93781ff5c60b4becf88"
}
}
],
"version" : 2
Expand Down
2 changes: 1 addition & 1 deletion pkg/dependency/parser/swift/swift/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type Pin struct {
}

type State struct {
Branch any `json:"branch"`
Branch string `json:"branch"`
Revision string `json:"revision"`
Version string `json:"version"`
}
Loading