Skip to content

Commit

Permalink
Strict type Dependabot::PullRequestUpdater::Gitlab (#9132)
Browse files Browse the repository at this point in the history
Co-authored-by: AbdulFattaah Popoola <abdulapopoola@github.com>
  • Loading branch information
JamieMagee and abdulapopoola committed Feb 26, 2024
1 parent 6a32b25 commit 71580c0
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 13 deletions.
2 changes: 1 addition & 1 deletion common/lib/dependabot/pull_request_updater.rb
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ def gitlab_updater
files: files,
credentials: credentials,
pull_request_number: pull_request_number,
target_project_id: provider_metadata[:target_project_id]
target_project_id: T.cast(provider_metadata[:target_project_id], T.nilable(Integer))
)
end

Expand Down
73 changes: 61 additions & 12 deletions common/lib/dependabot/pull_request_updater/gitlab.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,51 @@
# typed: true
# typed: strict
# frozen_string_literal: true

require "gitlab"
require "sorbet-runtime"

require "dependabot/clients/gitlab_with_retries"
require "dependabot/credential"
require "dependabot/pull_request_creator"
require "gitlab"

module Dependabot
class PullRequestUpdater
class Gitlab
attr_reader :source, :files, :base_commit, :old_commit, :credentials,
:pull_request_number, :target_project_id
extend T::Sig

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

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

sig { returns(String) }
attr_reader :base_commit

sig { returns(String) }
attr_reader :old_commit

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

sig { returns(Integer) }
attr_reader :pull_request_number

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

sig do
params(
source: Dependabot::Source,
base_commit: String,
old_commit: String,
files: T::Array[Dependabot::DependencyFile],
credentials: T::Array[Dependabot::Credential],
pull_request_number: Integer,
target_project_id: T.nilable(Integer)
)
.void
end
def initialize(source:, base_commit:, old_commit:, files:,
credentials:, pull_request_number:, target_project_id:)
@source = source
Expand All @@ -22,6 +57,7 @@ def initialize(source:, base_commit:, old_commit:, files:,
@target_project_id = target_project_id
end

sig { returns(T.nilable(String)) }
def update
return unless merge_request_exists?
return unless branch_exists?(merge_request.source_branch)
Expand All @@ -32,38 +68,51 @@ def update

private

sig { returns(T::Boolean) }
def merge_request_exists?
merge_request
true
rescue ::Gitlab::Error::NotFound
false
end

sig { returns(T.untyped) }
def merge_request
@merge_request ||= gitlab_client_for_source.merge_request(
target_project_id || source.repo,
pull_request_number
@merge_request ||= T.let(
T.unsafe(gitlab_client_for_source).merge_request(
target_project_id || source.repo,
pull_request_number
),
T.untyped
)
end

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

sig { params(name: String).returns(T::Boolean) }
def branch_exists?(name)
gitlab_client_for_source.branch(source.repo, name)
!T.unsafe(gitlab_client_for_source).branch(source.repo, name).nil?
rescue ::Gitlab::Error::NotFound
false
end

# TODO: This needs to be typed when the underlying client is
sig { returns(T.untyped) }
def commit_being_updated
gitlab_client_for_source.commit(source.repo, old_commit)
T.unsafe(gitlab_client_for_source).commit(source.repo, old_commit)
end

sig { void }
def create_commit
gitlab_client_for_source.create_commit(
source.repo,
Expand Down

0 comments on commit 71580c0

Please sign in to comment.