Class: Dependabot::Clients::GitlabWithRetries

Inherits:
Object
  • Object
show all
Defined in:
lib/dependabot/clients/gitlab_with_retries.rb

Defined Under Namespace

Classes: ContentEncoding

Constant Summary collapse

RETRYABLE_ERRORS =
[Gitlab::Error::BadGateway].freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Proxying #



64
65
66
67
# File 'lib/dependabot/clients/gitlab_with_retries.rb', line 64

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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



87
88
89
90
91
92
93
94
95
96
# File 'lib/dependabot/clients/gitlab_with_retries.rb', line 87

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

Class Method Details

.for_gitlab_dot_com(credentials:) ⇒ Object



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

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

Constructor methods #



20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/dependabot/clients/gitlab_with_retries.rb', line 20

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) ⇒ Gitlab::ObjectifiedHash

Create commit in gitlab repo with correctly mapped file actions

Parameters:

Returns:

  • (Gitlab::ObjectifiedHash)


77
78
79
80
81
82
83
84
85
# File 'lib/dependabot/clients/gitlab_with_retries.rb', line 77

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

VCS Interface #



52
53
54
# File 'lib/dependabot/clients/gitlab_with_retries.rb', line 52

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

#fetch_default_branch(repo) ⇒ Object



56
57
58
# File 'lib/dependabot/clients/gitlab_with_retries.rb', line 56

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

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

Returns:

  • (Boolean)


98
99
100
# File 'lib/dependabot/clients/gitlab_with_retries.rb', line 98

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

#retry_connection_failuresObject



102
103
104
105
106
107
108
109
110
111
# File 'lib/dependabot/clients/gitlab_with_retries.rb', line 102

def retry_connection_failures
  retry_attempt = 0

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