2018-12-05 23:21:45 +05:30
# frozen_string_literal: true
2017-08-17 22:00:37 +05:30
module Projects
module Settings
class CiCdController < Projects :: ApplicationController
before_action :authorize_admin_pipeline!
2018-05-09 12:01:36 +05:30
before_action :define_variables
2020-04-08 14:13:33 +05:30
before_action do
push_frontend_feature_flag ( :new_variables_ui , @project , default_enabled : true )
2020-04-22 19:07:51 +05:30
push_frontend_feature_flag ( :ajax_new_deploy_token , @project )
push_frontend_feature_flag ( :ci_key_autocomplete , default_enabled : true )
2020-04-08 14:13:33 +05:30
end
2017-08-17 22:00:37 +05:30
def show
2018-05-09 12:01:36 +05:30
end
def update
Projects :: UpdateService . new ( project , current_user , update_params ) . tap do | service |
result = service . execute
if result [ :status ] == :success
2020-01-01 13:55:28 +05:30
flash [ :toast ] = _ ( " Pipelines settings for '%{project_name}' were successfully updated. " ) % { project_name : @project . name }
2018-05-09 12:01:36 +05:30
run_autodevops_pipeline ( service )
redirect_to project_settings_ci_cd_path ( @project )
else
2019-07-07 11:18:12 +05:30
redirect_to project_settings_ci_cd_path ( @project ) , alert : result [ :message ]
2018-05-09 12:01:36 +05:30
end
end
2018-03-17 18:26:18 +05:30
end
def reset_cache
if ResetProjectCacheService . new ( @project , current_user ) . execute
2018-05-09 12:01:36 +05:30
respond_to do | format |
format . json { head :ok }
end
2018-03-17 18:26:18 +05:30
else
2018-05-09 12:01:36 +05:30
respond_to do | format |
format . json { head :bad_request }
end
2018-03-17 18:26:18 +05:30
end
2017-08-17 22:00:37 +05:30
end
2018-12-05 23:21:45 +05:30
def reset_registration_token
@project . reset_runners_token!
2020-01-01 13:55:28 +05:30
flash [ :toast ] = _ ( " New runners registration token has been generated! " )
2018-12-05 23:21:45 +05:30
redirect_to namespace_project_settings_ci_cd_path
end
2020-04-08 14:13:33 +05:30
def create_deploy_token
2020-04-22 19:07:51 +05:30
result = Projects :: DeployTokens :: CreateService . new ( @project , current_user , deploy_token_params ) . execute
@new_deploy_token = result [ :deploy_token ]
2020-04-08 14:13:33 +05:30
2020-04-22 19:07:51 +05:30
if result [ :status ] == :success
respond_to do | format |
format . json do
# IMPORTANT: It's a security risk to expose the token value more than just once here!
json = API :: Entities :: DeployTokenWithToken . represent ( @new_deploy_token ) . as_json
render json : json , status : result [ :http_status ]
end
format . html do
flash . now [ :notice ] = s_ ( 'DeployTokens|Your new project deploy token has been created.' )
render :show
end
end
else
respond_to do | format |
format . json { render json : { message : result [ :message ] } , status : result [ :http_status ] }
format . html do
flash . now [ :alert ] = result [ :message ]
render :show
end
end
2020-04-08 14:13:33 +05:30
end
end
2017-08-17 22:00:37 +05:30
private
2018-05-09 12:01:36 +05:30
def update_params
2019-12-21 20:55:43 +05:30
params . require ( :project ) . permit ( * permitted_project_params )
end
def permitted_project_params
[
2018-05-09 12:01:36 +05:30
:runners_token , :builds_enabled , :build_allow_git_fetch ,
:build_timeout_human_readable , :build_coverage_regex , :public_builds ,
2020-04-08 14:13:33 +05:30
:auto_cancel_pending_pipelines , :forward_deployment_enabled , :ci_config_path ,
2019-09-04 21:01:54 +05:30
auto_devops_attributes : [ :id , :domain , :enabled , :deploy_strategy ] ,
ci_cd_settings_attributes : [ :default_git_depth ]
2019-12-21 20:55:43 +05:30
] . tap do | list |
list << :max_artifacts_size if can? ( current_user , :update_max_artifacts_size , project )
end
2018-05-09 12:01:36 +05:30
end
2020-04-08 14:13:33 +05:30
def deploy_token_params
2020-04-22 19:07:51 +05:30
params . require ( :deploy_token ) . permit ( :name , :expires_at , :read_repository , :read_registry , :write_registry , :username )
2020-04-08 14:13:33 +05:30
end
2018-05-09 12:01:36 +05:30
def run_autodevops_pipeline ( service )
return unless service . run_auto_devops_pipeline?
if @project . empty_repo?
2020-01-01 13:55:28 +05:30
flash [ :notice ] = _ ( " This repository is currently empty. A new Auto DevOps pipeline will be created after a new file has been pushed to a branch. " )
2018-05-09 12:01:36 +05:30
return
end
2020-03-13 15:44:24 +05:30
# rubocop:disable CodeReuse/Worker
2018-05-09 12:01:36 +05:30
CreatePipelineWorker . perform_async ( project . id , current_user . id , project . default_branch , :web , ignore_skip_ci : true , save_on_errors : false )
2020-03-13 15:44:24 +05:30
# rubocop:enable CodeReuse/Worker
2020-01-01 13:55:28 +05:30
pipelines_link_start = '<a href="%{url}">' . html_safe % { url : project_pipelines_path ( @project ) }
flash [ :toast ] = _ ( " A new Auto DevOps pipeline has been created, go to %{pipelines_link_start}Pipelines page%{pipelines_link_end} for details " ) % { pipelines_link_start : pipelines_link_start , pipelines_link_end : " </a> " . html_safe }
2018-05-09 12:01:36 +05:30
end
def define_variables
define_runners_variables
2018-12-13 13:39:08 +05:30
define_ci_variables
2020-04-08 14:13:33 +05:30
define_deploy_token_variables
2018-05-09 12:01:36 +05:30
define_triggers_variables
define_badges_variables
define_auto_devops_variables
2020-04-08 14:13:33 +05:30
define_deploy_keys
2018-05-09 12:01:36 +05:30
end
2017-08-17 22:00:37 +05:30
def define_runners_variables
@project_runners = @project . runners . ordered
2018-10-15 14:42:47 +05:30
@assignable_runners = current_user
2018-11-08 19:23:39 +05:30
. ci_owned_runners
2018-10-15 14:42:47 +05:30
. assignable_for ( project )
. ordered
. page ( params [ :page ] ) . per ( 20 )
2018-11-08 19:23:39 +05:30
@shared_runners = :: Ci :: Runner . instance_type . active
2018-10-15 14:42:47 +05:30
2017-08-17 22:00:37 +05:30
@shared_runners_count = @shared_runners . count ( :all )
2018-10-15 14:42:47 +05:30
@group_runners = :: Ci :: Runner . belonging_to_parent_group_of_project ( @project . id )
2017-08-17 22:00:37 +05:30
end
2018-12-13 13:39:08 +05:30
def define_ci_variables
2018-05-09 12:01:36 +05:30
@variable = :: Ci :: Variable . new ( project : project )
2017-09-10 17:25:29 +05:30
. present ( current_user : current_user )
@variables = project . variables . order_key_asc
. map { | variable | variable . present ( current_user : current_user ) }
2017-08-17 22:00:37 +05:30
end
def define_triggers_variables
@triggers = @project . triggers
2019-02-02 18:00:53 +05:30
. present ( current_user : current_user )
2018-05-09 12:01:36 +05:30
@trigger = :: Ci :: Trigger . new
2019-02-02 18:00:53 +05:30
. present ( current_user : current_user )
2017-08-17 22:00:37 +05:30
end
def define_badges_variables
@ref = params [ :ref ] || @project . default_branch || 'master'
2017-09-10 17:25:29 +05:30
@badges = [ Gitlab :: Badge :: Pipeline :: Status ,
2017-08-17 22:00:37 +05:30
Gitlab :: Badge :: Coverage :: Report ]
@badges . map! do | badge |
badge . new ( @project , @ref ) . metadata
end
end
2018-03-17 18:26:18 +05:30
def define_auto_devops_variables
@auto_devops = @project . auto_devops || ProjectAutoDevops . new
end
2020-04-08 14:13:33 +05:30
def define_deploy_token_variables
@deploy_tokens = @project . deploy_tokens . active
@new_deploy_token = DeployToken . new
end
def define_deploy_keys
@deploy_keys = DeployKeysPresenter . new ( @project , current_user : current_user )
end
2017-08-17 22:00:37 +05:30
end
end
end
2019-12-04 20:38:33 +05:30
Projects :: Settings :: CiCdController . prepend_if_ee ( 'EE::Projects::Settings::CiCdController' )