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.freeze
  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.



9
10
11
12
13
14
15
16
17
18
# File 'lib/gitlab/triage/graphql_queries/query_builder.rb', line 9

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'
  ]
end

Instance Attribute Details

#conditionsObject (readonly, private)

Returns the value of attribute conditions.



41
42
43
# File 'lib/gitlab/triage/graphql_queries/query_builder.rb', line 41

def conditions
  @conditions
end

#graphql_onlyObject (readonly, private)

Returns the value of attribute graphql_only.



41
42
43
# File 'lib/gitlab/triage/graphql_queries/query_builder.rb', line 41

def graphql_only
  @graphql_only
end

#resource_declarationsObject (readonly, private)

Returns the value of attribute resource_declarations.



41
42
43
# File 'lib/gitlab/triage/graphql_queries/query_builder.rb', line 41

def resource_declarations
  @resource_declarations
end

#resource_typeObject (readonly, private)

Returns the value of attribute resource_type.



41
42
43
# File 'lib/gitlab/triage/graphql_queries/query_builder.rb', line 41

def resource_type
  @resource_type
end

#source_typeObject (readonly, private)

Returns the value of attribute source_type.



41
42
43
# File 'lib/gitlab/triage/graphql_queries/query_builder.rb', line 41

def source_type
  @source_type
end

Instance Method Details

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



112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/gitlab/triage/graphql_queries/query_builder.rb', line 112

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::LabelsParamBuilder.new(*args[0...-1], **args.last)
end

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



142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/gitlab/triage/graphql_queries/query_builder.rb', line 142

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

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

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



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/gitlab/triage/graphql_queries/query_builder.rb', line 126

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



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/gitlab/triage/graphql_queries/query_builder.rb', line 24

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)



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/gitlab/triage/graphql_queries/query_builder.rb', line 64

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



20
21
22
# File 'lib/gitlab/triage/graphql_queries/query_builder.rb', line 20

def resource_path
  [source_type, resource_type]
end

#resource_queryObject (private)



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

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('milestoneTitle', condition_params) if condition.to_s == 'milestone'
    condition_queries << QueryParamBuilders::BaseParamBuilder.new('state', condition_params, with_quotes: false) if condition.to_s == 'state'

    if condition.to_s == 'iids'
      @resource_declarations << '$iids: [String!]'
      condition_queries << QueryParamBuilders::BaseParamBuilder.new('iids', '$iids', with_quotes: false)
    end

    case resource_type
    when 'issues'
      condition_queries << issues_label_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)



60
61
62
# File 'lib/gitlab/triage/graphql_queries/query_builder.rb', line 60

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