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

feat(swift): add Package.resolved file support #245

Merged
merged 7 commits into from
Aug 23, 2023
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
75 changes: 75 additions & 0 deletions pkg/swift/swift/parse.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package swift

import (
dio "github.com/aquasecurity/go-dep-parser/pkg/io"
"github.com/aquasecurity/go-dep-parser/pkg/types"
"github.com/liamg/jfather"
"golang.org/x/xerrors"
"io"
"sort"
"strings"
)

// Parser is a parser for Package.resolved files
type Parser struct{}

func NewParser() types.Parser {
return &Parser{}
}

func (Parser) Parse(r dio.ReadSeekerAt) ([]types.Library, []types.Dependency, error) {
var lockFile LockFile
input, err := io.ReadAll(r)
if err != nil {
return nil, nil, xerrors.Errorf("read error: %w", err)
}
if err := jfather.Unmarshal(input, &lockFile); err != nil {
return nil, nil, xerrors.Errorf("decode error: %w", err)
}

var libs types.Libraries
pins := lockFile.Object.Pins
if lockFile.Version > 1 {
pins = lockFile.Pins
}
for _, pin := range pins {
libs = append(libs, types.Library{
Name: libraryName(pin, lockFile.Version),
Version: pin.State.Version,
Locations: []types.Location{
{
StartLine: pin.StartLine,
EndLine: pin.EndLine,
},
},
})
}
sort.Sort(libs)
return libs, nil, nil
}

func libraryName(pin Pin, lockVersion int) string {
// Package.resolved v1 uses `RepositoryURL`
// v2 uses `Location`
name := pin.RepositoryURL
if lockVersion > 1 {
name = pin.Location
}
// Swift uses `https://github.com/<author>/<package>.git format
// `.git` suffix can be omitted (take a look happy test)
// Remove `https://` and `.git` to fit the same format
name = strings.TrimPrefix(name, "https://")
name = strings.TrimSuffix(name, ".git")
return name
}

// UnmarshalJSONWithMetadata needed to detect start and end lines of deps for v1
func (p *Pin) UnmarshalJSONWithMetadata(node jfather.Node) error {
if err := node.Decode(&p); err != nil {
return err
}
// Decode func will overwrite line numbers if we save them first
p.StartLine = node.Range().Start.Line
p.EndLine = node.Range().End.Line
return nil
}
93 changes: 93 additions & 0 deletions pkg/swift/swift/parse_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package swift

import (
"github.com/aquasecurity/go-dep-parser/pkg/types"
"github.com/stretchr/testify/assert"
"os"
"testing"
)

func TestParser_Parse(t *testing.T) {
tests := []struct {
name string
inputFile string
want []types.Library
}{
// docker run -it --rm swift@sha256:3c62ac97506ecf19ca15e4db57d7930e6a71559b23b19aa57e13d380133a54db
// mkdir app && cd app
// swift package init
// ## add new deps: ##
// sed -i 's/"1.0.0")/"1.0.0")\n.package(url: "https:\/\/github.com\/ReactiveCocoa\/ReactiveSwift", from: "7.0.0"),\n.package(url: "https:\/\/github.com\/Quick\/Nimble", .exact("9.2.1"))/' Package.swift
// swift package update
{
name: "happy path v1",
inputFile: "testdata/happy-v1-Package.resolved",
want: []types.Library{
{
Name: "github.com/Quick/Nimble",
Version: "9.2.1",
Locations: []types.Location{{StartLine: 4, EndLine: 12}},
},
{
Name: "github.com/ReactiveCocoa/ReactiveSwift",
Version: "7.1.1",
Locations: []types.Location{{StartLine: 13, EndLine: 21}},
},
},
},
// 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",
want: []types.Library{
{
Name: "github.com/Quick/Nimble",
Version: "9.2.1",
Locations: []types.Location{{StartLine: 21, EndLine: 29}},
},
{
Name: "github.com/Quick/Quick",
Version: "7.2.0",
Locations: []types.Location{{StartLine: 30, EndLine: 38}},
},
{
Name: "github.com/ReactiveCocoa/ReactiveSwift",
Version: "7.1.1",
Locations: []types.Location{{StartLine: 39, EndLine: 47}},
},
{
Name: "github.com/mattgallagher/CwlCatchException",
Version: "2.1.2",
Locations: []types.Location{{StartLine: 3, EndLine: 11}},
},
{
Name: "github.com/mattgallagher/CwlPreconditionTesting",
Version: "2.1.2",
Locations: []types.Location{{StartLine: 12, EndLine: 20}},
},
},
},
{
name: "empty",
inputFile: "testdata/empty-Package.resolved",
want: nil,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
parser := NewParser()
f, err := os.Open(tt.inputFile)
assert.NoError(t, err)

libs, _, err := parser.Parse(f)
assert.NoError(t, err)
assert.Equal(t, tt.want, libs)
})
}
}
1 change: 1 addition & 0 deletions pkg/swift/swift/testdata/empty-Package.resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
25 changes: 25 additions & 0 deletions pkg/swift/swift/testdata/happy-v1-Package.resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"object": {
"pins": [
{
"package": "Nimble",
"repositoryURL": "https://github.com/Quick/Nimble.git",
"state": {
"branch": null,
"revision": "c93f16c25af5770f0d3e6af27c9634640946b068",
"version": "9.2.1"
}
},
{
"package": "ReactiveSwift",
"repositoryURL": "https://github.com/ReactiveCocoa/ReactiveSwift",
"state": {
"branch": null,
"revision": "40c465af19b993344e84355c00669ba2022ca3cd",
"version": "7.1.1"
}
}
]
},
"version": 1
}
50 changes: 50 additions & 0 deletions pkg/swift/swift/testdata/happy-v2-Package.resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
{
"pins" : [
{
"identity" : "cwlcatchexception",
"kind" : "remoteSourceControl",
"location" : "https://github.com/mattgallagher/CwlCatchException.git",
"state" : {
"revision" : "3b123999de19bf04905bc1dfdb76f817b0f2cc00",
"version" : "2.1.2"
}
},
{
"identity" : "cwlpreconditiontesting",
"kind" : "remoteSourceControl",
"location" : "https://github.com/mattgallagher/CwlPreconditionTesting.git",
"state" : {
"revision" : "a23ded2c91df9156628a6996ab4f347526f17b6b",
"version" : "2.1.2"
}
},
{
"identity" : "nimble",
"kind" : "remoteSourceControl",
"location" : "https://github.com/Quick/Nimble.git",
"state" : {
"revision" : "c93f16c25af5770f0d3e6af27c9634640946b068",
"version" : "9.2.1"
}
},
{
"identity" : "quick",
"kind" : "remoteSourceControl",
"location" : "https://github.com/Quick/Quick.git",
"state" : {
"revision" : "494eff9ad74a37047782b0d5d8d84c7ff49a60e4",
"version" : "7.2.0"
}
},
{
"identity" : "reactiveswift",
"kind" : "remoteSourceControl",
"location" : "https://github.com/ReactiveCocoa/ReactiveSwift",
"state" : {
"revision" : "40c465af19b993344e84355c00669ba2022ca3cd",
"version" : "7.1.1"
}
}
],
"version" : 2
}
26 changes: 26 additions & 0 deletions pkg/swift/swift/types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package swift

type LockFile struct {
Object Object `json:"object"`
Pins []Pin `json:"pins"`
Version int `json:"version"`
}

type Object struct {
Pins []Pin `json:"pins"`
}

type Pin struct {
Package string `json:"package"`
RepositoryURL string `json:"repositoryURL"` // Package.revision v1
Location string `json:"location"` // Package.revision v2
State State `json:"state"`
StartLine int
EndLine int
}

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