From 61840d1015b1d4bfabceb7d556ecdf8c453308db Mon Sep 17 00:00:00 2001 From: Jamie Magee Date: Mon, 16 Nov 2026 08:26:32 -0800 Subject: [PATCH] Return typed provider comparison commits --- common/lib/dependabot/clients/azure.rb | 22 ++- common/lib/dependabot/clients/bitbucket.rb | 23 ++- .../dependabot/clients/comparison_commit.rb | 21 +++ .../clients/json_response_parser.rb | 8 -- .../base/commit_response_parser.rb | 8 -- .../metadata_finders/base/commits_finder.rb | 19 +-- .../clients/comparison_commit_spec.rb | 132 ++++++++++++++++++ sorbet/rbi/shims/bitbucket_with_retries.rbi | 2 +- 8 files changed, 187 insertions(+), 48 deletions(-) create mode 100644 common/lib/dependabot/clients/comparison_commit.rb create mode 100644 common/spec/dependabot/clients/comparison_commit_spec.rb diff --git a/common/lib/dependabot/clients/azure.rb b/common/lib/dependabot/clients/azure.rb index 9fc66fef5f5..d8cf2d3f6a7 100644 --- a/common/lib/dependabot/clients/azure.rb +++ b/common/lib/dependabot/clients/azure.rb @@ -3,6 +3,7 @@ require "dependabot/shared_helpers" require "dependabot/clients/json_response_parser" +require "dependabot/clients/comparison_commit" require "excon" require "sorbet-runtime" @@ -372,7 +373,7 @@ def update_ref(branch_name, old_commit, new_commit) new_tag: T.nilable(String), type: String ) - .returns(JsonObjects) + .returns(T::Array[ComparisonCommit]) end def compare(previous_tag, new_tag, type) response = get( @@ -386,7 +387,13 @@ def compare(previous_tag, new_tag, type) ) root = parse_json_object(response.body, "commit comparison") - object_array_field(root, "value", "commit comparison") + object_array_field(root, "value", "commit comparison").map do |commit| + ComparisonCommit.new( + sha: comparison_string(commit, "commitId"), + message: comparison_string(commit, "comment"), + html_url: comparison_string(commit, "remoteUrl") + ) + end end sig { params(url: String).returns(Excon::Response) } @@ -487,6 +494,17 @@ def patch(url, json) private + sig { params(commit: JsonObject, key: String).returns(String) } + def comparison_string(commit, key) + value = commit[key] + return value if value.is_a?(String) + + raise Dependabot::PrivateSourceBadResponse.new( + source.url, + "Malformed Azure commit response: commit #{key} must be a string" + ) + end + sig { returns([String, String]) } def response_identity ["Azure", source.url] diff --git a/common/lib/dependabot/clients/bitbucket.rb b/common/lib/dependabot/clients/bitbucket.rb index 3523eab70df..a2a034283e5 100644 --- a/common/lib/dependabot/clients/bitbucket.rb +++ b/common/lib/dependabot/clients/bitbucket.rb @@ -5,6 +5,7 @@ require "sorbet-runtime" require "dependabot/clients/json_response_parser" +require "dependabot/clients/comparison_commit" require "dependabot/shared_helpers" module Dependabot @@ -309,24 +310,22 @@ def tags(repo) object_array_field(root, "values", "tags") end - sig do - params( - repo: String, - previous_tag: String, - new_tag: String - ) - .returns(JsonObjects) - end + sig { params(repo: String, previous_tag: String, new_tag: String).returns(T::Array[ComparisonCommit]) } def compare(repo, previous_tag, new_tag) path = "#{repo}/commits/?include=#{new_tag}&exclude=#{previous_tag}" response = get(base_url + path) root = parse_json_object(response.body, "commit comparison") - commits = object_array_field(root, "values", "commit comparison") - commits.each do |commit| - validate_string_paths(commit, "commit comparison", [%w(hash), %w(summary raw), %w(links html href)]) + object_array_field(root, "values", "commit comparison").map do |commit| + summary = object_field(commit, "summary", "commit comparison") + links = object_field(commit, "links", "commit comparison") + html = object_field(links, "html", "commit comparison") + ComparisonCommit.new( + sha: string_field(commit, "hash", "commit comparison"), + message: string_field(summary, "raw", "commit comparison"), + html_url: string_field(html, "href", "commit comparison") + ) end - commits end sig { params(url: String).returns(Excon::Response) } diff --git a/common/lib/dependabot/clients/comparison_commit.rb b/common/lib/dependabot/clients/comparison_commit.rb new file mode 100644 index 00000000000..d4d3cb85834 --- /dev/null +++ b/common/lib/dependabot/clients/comparison_commit.rb @@ -0,0 +1,21 @@ +# typed: strong +# frozen_string_literal: true + +require "sorbet-runtime" + +module Dependabot + module Clients + class ComparisonCommit < T::ImmutableStruct + extend T::Sig + + const :sha, String + const :message, String + const :html_url, String + + sig { returns(T::Hash[Symbol, String]) } + def to_h + { message: message, sha: sha, html_url: html_url } + end + end + end +end diff --git a/common/lib/dependabot/clients/json_response_parser.rb b/common/lib/dependabot/clients/json_response_parser.rb index 02a5e7a6ae1..740ab49d7d1 100644 --- a/common/lib/dependabot/clients/json_response_parser.rb +++ b/common/lib/dependabot/clients/json_response_parser.rb @@ -77,14 +77,6 @@ def first_object_field(object, key, context) raise_bad_response(context, "#{key} must contain at least one object") end - sig { params(object: JsonObject, context: String, paths: T::Array[T::Array[String]]).void } - def validate_string_paths(object, context, paths) - paths.each do |path| - current = T.must(path[0...-1]).reduce(object) { |value, key| object_field(value, key, context) } - string_field(current, T.must(path.last), context) - end - end - sig { params(context: String, message: String).returns(T.noreturn) } def raise_bad_response(context, message) provider, source = response_identity diff --git a/common/lib/dependabot/metadata_finders/base/commit_response_parser.rb b/common/lib/dependabot/metadata_finders/base/commit_response_parser.rb index d88db0647af..a0a16f9218f 100644 --- a/common/lib/dependabot/metadata_finders/base/commit_response_parser.rb +++ b/common/lib/dependabot/metadata_finders/base/commit_response_parser.rb @@ -47,14 +47,6 @@ def gitlab_string(resource, key, context) raise_bad_response("GitLab", "#{context} #{key} must be a string") end - sig { params(object: T::Hash[String, Object], key: String, context: String).returns(String) } - def object_string(object, key, context) - value = object[key] - return value if value.is_a?(String) - - raise_bad_response("Azure", "#{context} #{key} must be a string") - end - sig { params(provider: String, message: String).returns(T.noreturn) } def raise_bad_response(provider, message) raise Dependabot::PrivateSourceBadResponse.new( diff --git a/common/lib/dependabot/metadata_finders/base/commits_finder.rb b/common/lib/dependabot/metadata_finders/base/commits_finder.rb index 125baf37b08..2f58b68c224 100644 --- a/common/lib/dependabot/metadata_finders/base/commits_finder.rb +++ b/common/lib/dependabot/metadata_finders/base/commits_finder.rb @@ -310,16 +310,7 @@ def fetch_github_commits def fetch_bitbucket_commits bitbucket_client .compare(T.must(source).repo, T.must(previous_tag), T.must(new_tag)) - .map do |commit| - summary = T.cast(commit.fetch("summary"), Dependabot::Clients::Bitbucket::JsonObject) - links = T.cast(commit.fetch("links"), Dependabot::Clients::Bitbucket::JsonObject) - html = T.cast(links.fetch("html"), Dependabot::Clients::Bitbucket::JsonObject) - { - message: T.cast(summary.fetch("raw"), String), - sha: T.cast(commit.fetch("hash"), String), - html_url: T.cast(html.fetch("href"), String) - } - end + .map(&:to_h) rescue Dependabot::Clients::Bitbucket::NotFound, Dependabot::Clients::Bitbucket::Unauthorized, Dependabot::Clients::Bitbucket::Forbidden, @@ -356,13 +347,7 @@ def fetch_azure_commits type = git_sha?(new_tag) ? "commit" : "tag" azure_client .compare(previous_tag, new_tag, type) - .map do |commit| - { - message: commit_response_parser.object_string(commit, "comment", "commit"), - sha: commit_response_parser.object_string(commit, "commitId", "commit"), - html_url: commit_response_parser.object_string(commit, "remoteUrl", "commit") - } - end + .map(&:to_h) rescue Dependabot::Clients::Azure::NotFound, Dependabot::Clients::Azure::Unauthorized, Dependabot::Clients::Azure::Forbidden, diff --git a/common/spec/dependabot/clients/comparison_commit_spec.rb b/common/spec/dependabot/clients/comparison_commit_spec.rb new file mode 100644 index 00000000000..e459c2d8050 --- /dev/null +++ b/common/spec/dependabot/clients/comparison_commit_spec.rb @@ -0,0 +1,132 @@ +# typed: strict +# frozen_string_literal: true + +require "spec_helper" +require "dependabot/clients/azure" +require "dependabot/clients/bitbucket" +require "dependabot/source" + +RSpec.describe Dependabot::Clients::ComparisonCommit do + describe Dependabot::Clients::Bitbucket do + extend T::Sig + + # rubocop:disable RSpec/ScatteredLet -- Grouping lets would detach their required Sorbet signatures. + sig { returns(Dependabot::Clients::Bitbucket) } # rubocop:disable RSpec/DescribedClass -- Concrete type. + let(:client) { described_class.new(credentials: nil) } + + sig { returns(String) } + let(:url) { "https://api.bitbucket.org/2.0/repositories/test/repo/commits/?include=new&exclude=old" } + + sig { returns(String) } + let(:response) do + { + "values" => %w(first second).map do |sha| + { + "hash" => sha, + "summary" => { "raw" => "#{sha} message" }, + "links" => { "html" => { "href" => "https://example.com/#{sha}" } }, + "unused" => false + } + end + }.to_json + end + # rubocop:enable RSpec/ScatteredLet + + before { stub_request(:get, url).to_return(status: 200, body: response) } + + it "returns decoded commits in response order" do + commits = client.compare("test/repo", "old", "new") + + expect(commits.map { |commit| [commit.sha, commit.message, commit.html_url] }).to eq( + [ + ["first", "first message", "https://example.com/first"], + ["second", "second message", "https://example.com/second"] + ] + ) + end + + context "with no comparison commits" do + sig { returns(String) } + let(:response) { '{"values":[]}' } + + it "returns an empty result" do + expect(client.compare("test/repo", "old", "new")).to eq([]) + end + end + + context "with malformed nested commit fields" do + sig { returns(String) } + let(:response) { '{"values":[{"hash":"first","summary":{"raw":false},"links":{"html":{"href":"url"}}}]}' } + + it "reports the provider and operation" do + expect { client.compare("test/repo", "old", "new") } + .to raise_error(Dependabot::PrivateSourceBadResponse, /Malformed Bitbucket response for commit comparison/) + end + end + end + + describe Dependabot::Clients::Azure do + extend T::Sig + + # rubocop:disable RSpec/ScatteredLet -- Grouping lets would detach their required Sorbet signatures. + sig { returns(Dependabot::Clients::Azure) } # rubocop:disable RSpec/DescribedClass -- Concrete type. + let(:client) { described_class.new(source, nil) } + + sig { returns(Dependabot::Source) } + let(:source) { T.must(Dependabot::Source.from_url("https://dev.azure.com/org/project/_git/repo")) } + + sig { returns(String) } + let(:url) do + "https://dev.azure.com/org/project/_apis/git/repositories/repo/commits" \ + "?searchCriteria.itemVersion.versionType=tag&searchCriteria.itemVersion.version=old" \ + "&searchCriteria.compareVersion.versionType=tag&searchCriteria.compareVersion.version=new" + end + + sig { returns(String) } + let(:response) do + { + "value" => %w(first second).map do |sha| + { + "commitId" => sha, + "comment" => "#{sha} message", + "remoteUrl" => "https://example.com/#{sha}", + "unused" => false + } + end + }.to_json + end + # rubocop:enable RSpec/ScatteredLet + + before { stub_request(:get, url).to_return(status: 200, body: response) } + + it "returns decoded commits in response order" do + commits = client.compare("old", "new", "tag") + + expect(commits.map { |commit| [commit.sha, commit.message, commit.html_url] }).to eq( + [ + ["first", "first message", "https://example.com/first"], + ["second", "second message", "https://example.com/second"] + ] + ) + end + + context "with no comparison commits" do + sig { returns(String) } + let(:response) { '{"value":[]}' } + + it "returns an empty result" do + expect(client.compare("old", "new", "tag")).to eq([]) + end + end + + context "with malformed commit fields" do + sig { returns(String) } + let(:response) { '{"value":[{"commitId":"first","comment":false,"remoteUrl":"url"}]}' } + + it "reports the provider and operation" do + expect { client.compare("old", "new", "tag") } + .to raise_error(Dependabot::PrivateSourceBadResponse, /Malformed Azure commit response/) + end + end + end +end diff --git a/sorbet/rbi/shims/bitbucket_with_retries.rbi b/sorbet/rbi/shims/bitbucket_with_retries.rbi index 8a70b914ea3..98561c80e77 100644 --- a/sorbet/rbi/shims/bitbucket_with_retries.rbi +++ b/sorbet/rbi/shims/bitbucket_with_retries.rbi @@ -44,7 +44,7 @@ module Dependabot sig { params(url: String).returns(Excon::Response) } def get(url); end - sig { params(repo: String, previous_tag: String, new_tag: String).returns(T::Array[Bitbucket::JsonObject]) } + sig { params(repo: String, previous_tag: String, new_tag: String).returns(T::Array[ComparisonCommit]) } def compare(repo, previous_tag, new_tag); end sig { params(repo: String).returns(T::Array[T::Hash[Symbol, String]]) }