summaryrefslogtreecommitdiff
path: root/app/controllers/projects/deployments_controller.rb
blob: bebade1b21bb5f3999109e50961d428158569636 (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
# frozen_string_literal: true

class Projects::DeploymentsController < Projects::ApplicationController
  before_action :authorize_read_deployment!

  feature_category :continuous_delivery
  urgency :low

  # rubocop: disable CodeReuse/ActiveRecord
  def index
    deployments = environment.deployments.reorder(created_at: :desc)
    deployments = deployments.where('created_at > ?', params[:after].to_time) if params[:after]&.to_time

    render json: { deployments: DeploymentSerializer.new(project: project)
                                  .represent_concise(deployments) }
  end
  # rubocop: enable CodeReuse/ActiveRecord

  def metrics
    return render_404 unless deployment_metrics.has_metrics?

    @metrics = deployment_metrics.metrics
    if @metrics&.any?
      render json: @metrics, status: :ok
    else
      head :no_content
    end
  end

  def additional_metrics
    return render_404 unless deployment_metrics.has_metrics?

    respond_to do |format|
      format.json do
        metrics = deployment_metrics.additional_metrics

        if metrics.any?
          render json: metrics
        else
          head :no_content
        end
      end
    end
  end

  private

  def deployment_metrics
    @deployment_metrics ||= DeploymentMetrics.new(deployment.project, deployment)
  end

  def deployment
    @deployment ||= environment.deployments.find_successful_deployment!(params[:id])
  end

  def environment
    @environment ||= project.environments.find(params[:environment_id])
  end
end