Read about our upcoming Code of Conduct on this issue

ci_cd_controller.rb 4.4 KB
Newer Older
1
2
# frozen_string_literal: true

3
4
module Projects
  module Settings
5
    class CiCdController < Projects::ApplicationController
6
      before_action :authorize_admin_pipeline!
7
      before_action :define_variables
8

9
      def show
10
11
12
13
14
15
      end

      def update
        Projects::UpdateService.new(project, current_user, update_params).tap do |service|
          result = service.execute
          if result[:status] == :success
16
            flash[:toast] = _("Pipelines settings for '%{project_name}' were successfully updated.") % { project_name: @project.name }
17
18
19
20
21

            run_autodevops_pipeline(service)

            redirect_to project_settings_ci_cd_path(@project)
          else
22
            redirect_to project_settings_ci_cd_path(@project), alert: result[:message]
23
24
          end
        end
25
26
      end

27
28
      def reset_cache
        if ResetProjectCacheService.new(@project, current_user).execute
29
30
31
          respond_to do |format|
            format.json { head :ok }
          end
32
        else
33
34
35
          respond_to do |format|
            format.json { head :bad_request }
          end
36
37
38
        end
      end

39
40
41
      def reset_registration_token
        @project.reset_runners_token!

42
        flash[:toast] = _("New runners registration token has been generated!")
43
44
45
        redirect_to namespace_project_settings_ci_cd_path
      end

46
47
      private

48
      def update_params
49
50
51
52
53
        params.require(:project).permit(*permitted_project_params)
      end

      def permitted_project_params
        [
54
55
56
          :runners_token, :builds_enabled, :build_allow_git_fetch,
          :build_timeout_human_readable, :build_coverage_regex, :public_builds,
          :auto_cancel_pending_pipelines, :ci_config_path,
57
58
          auto_devops_attributes: [:id, :domain, :enabled, :deploy_strategy],
          ci_cd_settings_attributes: [:default_git_depth]
59
60
61
        ].tap do |list|
          list << :max_artifacts_size if can?(current_user, :update_max_artifacts_size, project)
        end
62
63
64
65
66
67
      end

      def run_autodevops_pipeline(service)
        return unless service.run_auto_devops_pipeline?

        if @project.empty_repo?
68
          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.")
69
70
71
72
          return
        end

        CreatePipelineWorker.perform_async(project.id, current_user.id, project.default_branch, :web, ignore_skip_ci: true, save_on_errors: false)
73
74
75

        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 }
76
77
78
79
      end

      def define_variables
        define_runners_variables
80
        define_ci_variables
81
82
83
84
85
        define_triggers_variables
        define_badges_variables
        define_auto_devops_variables
      end

86
      def define_runners_variables
87
        @project_runners = @project.runners.ordered
88

89
        @assignable_runners = current_user
90
          .ci_owned_runners
91
92
93
94
          .assignable_for(project)
          .ordered
          .page(params[:page]).per(20)

95
        @shared_runners = ::Ci::Runner.instance_type.active
96

97
        @shared_runners_count = @shared_runners.count(:all)
98

99
        @group_runners = ::Ci::Runner.belonging_to_parent_group_of_project(@project.id)
100
101
      end

102
      def define_ci_variables
Mayra Cabrera's avatar
Mayra Cabrera committed
103
        @variable = ::Ci::Variable.new(project: project)
Shinya Maeda's avatar
Shinya Maeda committed
104
105
106
          .present(current_user: current_user)
        @variables = project.variables.order_key_asc
          .map { |variable| variable.present(current_user: current_user) }
107
108
      end

109
      def define_triggers_variables
110
        @triggers = @project.triggers
111
          .present(current_user: current_user)
Mayra Cabrera's avatar
Mayra Cabrera committed
112
        @trigger = ::Ci::Trigger.new
113
          .present(current_user: current_user)
114
115
116
      end

      def define_badges_variables
117
118
        @ref = params[:ref] || @project.default_branch || 'master'

119
        @badges = [Gitlab::Badge::Pipeline::Status,
120
121
122
123
124
125
                   Gitlab::Badge::Coverage::Report]

        @badges.map! do |badge|
          badge.new(@project, @ref).metadata
        end
      end
126
127
128
129

      def define_auto_devops_variables
        @auto_devops = @project.auto_devops || ProjectAutoDevops.new
      end
130
131
132
    end
  end
end
133
134

Projects::Settings::CiCdController.prepend_if_ee('EE::Projects::Settings::CiCdController')