Class: Dependabot::Clients::GitlabWithRetries

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

Defined Under Namespace

Classes: ContentEncoding

Constant Summary collapse

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.



83
84
85
86
# File 'lib/dependabot/clients/gitlab_with_retries.rb', line 83

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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



125
126
127
128
129
130
131
132
133
134
# File 'lib/dependabot/clients/gitlab_with_retries.rb', line 125

def method_missing(method_name, *args, &block)
  retry_connection_failures do
    if @client.respond_to?(method_name)
      mutatable_args = args.map(&:dup)
      T.unsafe(@client).public_send(method_name, *mutatable_args, &block)
    else
      super
    end
  end
end

Class Method Details

.for_gitlab_dot_com(credentials:) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/dependabot/clients/gitlab_with_retries.rb', line 50

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



35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/dependabot/clients/gitlab_with_retries.rb', line 35

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



106
107
108
109
110
111
112
113
114
# File 'lib/dependabot/clients/gitlab_with_retries.rb', line 106

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



69
70
71
# File 'lib/dependabot/clients/gitlab_with_retries.rb', line 69

def fetch_commit(repo, branch)
  T.unsafe(self).branch(repo, branch).commit.id
end

#fetch_default_branch(repo) ⇒ Object



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

def fetch_default_branch(repo)
  T.unsafe(self).project(repo).default_branch
end

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

Returns:

  • (Boolean)


143
144
145
# File 'lib/dependabot/clients/gitlab_with_retries.rb', line 143

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

#retry_connection_failures(&_blk) ⇒ Object



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

def retry_connection_failures(&_blk)
  retry_attempt = 0

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