Class: Dependabot::Clients::GithubWithRetries

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

Constant Summary collapse

DEFAULT_OPEN_TIMEOUT_IN_SECONDS =
2
DEFAULT_READ_TIMEOUT_IN_SECONDS =
5
ClientOptions =
T.type_alias { T::Hash[Symbol, Object] }
DEFAULT_CLIENT_ARGS =
T.let(
  {
    connection_options: {
      request: {
        open_timeout: open_timeout_in_seconds,
        timeout: read_timeout_in_seconds
      }
    }
  }.freeze,
  ClientOptions
)
RETRYABLE_ERRORS =
T.let(
  [
    Faraday::ConnectionFailed,
    Faraday::TimeoutError,
    Octokit::InternalServerError,
    Octokit::BadGateway
  ].freeze,
  T::Array[T.class_of(StandardError)]
)

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of GithubWithRetries.



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/dependabot/clients/github_with_retries.rb', line 119

def initialize(max_retries: 3, **args)
  args = DEFAULT_CLIENT_ARGS.merge(args)
  access_tokens = extract_access_tokens(args)

  # Explicitly set the proxy if one is set in the environment
  # as Faraday's find_proxy is very slow.
  Octokit.configure do |c|
    c.proxy = ENV["HTTPS_PROXY"] if ENV["HTTPS_PROXY"]
  end

  args[:middleware] = Faraday::RackBuilder.new do |builder|
    builder.use Faraday::Retry::Middleware, exceptions: RETRYABLE_ERRORS, max: max_retries || 3

    Octokit::Default::MIDDLEWARE.handlers.each do |handler|
      next if handler.klass == Faraday::Retry::Middleware

      builder.use handler.klass
    end
  end

  @clients = T.let(
    access_tokens.map do |token|
      Octokit::Client.new(args.merge(access_token: token))
    end,
    T::Array[Octokit::Client]
  )
  super(T.must(@clients.last))
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/dependabot/clients/github_with_retries.rb', line 157

def method_missing(method_name, *args, &block)
  original_args = args
  untried_clients = @clients.dup
  client = untried_clients.pop

  begin
    __setobj__(T.must(client))
    args = original_args.map(&:dup)
    super
  rescue Octokit::NotFound, Octokit::Unauthorized, Octokit::Forbidden
    Kernel.raise unless (client = untried_clients.pop)

    retry
  end
end

Class Method Details

.for_github_dot_com(credentials:) ⇒ Object



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

def self.for_github_dot_com(credentials:)
  access_tokens =
    credentials
    .select { |cred| cred["type"] == "git_source" }
    .select { |cred| cred["host"] == "github.com" }
    .select { |cred| cred["password"] }
    .map { |cred| cred.fetch("password") }

  new(access_tokens: access_tokens)
end

.for_source(source:, credentials:) ⇒ Object



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

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

  new(
    access_tokens: access_tokens,
    api_endpoint: source.api_endpoint
  )
end

.open_timeout_in_secondsObject



19
20
21
# File 'lib/dependabot/clients/github_with_retries.rb', line 19

def self.open_timeout_in_seconds
  ENV.fetch("DEPENDABOT_OPEN_TIMEOUT_IN_SECONDS", DEFAULT_OPEN_TIMEOUT_IN_SECONDS).to_i
end

.read_timeout_in_secondsObject



24
25
26
# File 'lib/dependabot/clients/github_with_retries.rb', line 24

def self.read_timeout_in_seconds
  ENV.fetch("DEPENDABOT_READ_TIMEOUT_IN_SECONDS", DEFAULT_READ_TIMEOUT_IN_SECONDS).to_i
end

Instance Method Details

#fetch_commit(repo, branch) ⇒ Object



92
93
94
95
96
97
98
99
# File 'lib/dependabot/clients/github_with_retries.rb', line 92

def fetch_commit(repo, branch)
  response = ref(repo, "heads/#{branch}")

  Kernel.raise Octokit::NotFound if response.is_a?(Array)

  object = T.cast(response[:object], Sawyer::Resource)
  T.cast(object[:sha], String)
end

#fetch_default_branch(repo) ⇒ Object



102
103
104
# File 'lib/dependabot/clients/github_with_retries.rb', line 102

def fetch_default_branch(repo)
  T.cast(repository(repo)[:default_branch], String)
end

#raw_contents(repo, path:, ref:) ⇒ Object



107
108
109
110
111
112
# File 'lib/dependabot/clients/github_with_retries.rb', line 107

def raw_contents(repo, path:, ref:)
  T.cast(
    contents(repo, path: path, ref: ref, accept: "application/vnd.github.v3.raw"),
    String
  )
end

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

Returns:

  • (Boolean)


180
181
182
# File 'lib/dependabot/clients/github_with_retries.rb', line 180

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