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

Strict type Dependabot::PullRequestCreator::Bitbucket #9140

Merged
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
2 changes: 1 addition & 1 deletion common/lib/dependabot/clients/bitbucket.rb
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ def create_commit(repo, branch_name, base_commit, commit_message, files,
target_branch: String,
pr_description: String,
_labels: T.nilable(T::Array[String]),
_work_item: T.nilable(String)
_work_item: T.nilable(Integer)
)
.void
end
Expand Down
8 changes: 4 additions & 4 deletions common/lib/dependabot/pull_request_creator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -326,12 +326,12 @@ def bitbucket_creator
base_commit: base_commit,
credentials: credentials,
files: files,
commit_message: message.commit_message,
pr_description: message.pr_message,
pr_name: message.pr_name,
commit_message: T.must(message.commit_message),
pr_description: T.must(message.pr_message),
pr_name: T.must(message.pr_name),
author_details: author_details,
labeler: nil,
work_item: provider_metadata&.fetch(:work_item, nil)
work_item: T.cast(provider_metadata&.fetch(:work_item, nil), T.nilable(Integer))
)
end

Expand Down
82 changes: 73 additions & 9 deletions common/lib/dependabot/pull_request_creator/bitbucket.rb
Original file line number Diff line number Diff line change
@@ -1,19 +1,70 @@
# typed: true
# typed: strict
# frozen_string_literal: true

require "sorbet-runtime"

require "dependabot/clients/bitbucket"
require "dependabot/credential"
require "dependabot/dependency_file"
require "dependabot/pull_request_creator"

module Dependabot
class PullRequestCreator
class Bitbucket
attr_reader :source, :branch_name, :base_commit, :credentials,
:files, :commit_message, :pr_description, :pr_name,
:author_details, :labeler, :work_item
extend T::Sig

sig { returns(Dependabot::Source) }
attr_reader :source

sig { returns(String) }
attr_reader :branch_name

sig { returns(String) }
attr_reader :base_commit

sig { returns(T::Array[Dependabot::Credential]) }
attr_reader :credentials

sig { returns(T::Array[Dependabot::DependencyFile]) }
attr_reader :files

sig { returns(String) }
attr_reader :commit_message

sig { returns(String) }
attr_reader :pr_description

sig { returns(String) }
attr_reader :pr_name

sig { returns(T.nilable(T::Hash[Symbol, String])) }
attr_reader :author_details

sig { returns(T.nilable(Dependabot::PullRequestCreator::Labeler)) }
attr_reader :labeler

sig { returns(T.nilable(Integer)) }
attr_reader :work_item

# BitBucket Cloud accepts > 1MB characters, but they display poorly in the UI, so limiting to 4x 65,536
PR_DESCRIPTION_MAX_LENGTH = 262_143 # 0 based count

sig do
params(
source: Dependabot::Source,
branch_name: String,
base_commit: String,
credentials: T::Array[Dependabot::Credential],
files: T::Array[Dependabot::DependencyFile],
commit_message: String,
pr_description: String,
pr_name: String,
author_details: T.nilable(T::Hash[Symbol, String]),
labeler: T.nilable(Dependabot::PullRequestCreator::Labeler),
work_item: T.nilable(Integer)
)
.void
end
def initialize(source:, branch_name:, base_commit:, credentials:,
files:, commit_message:, pr_description:, pr_name:,
author_details:, labeler: nil, work_item: nil)
Expand All @@ -30,6 +81,7 @@ def initialize(source:, branch_name:, base_commit:, credentials:,
@work_item = work_item
end

sig { void }
def create
return if branch_exists? && pull_request_exists?

Expand All @@ -43,20 +95,26 @@ def create

private

sig { returns(Dependabot::Clients::Bitbucket) }
def bitbucket_client_for_source
@bitbucket_client_for_source ||=
Dependabot::Clients::Bitbucket.for_source(
source: source,
credentials: credentials
T.let(
Dependabot::Clients::Bitbucket.for_source(
source: source,
credentials: credentials
),
T.nilable(Dependabot::Clients::Bitbucket)
)
end

sig { returns(T::Boolean) }
def branch_exists?
bitbucket_client_for_source.branch(source.repo, branch_name)
!bitbucket_client_for_source.branch(source.repo, branch_name).nil?
rescue Clients::Bitbucket::NotFound
false
end

sig { returns(T::Boolean) }
def pull_request_exists?
bitbucket_client_for_source.pull_requests(
source.repo,
Expand All @@ -65,6 +123,7 @@ def pull_request_exists?
).any?
end

sig { void }
def create_commit
author = author_details&.slice(:name, :email)
author = nil unless author&.any?
Expand All @@ -79,6 +138,7 @@ def create_commit
)
end

sig { void }
def create_pull_request
bitbucket_client_for_source.create_pull_request(
source.repo,
Expand All @@ -91,9 +151,13 @@ def create_pull_request
)
end

sig { returns(String) }
def default_branch
@default_branch ||=
bitbucket_client_for_source.fetch_default_branch(source.repo)
T.let(
bitbucket_client_for_source.fetch_default_branch(source.repo),
T.nilable(String)
)
end
end
end
Expand Down
Loading