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.



16
17
18
19
# File 'lib/gitlab/triage/graphql_network.rb', line 16

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

Instance Attribute Details

#adapterObject (readonly)

Returns the value of attribute adapter.



12
13
14
# File 'lib/gitlab/triage/graphql_network.rb', line 12

def adapter
  @adapter
end

#optionsObject (readonly)

Returns the value of attribute options.



12
13
14
# File 'lib/gitlab/triage/graphql_network.rb', line 12

def options
  @options
end

Instance Method Details

#extract_id_from_global_id(global_id) ⇒ Object (private)



62
63
64
65
66
# 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) ⇒ Object (private)



52
53
54
55
56
57
58
59
60
# 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

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



21
22
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
# 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

#rate_limit_debug(response) ⇒ Object (private)



68
69
70
71
# 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) ⇒ Object (private)



73
74
75
76
77
78
# 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