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

Add support for pre-commit package manager #486

Closed
wants to merge 1 commit into from
Closed
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
44 changes: 44 additions & 0 deletions lib/bibliothecary/parsers/pre_commit.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
require "yaml"

module Bibliothecary
module Parsers
class PreCommit
include Bibliothecary::Analyser

def self.mapping
{
match_filename(".pre-commit-config.yaml") => {
kind: "manifest",
parser: :parser_manifest,
}
}
end

def self.parser_manifest(file_contents)
manifest = YAML.load(file_contents)

return [] unless manifest
return [] unless manifest["repos"]

manifest["repos"].map do |repo|
if (
repo["repo"] and
repo["rev"] and
repo["hooks"] and
not ["meta", "local"].include?(repo["repo"])
)
repo.fetch("hooks", []).map do |hook|
{
name: hook["id"],
requirement: repo["rev"],
type: "runtime",
}
end
else
[]
end
end.flatten
end
end
end
end
7 changes: 7 additions & 0 deletions spec/bibliothecary_spec.rb
Original file line number Diff line number Diff line change
@@ -27,6 +27,7 @@
Bibliothecary::Parsers::NPM,
Bibliothecary::Parsers::Nuget,
Bibliothecary::Parsers::Packagist,
Bibliothecary::Parsers::PreCommit,
Bibliothecary::Parsers::Pub,
Bibliothecary::Parsers::Pypi,
Bibliothecary::Parsers::Rubygems,
@@ -270,6 +271,12 @@
:kind=>"manifest",
:success=>true,
:related_paths=>[]},
{:platform=>"precommit",
:path=>".pre-commit-config.yaml",
:dependencies=>[],
:kind=>"manifest",
:success=>true,
:related_paths=>[]},
{:platform=>"rubygems",
:path=>"Gemfile",
:dependencies=>[],
23 changes: 23 additions & 0 deletions spec/fixtures/.pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v2.3.0
hooks:
- id: check-yaml
- id: end-of-file-fixer
- id: trailing-whitespace
- repo: https://github.com/psf/black
rev: 19.3b0
hooks:
- id: black
- repo: meta
hooks:
- id: check-hooks-apply
- id: check-useless-excludes
- repo: local
hooks:
- id: scss-lint
name: scss-lint
entry: scss-lint
types: [scss]
language: ruby
additional_dependencies: ['scss_lint:0.57.0']
4 changes: 4 additions & 0 deletions spec/fixtures/broken/.pre-commit-config_hooks.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v2.3.0
hooks:
1 change: 1 addition & 0 deletions spec/fixtures/broken/.pre-commit-config_repos.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
repos:
Empty file.
42 changes: 42 additions & 0 deletions spec/parsers/pre_commit_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
require "spec_helper"

describe Bibliothecary::Parsers::PreCommit do
it "has a platform name" do
expect(described_class.platform_name).to eq("precommit")
end

it "parses dependencies from .pre-commit-config.yaml" do
expect(described_class.analyse_contents(".pre-commit-config.yaml", load_fixture(".pre-commit-config.yaml"))).to eq({
:platform=>"precommit",
:path=>".pre-commit-config.yaml",
:dependencies=>[
{:name=>"check-yaml", :requirement=>"v2.3.0", :type=>"runtime"},
{:name=>"end-of-file-fixer", :requirement=>"v2.3.0", :type=>"runtime"},
{:name=>"trailing-whitespace", :requirement=>"v2.3.0", :type=>"runtime"},
{:name=>"black", :requirement=>"19.3b0", :type=>"runtime"},
],
kind: 'manifest',
success: true,
})
end

it "parses dependencies from .pre-commit-config.yaml without repos" do
expect(described_class.analyse_contents(".pre-commit-config.yaml", load_fixture("broken/.pre-commit-config_repos.yaml"))).to eq({
:platform=>"precommit",
:path=>".pre-commit-config.yaml",
:dependencies=>[],
kind: 'manifest',
success: true,
})
end

it "parses dependencies from .pre-commit-config.yaml without hooks" do
expect(described_class.analyse_contents(".pre-commit-config.yaml", load_fixture("broken/.pre-commit-config_hooks.yaml"))).to eq({
:platform=>"precommit",
:path=>".pre-commit-config.yaml",
:dependencies=>[],
kind: 'manifest',
success: true,
})
end
end