Class: Dependabot::Clients::GitlabWithRetries

Inherits:
SimpleDelegator
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/dependabot/clients/gitlab_with_retries.rb

Defined Under Namespace

Classes: ContentEncoding

Constant Summary collapse

ClientOptions =
T.type_alias { T::Hash[Symbol, Object] }
RETRYABLE_ERRORS =
T.let(
  [Gitlab::Error::BadGateway].freeze,
  T::Array[T.class_of(Gitlab::Error::ResponseError)]
)

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(max_retries: 3, **args) ⇒ GitlabWithRetries

Returns a new instance of GitlabWithRetries.



87
88
89
90
91
# File 'lib/dependabot/clients/gitlab_with_retries.rb', line 87

def initialize(max_retries: 3, **args)
  @max_retries = T.let(max_retries || 3, Integer)
  @client = T.let(::Gitlab::Client.new(args), ::Gitlab::Client)
  super(@client)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object



130
131
132
133
134
135
136
# File 'lib/dependabot/clients/gitlab_with_retries.rb', line 130

def method_missing(method_name, *args, &block)
  original_args = args
  retry_connection_failures do
    args = original_args.map(&:dup)
    super
  end
end

Class Method Details

.for_gitlab_dot_com(credentials:) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/dependabot/clients/gitlab_with_retries.rb', line 53

def self.for_gitlab_dot_com(credentials:)
  access_token =
    credentials
    .select { |cred| cred["type"] == "git_source" }
    .select { |cred| cred["password"] }
    .find { |cred| cred["host"] == "gitlab.com" }
    &.fetch("password")

  new(
    endpoint: "https://gitlab.com/api/v4",
    private_token: access_token || ""
  )
end

.for_source(source:, credentials:) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/dependabot/clients/gitlab_with_retries.rb', line 38

def self.for_source(source:, credentials:)
  access_token =
    credentials
    .select { |cred| cred["type"] == "git_source" }
    .select { |cred| cred["password"] }
    .find { |cred| cred["host"] == source.hostname }
    &.fetch("password")

  new(
    endpoint: source.api_endpoint,
    private_token: access_token || ""
  )
end

Instance Method Details

#create_commit(repo, branch_name, commit_message, files, **options) ⇒ Object



111
112
113
114
115
116
117
118
119
# File 'lib/dependabot/clients/gitlab_with_retries.rb', line 111

def create_commit(repo, branch_name, commit_message, files, **options)
  @client.create_commit(
    repo,
    branch_name,
    commit_message,
    file_actions(files),
    options
  )
end

#fetch_commit(repo, branch) ⇒ Object



72
73
74
75
# File 'lib/dependabot/clients/gitlab_with_retries.rb', line 72

def fetch_commit(repo, branch)
  commit = T.cast(branch(repo, branch)["commit"], Gitlab::ObjectifiedHash)
  T.cast(commit["id"], String)
end

#fetch_default_branch(repo) ⇒ Object



78
79
80
# File 'lib/dependabot/clients/gitlab_with_retries.rb', line 78

def fetch_default_branch(repo)
  T.cast(project(repo)["default_branch"], String)
end

#respond_to_missing?(method_name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


145
146
147
# File 'lib/dependabot/clients/gitlab_with_retries.rb', line 145

def respond_to_missing?(method_name, include_private = false)
  @client.respond_to?(method_name, include_private)
end

#retry_connection_failures(&_blk) ⇒ Object



154
155
156
157
158
159
160
161
162
163
# File 'lib/dependabot/clients/gitlab_with_retries.rb', line 154

def retry_connection_failures(&_blk)
  retry_attempt = 0

  begin
    yield
  rescue *RETRYABLE_ERRORS
    retry_attempt += 1
    retry_attempt <= @max_retries ? retry : Kernel.raise
  end
end