class Dependabot::PullRequestCreator::Azure

def azure_client_for_source

def azure_client_for_source
  @azure_client_for_source ||=
    T.let(
      Dependabot::Clients::Azure.for_source(
        source: source,
        credentials: credentials
      ),
      T.nilable(Dependabot::Clients::Azure)
    )
end

def branch_exists?

def branch_exists?
  !azure_client_for_source.branch(branch_name).nil?
rescue ::Dependabot::Clients::Azure::NotFound
  false
end

def create

def create
  return if branch_exists? && pull_request_exists?
  # For Azure we create or update a branch in the same request as creating
  # a commit (so we don't need create or update branch logic here)
  create_commit
  create_pull_request
end

def create_commit

def create_commit
  author = author_details&.slice(:name, :email, :date)
  author = nil unless author&.any?
  azure_client_for_source.create_commit(
    branch_name,
    base_commit,
    commit_message,
    files,
    author
  )
end

def create_pull_request

def create_pull_request
  azure_client_for_source.create_pull_request(
    pr_name,
    branch_name,
    source.branch || default_branch,
    pr_description,
    labeler.labels_for_pr,
    reviewers,
    assignees,
    work_item
  )
end

def default_branch

def default_branch
  @default_branch ||=
    T.let(
      azure_client_for_source.fetch_default_branch(source.repo),
      T.nilable(String)
    )
end

def initialize(source:, branch_name:, base_commit:, credentials:,

def initialize(source:, branch_name:, base_commit:, credentials:,
               files:, commit_message:, pr_description:, pr_name:,
               author_details:, labeler:, reviewers: nil, assignees: nil, work_item: nil)
  @source         = source
  @branch_name    = branch_name
  @base_commit    = base_commit
  @credentials    = credentials
  @files          = files
  @commit_message = commit_message
  @pr_description = pr_description
  @pr_name        = pr_name
  @author_details = author_details
  @labeler        = labeler
  @reviewers      = reviewers
  @assignees      = assignees
  @work_item      = work_item
end

def pull_request_exists?

def pull_request_exists?
  azure_client_for_source.pull_requests(
    branch_name,
    source.branch || default_branch
  ).any?
end