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: support parsing commits from Gemfile.lock files #108

Merged
merged 1 commit into from
Jun 12, 2022
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
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ typically a few hours ahead of the offline databases, and supports commits;
however it currently can produce false negatives for some ecosystems.

> While the API supports commits, the detector currently has limited support for
> extracting them - only the `composer.lock`, `package-lock.json`, `yarn.lock`,
> & `pnpm.yaml` parsers include commit details
> extracting them - only the `composer.lock`, `Gemfile.lock`,
> `package-lock.json`, `yarn.lock`, & `pnpm.yaml` parsers include commit details

You cannot use the API in `--offline` mode, but you can use both the offline
databases and the API together; the detector will remove any duplicate results.
Expand Down
33 changes: 33 additions & 0 deletions pkg/lockfile/fixtures/bundler/has-git-gem.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
GIT
remote: https://github.com/hanami/controller.git
revision: 027dbe2e56397b534e859fc283990cad1b6addd6
branch: action-new
specs:
hanami-controller (2.0.0.alpha1)
hanami-utils (~> 2.0.alpha)
rack (~> 2.0)

GIT
remote: https://github.com/hanami/utils.git
revision: 5904fc9a70683b8749aa2861257d0c8c01eae4aa
branch: unstable
specs:
hanami-utils (2.0.0.alpha1)
concurrent-ruby (~> 1.0)
transproc (~> 1.0)

GEM
remote: https://rubygems.org/
specs:
concurrent-ruby (1.1.7)
rack (2.2.3)
transproc (1.1.1)

PLATFORMS
x86_64-linux

DEPENDENCIES
hanami-utils!

BUNDLED WITH
2.2.28
15 changes: 15 additions & 0 deletions pkg/lockfile/parse-gemfile-lock.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,17 @@ type gemfileLockfileParser struct {
dependencies []PackageDetails
bundlerVersion string
rubyVersion string

// holds the commit of the gem that is currently being parsed, if found
currentGemCommit string
}

func (parser *gemfileLockfileParser) addDependency(name string, version string) {
parser.dependencies = append(parser.dependencies, PackageDetails{
Name: name,
Version: version,
Ecosystem: BundlerEcosystem,
Commit: parser.currentGemCommit,
})
}

Expand Down Expand Up @@ -83,6 +87,14 @@ func (parser *gemfileLockfileParser) parseSource(line string) {
options := optionsRegexp.FindStringSubmatch(line)

if options != nil {
commit := strings.TrimPrefix(options[0], " revision: ")

// if the prefix was removed then the gem being parsed is git based, so
// we store the commit to be included later
if commit != options[0] {
parser.currentGemCommit = commit
}

return
}

Expand Down Expand Up @@ -120,6 +132,9 @@ func (parser *gemfileLockfileParser) parse(contents string) {

for _, line := range lines {
if isSourceSection(line) {
// clear the stateful package details,
// since we're now parsing a new group
parser.currentGemCommit = ""
parser.state = parserStateSource
parser.parseSource(line)

Expand Down
43 changes: 43 additions & 0 deletions pkg/lockfile/parse-gemfile-lock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -619,3 +619,46 @@ func TestParseGemfileLock_HasLocalGem(t *testing.T) {
},
})
}

func TestParseGemfileLock_HasGitGem(t *testing.T) {
t.Parallel()

packages, err := lockfile.ParseGemfileLock("fixtures/bundler/has-git-gem.lock")

if err != nil {
t.Errorf("Got unexpected error: %v", err)
}

expectPackages(t, packages, []lockfile.PackageDetails{
{
Name: "hanami-controller",
Version: "2.0.0.alpha1",
Ecosystem: lockfile.BundlerEcosystem,
Commit: "027dbe2e56397b534e859fc283990cad1b6addd6",
},
{
Name: "hanami-utils",
Version: "2.0.0.alpha1",
Ecosystem: lockfile.BundlerEcosystem,
Commit: "5904fc9a70683b8749aa2861257d0c8c01eae4aa",
},
{
Name: "concurrent-ruby",
Version: "1.1.7",
Ecosystem: lockfile.BundlerEcosystem,
Commit: "",
},
{
Name: "rack",
Version: "2.2.3",
Ecosystem: lockfile.BundlerEcosystem,
Commit: "",
},
{
Name: "transproc",
Version: "1.1.1",
Ecosystem: lockfile.BundlerEcosystem,
Commit: "",
},
})
}