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

environments_finder.rb « finders « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 29c00e4b2c28c2f0aa5b7fe9a6ad4b8c5ffab408 (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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# frozen_string_literal: true

class EnvironmentsFinder
  attr_reader :project, :current_user, :params

  def initialize(project, current_user, params = {})
    @project, @current_user, @params = project, current_user, params
  end

  # rubocop: disable CodeReuse/ActiveRecord
  def execute
    deployments = project.deployments
    deployments =
      if ref
        deployments_query = params[:with_tags] ? 'ref = :ref OR tag IS TRUE' : 'ref = :ref'
        deployments.where(deployments_query, ref: ref.to_s)
      elsif commit
        deployments.where(sha: commit.sha)
      else
        deployments.none
      end

    environment_ids = deployments
      .group(:environment_id)
      .select(:environment_id)

    environments = project.environments.available
      .where(id: environment_ids).order_by_last_deployed_at.to_a

    environments.select! do |environment|
      Ability.allowed?(current_user, :read_environment, environment)
    end

    if ref && commit
      environments.select! do |environment|
        environment.includes_commit?(commit)
      end
    end

    if ref && params[:recently_updated]
      environments.select! do |environment|
        environment.recently_updated_on_branch?(ref)
      end
    end

    environments
  end
  # rubocop: enable CodeReuse/ActiveRecord

  # This method will eventually take the place of `#execute` as an
  # efficient way to get relevant environment entries.
  # Currently, `#execute` method has a serious technical debt and
  # we will likely rework on it in the future.
  # See more https://gitlab.com/gitlab-org/gitlab-ce/issues/63381
  def find
    environments = project.environments
    environments = by_name(environments)
    environments = by_search(environments)

    environments
  end

  private

  def ref
    params[:ref].try(:to_s)
  end

  def commit
    params[:commit]
  end

  def by_name(environments)
    if params[:name].present?
      environments.for_name(params[:name])
    else
      environments
    end
  end

  def by_search(environments)
    if params[:search].present?
      environments.for_name_like(params[:search], limit: nil)
    else
      environments
    end
  end
end