debian-mirror-gitlab/app/controllers/projects/pipelines_controller.rb

329 lines
9.5 KiB
Ruby
Raw Normal View History

2018-12-05 23:21:45 +05:30
# frozen_string_literal: true
2016-06-02 11:05:42 +05:30
class Projects::PipelinesController < Projects::ApplicationController
2019-12-21 20:55:43 +05:30
include ::Gitlab::Utils::StrongMemoize
2021-09-30 23:02:18 +05:30
include RedisTracking
2019-12-21 20:55:43 +05:30
2021-04-29 21:17:54 +05:30
before_action :disable_query_limiting, only: [:create, :retry]
2021-01-03 14:25:43 +05:30
before_action :pipeline, except: [:index, :new, :create, :charts, :config_variables]
2019-12-04 20:38:33 +05:30
before_action :set_pipeline_path, only: [:show]
2016-06-02 11:05:42 +05:30
before_action :authorize_read_pipeline!
2021-08-04 16:29:09 +05:30
before_action :authorize_read_build!, only: [:index, :show]
before_action :authorize_read_ci_cd_analytics!, only: [:charts]
2021-01-03 14:25:43 +05:30
before_action :authorize_create_pipeline!, only: [:new, :create, :config_variables]
2016-06-02 11:05:42 +05:30
before_action :authorize_update_pipeline!, only: [:retry, :cancel]
2021-10-27 15:23:28 +05:30
before_action :ensure_pipeline, only: [:show, :downloadable_artifacts]
2020-07-28 23:09:34 +05:30
# Will be removed with https://gitlab.com/gitlab-org/gitlab/-/issues/225596
before_action :redirect_for_legacy_scope_filter, only: [:index], if: -> { request.format.html? }
2019-07-07 11:18:12 +05:30
around_action :allow_gitaly_ref_name_caching, only: [:index, :show]
2021-09-30 23:02:18 +05:30
track_redis_hll_event :charts, name: 'p_analytics_pipelines'
2020-07-28 23:09:34 +05:30
2017-08-17 22:00:37 +05:30
wrap_parameters Ci::Pipeline
POLLING_INTERVAL = 10_000
2016-06-02 11:05:42 +05:30
2021-06-08 01:23:25 +05:30
feature_category :continuous_integration, [
:charts, :show, :config_variables, :stage, :cancel, :retry,
2021-10-27 15:23:28 +05:30
:builds, :dag, :failures, :status,
2021-06-08 01:23:25 +05:30
:index, :create, :new, :destroy
]
feature_category :code_testing, [:test_report]
2021-10-27 15:23:28 +05:30
feature_category :build_artifacts, [:downloadable_artifacts]
2021-01-03 14:25:43 +05:30
2016-06-02 11:05:42 +05:30
def index
2020-04-08 14:13:33 +05:30
@pipelines = Ci::PipelinesFinder
2020-05-24 23:13:21 +05:30
.new(project, current_user, index_params)
2017-08-17 22:00:37 +05:30
.execute
.page(params[:page])
2018-11-08 19:23:39 +05:30
@pipelines_count = limited_pipelines_count(project)
2018-03-17 18:26:18 +05:30
2017-08-17 22:00:37 +05:30
respond_to do |format|
2021-04-29 21:17:54 +05:30
format.html do
2021-09-04 01:27:46 +05:30
enable_code_quality_walkthrough_experiment
enable_ci_runner_templates_experiment
2021-04-29 21:17:54 +05:30
end
2017-08-17 22:00:37 +05:30
format.json do
Gitlab::PollingInterval.set_header(response, interval: POLLING_INTERVAL)
render json: {
2019-07-07 11:18:12 +05:30
pipelines: serialize_pipelines,
2017-08-17 22:00:37 +05:30
count: {
2020-07-28 23:09:34 +05:30
all: @pipelines_count
2017-08-17 22:00:37 +05:30
}
}
end
end
2016-06-02 11:05:42 +05:30
end
def new
2019-02-15 15:39:39 +05:30
@pipeline = project.all_pipelines.new(ref: @project.default_branch)
2016-06-02 11:05:42 +05:30
end
def create
2021-10-27 15:23:28 +05:30
service_response = Ci::CreatePipelineService
2017-08-17 22:00:37 +05:30
.new(project, current_user, create_params)
2017-09-10 17:25:29 +05:30
.execute(:web, ignore_skip_ci: true, save_on_errors: false)
2017-08-17 22:00:37 +05:30
2021-10-27 15:23:28 +05:30
@pipeline = service_response.payload
2020-11-24 15:15:51 +05:30
respond_to do |format|
format.html do
2021-10-27 15:23:28 +05:30
if service_response.success?
2020-11-24 15:15:51 +05:30
redirect_to project_pipeline_path(project, @pipeline)
else
render 'new', status: :bad_request
end
end
format.json do
2021-10-27 15:23:28 +05:30
if service_response.success?
2020-11-24 15:15:51 +05:30
render json: PipelineSerializer
.new(project: project, current_user: current_user)
.represent(@pipeline),
status: :created
else
render json: { errors: @pipeline.error_messages.map(&:content),
warnings: @pipeline.warning_messages(limit: ::Gitlab::Ci::Warnings::MAX_LIMIT).map(&:content),
total_warnings: @pipeline.warning_messages.length },
status: :bad_request
end
end
2016-06-02 11:05:42 +05:30
end
end
def show
2021-04-29 21:17:54 +05:30
Gitlab::QueryLimiting.disable!('https://gitlab.com/gitlab-org/gitlab/-/issues/26657')
2020-05-24 23:13:21 +05:30
2017-08-17 22:00:37 +05:30
respond_to do |format|
2021-04-29 21:17:54 +05:30
format.html { render_show }
2017-08-17 22:00:37 +05:30
format.json do
Gitlab::PollingInterval.set_header(response, interval: POLLING_INTERVAL)
render json: PipelineSerializer
.new(project: @project, current_user: @current_user)
2019-03-02 22:35:43 +05:30
.represent(@pipeline, show_represent_params)
2017-08-17 22:00:37 +05:30
end
end
end
2020-03-13 15:44:24 +05:30
def destroy
::Ci::DestroyPipelineService.new(project, current_user).execute(pipeline)
redirect_to project_pipelines_path(project), status: :see_other
end
2017-08-17 22:00:37 +05:30
def builds
render_show
end
2020-05-24 23:13:21 +05:30
def dag
2020-06-23 00:09:42 +05:30
respond_to do |format|
format.html { render_show }
format.json do
render json: Ci::DagPipelineSerializer
.new(project: @project, current_user: @current_user)
.represent(@pipeline)
end
end
2020-05-24 23:13:21 +05:30
end
2017-08-17 22:00:37 +05:30
def failures
2018-10-15 14:42:47 +05:30
if @pipeline.failed_builds.present?
2017-08-17 22:00:37 +05:30
render_show
else
redirect_to pipeline_path(@pipeline)
end
end
def status
render json: PipelineSerializer
.new(project: @project, current_user: @current_user)
.represent_status(@pipeline)
end
def stage
2017-09-10 17:25:29 +05:30
@stage = pipeline.legacy_stage(params[:stage])
2017-08-17 22:00:37 +05:30
return not_found unless @stage
2018-10-15 14:42:47 +05:30
render json: StageSerializer
.new(project: @project, current_user: @current_user)
2018-12-05 23:21:45 +05:30
.represent(@stage, details: true, retried: params[:retried])
2018-10-15 14:42:47 +05:30
end
2016-06-02 11:05:42 +05:30
def retry
2021-09-04 01:27:46 +05:30
::Ci::RetryPipelineWorker.perform_async(pipeline.id, current_user.id) # rubocop:disable CodeReuse/Worker
2016-06-02 11:05:42 +05:30
2017-08-17 22:00:37 +05:30
respond_to do |format|
format.html do
2017-09-10 17:25:29 +05:30
redirect_back_or_default default: project_pipelines_path(project)
2017-08-17 22:00:37 +05:30
end
format.json { head :no_content }
end
2016-06-02 11:05:42 +05:30
end
def cancel
pipeline.cancel_running
2017-08-17 22:00:37 +05:30
respond_to do |format|
format.html do
2017-09-10 17:25:29 +05:30
redirect_back_or_default default: project_pipelines_path(project)
2017-08-17 22:00:37 +05:30
end
format.json { head :no_content }
end
end
2019-12-21 20:55:43 +05:30
def test_report
2019-12-26 22:10:19 +05:30
respond_to do |format|
2021-04-29 21:17:54 +05:30
format.html { render_show }
2019-12-26 22:10:19 +05:30
format.json do
2020-05-24 23:13:21 +05:30
render json: TestReportSerializer
.new(current_user: @current_user)
2020-07-28 23:09:34 +05:30
.represent(pipeline_test_report, project: project, details: true)
2019-12-26 22:10:19 +05:30
end
end
2019-12-21 20:55:43 +05:30
end
2021-01-03 14:25:43 +05:30
def config_variables
respond_to do |format|
format.json do
2021-11-11 11:23:49 +05:30
project = @project.uses_external_project_ci_config? ? @project.ci_config_external_project : @project
result = Ci::ListConfigVariablesService.new(project, current_user).execute(params[:sha])
2021-02-22 17:27:13 +05:30
result.nil? ? head(:no_content) : render(json: result)
2021-01-03 14:25:43 +05:30
end
end
end
2021-06-08 01:23:25 +05:30
def downloadable_artifacts
render json: Ci::DownloadableArtifactSerializer.new(
project: project,
current_user: current_user
).represent(@pipeline)
end
2016-06-02 11:05:42 +05:30
private
2019-07-07 11:18:12 +05:30
def serialize_pipelines
PipelineSerializer
.new(project: @project, current_user: @current_user)
.with_pagination(request, response)
2021-06-08 01:23:25 +05:30
.represent(@pipelines, disable_coverage: true, preload: true, code_quality_walkthrough: params[:code_quality_walkthrough].present?)
2019-07-07 11:18:12 +05:30
end
2017-08-17 22:00:37 +05:30
def render_show
2021-06-02 17:11:27 +05:30
@stages = @pipeline.stages
2021-04-29 21:17:54 +05:30
2017-08-17 22:00:37 +05:30
respond_to do |format|
format.html do
render 'show'
end
end
end
2019-03-02 22:35:43 +05:30
def show_represent_params
2019-12-21 20:55:43 +05:30
{ grouped: true, expanded: params[:expanded].to_a.map(&:to_i) }
2019-03-02 22:35:43 +05:30
end
2016-06-02 11:05:42 +05:30
def create_params
2019-07-31 22:56:46 +05:30
params.require(:pipeline).permit(:ref, variables_attributes: %i[key variable_type secret_value])
2016-06-02 11:05:42 +05:30
end
2020-04-22 19:07:51 +05:30
def ensure_pipeline
render_404 unless pipeline
end
2020-07-28 23:09:34 +05:30
def redirect_for_legacy_scope_filter
return unless %w[running pending].include?(params[:scope])
redirect_to url_for(safe_params.except(:scope).merge(status: safe_params[:scope])), status: :moved_permanently
end
2018-12-05 23:21:45 +05:30
# rubocop: disable CodeReuse/ActiveRecord
2016-06-02 11:05:42 +05:30
def pipeline
2019-12-04 20:38:33 +05:30
@pipeline ||= if params[:id].blank? && params[:latest]
latest_pipeline
else
project
.all_pipelines
.includes(builds: :tags, user: :status)
.find_by!(id: params[:id])
.present(current_user: current_user)
end
2016-06-02 11:05:42 +05:30
end
2018-12-05 23:21:45 +05:30
# rubocop: enable CodeReuse/ActiveRecord
2018-03-17 18:26:18 +05:30
2019-12-04 20:38:33 +05:30
def set_pipeline_path
@pipeline_path ||= if params[:id].blank? && params[:latest]
latest_project_pipelines_path(@project, params['ref'])
else
project_pipeline_path(@project, @pipeline)
end
end
def latest_pipeline
2020-11-24 15:15:51 +05:30
@project.latest_pipeline(params['ref'])
2019-12-04 20:38:33 +05:30
&.present(current_user: current_user)
end
2021-04-29 21:17:54 +05:30
def disable_query_limiting
# Also see https://gitlab.com/gitlab-org/gitlab/-/issues/20785
Gitlab::QueryLimiting.disable!('https://gitlab.com/gitlab-org/gitlab/-/issues/20784')
2018-03-17 18:26:18 +05:30
end
2018-11-08 19:23:39 +05:30
def authorize_update_pipeline!
return access_denied! unless can?(current_user, :update_pipeline, @pipeline)
end
def limited_pipelines_count(project, scope = nil)
2020-05-24 23:13:21 +05:30
finder = Ci::PipelinesFinder.new(project, current_user, index_params.merge(scope: scope))
2018-11-08 19:23:39 +05:30
view_context.limited_counter_with_delimiter(finder.execute)
end
2019-12-21 20:55:43 +05:30
def pipeline_test_report
strong_memoize(:pipeline_test_report) do
2020-05-24 23:13:21 +05:30
@pipeline.test_reports.tap do |reports|
reports.with_attachment! if params[:scope] == 'with_attachment'
end
2019-12-21 20:55:43 +05:30
end
end
2020-05-24 23:13:21 +05:30
def index_params
2021-11-11 11:23:49 +05:30
params.permit(:scope, :username, :ref, :status, :source)
2020-05-24 23:13:21 +05:30
end
2021-09-04 01:27:46 +05:30
def enable_code_quality_walkthrough_experiment
experiment(:code_quality_walkthrough, namespace: project.root_ancestor) do |e|
e.exclude! unless current_user
e.exclude! unless can?(current_user, :create_pipeline, project)
e.exclude! unless project.root_ancestor.recent?
e.exclude! if @pipelines_count.to_i > 0
e.exclude! if helpers.has_gitlab_ci?(project)
e.control {}
e.candidate {}
e.record!
end
end
def enable_ci_runner_templates_experiment
experiment(:ci_runner_templates, namespace: project.root_ancestor) do |e|
e.exclude! unless current_user
e.exclude! unless can?(current_user, :create_pipeline, project)
e.exclude! if @pipelines_count.to_i > 0
e.exclude! if helpers.has_gitlab_ci?(project)
e.control {}
e.candidate {}
e.record!
end
end
2016-06-02 11:05:42 +05:30
end
2019-12-04 20:38:33 +05:30
2021-06-08 01:23:25 +05:30
Projects::PipelinesController.prepend_mod_with('Projects::PipelinesController')