Class: Gitlab::Triage::GraphqlNetwork

Inherits:
Object
  • Object
show all
Defined in:
lib/gitlab/triage/graphql_network.rb

Constant Summary collapse

MINIMUM_RATE_LIMIT =
25

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(adapter) ⇒ GraphqlNetwork

Returns a new instance of GraphqlNetwork.



18
19
20
21
# File 'lib/gitlab/triage/graphql_network.rb', line 18

def initialize(adapter)
  @adapter = adapter
  @options = adapter.options
end

Instance Attribute Details

#adapterObject (readonly)

Returns the value of attribute adapter.



14
15
16
# File 'lib/gitlab/triage/graphql_network.rb', line 14

def adapter
  @adapter
end

#optionsObject (readonly)

Returns the value of attribute options.



14
15
16
# File 'lib/gitlab/triage/graphql_network.rb', line 14

def options
  @options
end

Instance Method Details

#extract_id_from_global_id(global_id) ⇒ Object (private)



64
65
66
67
68
# File 'lib/gitlab/triage/graphql_network.rb', line 64

def extract_id_from_global_id(global_id)
  return if global_id.blank?

  GlobalID.parse(global_id).model_id.to_i
end

#normalize(resource) ⇒ Object (private)



54
55
56
57
58
59
60
61
62
# File 'lib/gitlab/triage/graphql_network.rb', line 54

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

#query(graphql_query, variables = {}) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/gitlab/triage/graphql_network.rb', line 23

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

#rate_limit_debug(response) ⇒ Object (private)



70
71
72
73
# File 'lib/gitlab/triage/graphql_network.rb', line 70

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) ⇒ Object (private)



75
76
77
78
79
80
# File 'lib/gitlab/triage/graphql_network.rb', line 75

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