Welcome to mirror list, hosted at ThFree Co, Russian Federation.

group_variables_finder.rb « ci « finders « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: e4697b07e6424d30fd4a848a93de67d86972fc88 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# frozen_string_literal: true

module Ci
  class GroupVariablesFinder
    def initialize(project, sort_key = nil)
      @project = project
      @params = sort_to_params_map(sort_key)
    end

    def execute
      variables = ::Ci::GroupVariable.for_groups(project.group&.self_and_ancestor_ids)

      return Ci::GroupVariable.none if variables.empty?

      sort(variables)
    end

    private

    SORT_TO_PARAMS_MAP = {
      created_desc: { order_by: 'created_at', sort: 'desc' },
      created_asc: { order_by: 'created_at', sort: 'asc' },
      key_desc: { order_by: 'key', sort: 'desc' },
      key_asc: { order_by: 'key', sort: 'asc' }
    }.freeze

    def sort_to_params_map(sort_key)
      SORT_TO_PARAMS_MAP[sort_key] || {}
    end

    def sort(variables)
      return variables unless params[:order_by]

      variables.sort_by_attribute("#{params[:order_by]}_#{params[:sort]}")
    end

    attr_reader :project, :params
  end
end