Skip to content

Commit 3329651

Browse files
Implement metadata finder for pre-commit (#14222)
* implement metadata finder for precommit * implement metadata finder for precommit
1 parent ff015cb commit 3329651

File tree

2 files changed

+95
-9
lines changed

2 files changed

+95
-9
lines changed

pre_commit/lib/dependabot/pre_commit/metadata_finder.rb

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
1-
# typed: strong
1+
# typed: strict
22
# frozen_string_literal: true
33

4-
# NOTE: This file was scaffolded automatically but is OPTIONAL.
5-
# If you don't need custom metadata finding logic (changelogs, release notes, etc.),
6-
# you can safely delete this file and remove the require from lib/dependabot/pre_commit.rb
7-
4+
require "sorbet-runtime"
85
require "dependabot/metadata_finders"
96
require "dependabot/metadata_finders/base"
107

@@ -17,9 +14,15 @@ class MetadataFinder < Dependabot::MetadataFinders::Base
1714

1815
sig { override.returns(T.nilable(Dependabot::Source)) }
1916
def look_up_source
20-
# TODO: Implement custom source lookup logic if needed
21-
# Otherwise, delete this file and the require in the main registration file
22-
nil
17+
info = dependency.requirements.filter_map { |r| r[:source] }.first
18+
19+
url =
20+
if info.nil?
21+
dependency.name
22+
else
23+
info[:url] || info.fetch("url")
24+
end
25+
Source.from_url(url)
2326
end
2427
end
2528
end
Lines changed: 84 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,89 @@
1-
# typed: strong
1+
# typed: false
22
# frozen_string_literal: true
33

44
require "spec_helper"
55
require "dependabot/pre_commit/metadata_finder"
66
require_common_spec "metadata_finders/shared_examples_for_metadata_finders"
7+
8+
RSpec.describe Dependabot::PreCommit::MetadataFinder do
9+
subject(:finder) do
10+
described_class.new(dependency: dependency, credentials: credentials)
11+
end
12+
13+
let(:credentials) do
14+
[{
15+
"type" => "git_source",
16+
"host" => "github.com",
17+
"username" => "x-access-token",
18+
"password" => "token"
19+
}]
20+
end
21+
let(:dependency_source) do
22+
{
23+
type: "git",
24+
url: "https://github.com/pre-commit/pre-commit-hooks",
25+
ref: "v4.4.0",
26+
branch: nil
27+
}
28+
end
29+
let(:dependency_name) { "https://github.com/pre-commit/pre-commit-hooks" }
30+
let(:dependency) do
31+
Dependabot::Dependency.new(
32+
name: dependency_name,
33+
version: "v4.4.0",
34+
requirements: [{
35+
requirement: nil,
36+
groups: [],
37+
file: ".pre-commit-config.yaml",
38+
source: dependency_source
39+
}],
40+
package_manager: "pre_commit"
41+
)
42+
end
43+
44+
it_behaves_like "a dependency metadata finder"
45+
46+
describe "#source_url" do
47+
subject(:source_url) { finder.source_url }
48+
49+
context "when dealing with a git source" do
50+
let(:dependency_source) do
51+
{
52+
type: "git",
53+
url: "https://github.com/pre-commit/pre-commit-hooks",
54+
ref: "v4.4.0",
55+
branch: nil
56+
}
57+
end
58+
59+
it { is_expected.to eq("https://github.com/pre-commit/pre-commit-hooks") }
60+
end
61+
62+
context "when dealing with a subdependency (no requirements)" do
63+
let(:dependency) do
64+
Dependabot::Dependency.new(
65+
name: dependency_name,
66+
version: "v4.4.0",
67+
requirements: [],
68+
package_manager: "pre_commit"
69+
)
70+
end
71+
72+
it { is_expected.to eq("https://github.com/pre-commit/pre-commit-hooks") }
73+
end
74+
75+
context "when dealing with a gitlab source" do
76+
let(:dependency_name) { "https://gitlab.com/pycqa/flake8" }
77+
let(:dependency_source) do
78+
{
79+
type: "git",
80+
url: "https://gitlab.com/pycqa/flake8",
81+
ref: "v5.0.0",
82+
branch: nil
83+
}
84+
end
85+
86+
it { is_expected.to eq("https://gitlab.com/pycqa/flake8") }
87+
end
88+
end
89+
end

0 commit comments

Comments
 (0)