Skip to content
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
22 changes: 20 additions & 2 deletions common/lib/dependabot/clients/azure.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

require "dependabot/shared_helpers"
require "dependabot/clients/json_response_parser"
require "dependabot/clients/comparison_commit"
require "excon"
require "sorbet-runtime"

Expand Down Expand Up @@ -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(
Expand All @@ -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) }
Expand Down Expand Up @@ -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]
Expand Down
23 changes: 11 additions & 12 deletions common/lib/dependabot/clients/bitbucket.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
require "sorbet-runtime"

require "dependabot/clients/json_response_parser"
require "dependabot/clients/comparison_commit"
require "dependabot/shared_helpers"

module Dependabot
Expand Down Expand Up @@ -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) }
Expand Down
21 changes: 21 additions & 0 deletions common/lib/dependabot/clients/comparison_commit.rb
Original file line number Diff line number Diff line change
@@ -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
8 changes: 0 additions & 8 deletions common/lib/dependabot/clients/json_response_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
19 changes: 2 additions & 17 deletions common/lib/dependabot/metadata_finders/base/commits_finder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
132 changes: 132 additions & 0 deletions common/spec/dependabot/clients/comparison_commit_spec.rb
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion sorbet/rbi/shims/bitbucket_with_retries.rbi
Original file line number Diff line number Diff line change
Expand Up @@ -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]]) }
Expand Down
Loading