class Gitlab::Triage::GraphqlNetwork

Constants

MINIMUM_RATE_LIMIT

Attributes

adapter[R]
options[R]

Public Class Methods

new(adapter) click to toggle source
# File lib/gitlab/triage/graphql_network.rb, line 16
def initialize(adapter)
  @adapter = adapter
  @options = adapter.options
end

Public Instance Methods

query(graphql_query, variables = {}) click to toggle source
# File lib/gitlab/triage/graphql_network.rb, line 21
def query(graphql_query, variables = {})
  return if graphql_query.blank?

  response = {}
  resources = []

  parsed_graphql_query = adapter.parse(graphql_query.query)

  begin
    print '.'

    response = adapter.query(
      parsed_graphql_query,
      resource_path: graphql_query.resource_path,
      variables: variables.merge(after: response.delete(:end_cursor))
    )

    rate_limit_debug(response) if options.debug
    rate_limit_wait(response)

    resources.concat(Array.wrap(response.delete(:results)))
  end while response.delete(:more_pages)

  resources
    .map { |resource| resource.deep_transform_keys(&:underscore) }
    .map(&:with_indifferent_access)
    .map { |resource| normalize(resource) }
end

Private Instance Methods

extract_id_from_global_id(global_id) click to toggle source
# File lib/gitlab/triage/graphql_network.rb, line 62
def extract_id_from_global_id(global_id)
  return if global_id.blank?

  GlobalID.parse(global_id).model_id.to_i
end
normalize(resource) click to toggle source
# File lib/gitlab/triage/graphql_network.rb, line 52
def normalize(resource)
  resource
    .slice(:iid, :title, :state, :author, :merged_at, :user_notes_count, :user_discussions_count, :upvotes, :downvotes, :project_id, :web_url)
    .merge(
      id: extract_id_from_global_id(resource[:id]),
      labels: [*resource.dig(:labels, :nodes)].pluck(:title),
      assignees: [*resource.dig(:assignees, :nodes)]
    )
end
rate_limit_debug(response) click to toggle source
# File lib/gitlab/triage/graphql_network.rb, line 68
def rate_limit_debug(response)
  rate_limit_infos = "Rate limit remaining: #{response[:ratelimit_remaining]} (reset at #{response[:ratelimit_reset_at]})"
  puts Gitlab::Triage::UI.debug "rate_limit_infos: #{rate_limit_infos}"
end
rate_limit_wait(response) click to toggle source
# File lib/gitlab/triage/graphql_network.rb, line 73
def rate_limit_wait(response)
  return unless response.delete(:ratelimit_remaining) < MINIMUM_RATE_LIMIT

  puts Gitlab::Triage::UI.debug "Rate limit almost exceeded, sleeping for #{response[:ratelimit_reset_at] - Time.now} seconds" if options.debug
  sleep(1) until Time.now >= response[:ratelimit_reset_at]
end