Class: Gitlab::Triage::GraphqlQueries::QueryBuilder

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

Constant Summary collapse

BASE_QUERY =
<<~GRAPHQL
  query(%{resource_declarations}) {
    %{source_type}(fullPath: $source) {
      id
      %{resource_type}(after: $after%{resource_query}) {
        pageInfo {
          hasNextPage
          endCursor
        }
        nodes {
          id iid title updatedAt createdAt webUrl projectId %{resource_fields}
        }
      }
    }
  }
GRAPHQL

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source_type, resource_type, conditions, graphql_only: false) ⇒ QueryBuilder

Returns a new instance of QueryBuilder.



11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/gitlab/triage/graphql_queries/query_builder.rb', line 11

def initialize(source_type, resource_type, conditions, graphql_only: false)
  @source_type = source_type.to_s.singularize
  @resource_type = resource_type
  @conditions = conditions
  @graphql_only = graphql_only
  @resource_declarations = [
    '$source: ID!',
    '$after: String'
  ]

  has_any_iids = conditions.each_key.find { |key| key.to_s == 'iids' }
  @resource_declarations << '$iids: [String!]' if has_any_iids
end

Instance Attribute Details

#conditionsObject (readonly, private)

Returns the value of attribute conditions.



46
47
48
# File 'lib/gitlab/triage/graphql_queries/query_builder.rb', line 46

def conditions
  @conditions
end

#graphql_onlyObject (readonly, private)

Returns the value of attribute graphql_only.



46
47
48
# File 'lib/gitlab/triage/graphql_queries/query_builder.rb', line 46

def graphql_only
  @graphql_only
end

#resource_declarationsObject (readonly, private)

Returns the value of attribute resource_declarations.



46
47
48
# File 'lib/gitlab/triage/graphql_queries/query_builder.rb', line 46

def resource_declarations
  @resource_declarations
end

#resource_typeObject (readonly, private)

Returns the value of attribute resource_type.



46
47
48
# File 'lib/gitlab/triage/graphql_queries/query_builder.rb', line 46

def resource_type
  @resource_type
end

#source_typeObject (readonly, private)

Returns the value of attribute source_type.



46
47
48
# File 'lib/gitlab/triage/graphql_queries/query_builder.rb', line 46

def source_type
  @source_type
end

Instance Method Details

#issues_label_query(condition, condition_params) ⇒ Object (private)



115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/gitlab/triage/graphql_queries/query_builder.rb', line 115

def issues_label_query(condition, condition_params)
  args =
    case condition.to_s
    when 'forbidden_labels'
      ['labelName', condition_params, { negated: true }]
    when 'labels'
      ['labelName', condition_params, {}]
    else
      return nil
    end

  QueryParamBuilders::ArrayParamBuilder.new(*args[0...-1], **args.last)
end

#issues_type_query(condition, condition_params) ⇒ Object (private)



129
130
131
132
133
# File 'lib/gitlab/triage/graphql_queries/query_builder.rb', line 129

def issues_type_query(condition, condition_params)
  return unless condition.to_s == 'issue_type'

  QueryParamBuilders::ArrayParamBuilder.new('types', [condition_params.upcase], with_quotes: false)
end

#merge_requests_label_query(condition, condition_params) ⇒ Object (private)



151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/gitlab/triage/graphql_queries/query_builder.rb', line 151

def merge_requests_label_query(condition, condition_params)
  args =
    case condition.to_s
    when 'forbidden_labels'
      ['labels', condition_params, { negated: true }]
    when 'labels'
      ['labelName', condition_params, {}]
    else
      return nil
    end

  QueryParamBuilders::ArrayParamBuilder.new(*args[0...-1], **args.last)
end

#merge_requests_resource_query(condition, condition_params) ⇒ Object (private)



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/gitlab/triage/graphql_queries/query_builder.rb', line 135

def merge_requests_resource_query(condition, condition_params)
  args =
    case condition.to_s
    when 'source_branch'
      ['sourceBranches', condition_params, {}]
    when 'target_branch'
      ['targetBranches', condition_params, {}]
    when 'draft'
      ['draft', condition_params, { with_quotes: false }]
    else
      return nil
    end

  QueryParamBuilders::BaseParamBuilder.new(*args[0...-1], **args.last)
end

#queryObject



29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/gitlab/triage/graphql_queries/query_builder.rb', line 29

def query
  return if resource_fields.empty?

  format(
    BASE_QUERY,
    source_type: source_type,
    resource_type: resource_type.to_s.camelize(:lower),
    resource_fields: resource_fields.join(' '),
    resource_query: resource_query,
    resource_declarations: resource_declarations.join(', ')
  )
end

#resource_fieldsObject (private)



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/gitlab/triage/graphql_queries/query_builder.rb', line 69

def resource_fields
  fields = []

  fields << 'userNotesCount' if conditions.dig(:discussions, :attribute).to_s == 'notes'
  fields << 'userDiscussionsCount' if conditions.dig(:discussions, :attribute).to_s == 'threads'

  if graphql_only
    fields << 'labels { nodes { title } }'
    fields << 'author { id name username }'
    fields << 'assignees { nodes { id name username } }' if conditions.key?(:assignee_member)
    fields << 'upvotes' if vote_attribute == 'upvotes'
    fields << 'downvotes' if vote_attribute == 'downvotes'
    fields.push('draft', 'mergedAt') if resource_type == 'merge_requests'
  end

  fields
end

#resource_pathObject



25
26
27
# File 'lib/gitlab/triage/graphql_queries/query_builder.rb', line 25

def resource_path
  [source_type, resource_type]
end

#resource_queryObject (private)



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/gitlab/triage/graphql_queries/query_builder.rb', line 87

def resource_query
  condition_queries = []

  condition_queries << QueryParamBuilders::BaseParamBuilder.new('includeSubgroups', true, with_quotes: false) if source_type == 'group'

  conditions.each do |condition, condition_params|
    condition_queries << QueryParamBuilders::DateParamBuilder.new(condition_params) if condition.to_s == 'date'
    condition_queries << QueryParamBuilders::BaseParamBuilder.new('authorUsername', condition_params) if condition.to_s == 'author_username'
    condition_queries << QueryParamBuilders::BaseParamBuilder.new('milestoneTitle', condition_params) if condition.to_s == 'milestone'
    condition_queries << QueryParamBuilders::BaseParamBuilder.new('state', condition_params, with_quotes: false) if condition.to_s == 'state'
    condition_queries << QueryParamBuilders::BaseParamBuilder.new('iids', '$iids', with_quotes: false) if condition.to_s == 'iids'

    case resource_type
    when 'issues'
      condition_queries << issues_label_query(condition, condition_params)
      condition_queries << issues_type_query(condition, condition_params)
    when 'merge_requests'
      condition_queries << merge_requests_label_query(condition, condition_params)
      condition_queries << merge_requests_resource_query(condition, condition_params)
    end
  end

  condition_queries
    .compact
    .map(&:build_param)
    .join
end

#vote_attributeObject (private)



65
66
67
# File 'lib/gitlab/triage/graphql_queries/query_builder.rb', line 65

def vote_attribute
  @vote_attribute ||= (conditions.dig(:votes, :attribute) || conditions.dig(:upvotes, :attribute)).to_s
end