Class: Dependabot::Clients::GitlabWithRetries

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

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 #



58
59
60
61
# File 'lib/dependabot/clients/gitlab_with_retries.rb', line 58

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



63
64
65
66
67
68
69
70
71
72
# File 'lib/dependabot/clients/gitlab_with_retries.rb', line 63

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



28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/dependabot/clients/gitlab_with_retries.rb', line 28

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 #



14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/dependabot/clients/gitlab_with_retries.rb', line 14

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

#fetch_commit(repo, branch) ⇒ Object

VCS Interface #



46
47
48
# File 'lib/dependabot/clients/gitlab_with_retries.rb', line 46

def fetch_commit(repo, branch)
  branch(repo, branch).commit.id
end

#fetch_default_branch(repo) ⇒ Object



50
51
52
# File 'lib/dependabot/clients/gitlab_with_retries.rb', line 50

def fetch_default_branch(repo)
  project(repo).default_branch
end

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

Returns:

  • (Boolean)


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

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

#retry_connection_failuresObject



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

def retry_connection_failures
  retry_attempt = 0

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