ci_cd_controller.rb 3.44 KB
Newer Older
1 2
module Projects
  module Settings
3
    class CiCdController < Projects::ApplicationController
4
      before_action :authorize_admin_pipeline!
5
      before_action :define_variables
6

7
      def show
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
      end

      def update
        Projects::UpdateService.new(project, current_user, update_params).tap do |service|
          result = service.execute
          if result[:status] == :success
            flash[:notice] = "Pipelines settings for '#{@project.name}' were successfully updated."

            run_autodevops_pipeline(service)

            redirect_to project_settings_ci_cd_path(@project)
          else
            render 'show'
          end
        end
23 24
      end

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

37 38
      private

39 40 41 42 43
      def update_params
        params.require(:project).permit(
          :runners_token, :builds_enabled, :build_allow_git_fetch,
          :build_timeout_human_readable, :build_coverage_regex, :public_builds,
          :auto_cancel_pending_pipelines, :ci_config_path,
44
          auto_devops_attributes: [:id, :domain, :enabled, :deploy_strategy]
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
        )
      end

      def run_autodevops_pipeline(service)
        return unless service.run_auto_devops_pipeline?

        if @project.empty_repo?
          flash[:warning] = "This repository is currently empty. A new Auto DevOps pipeline will be created after a new file has been pushed to a branch."
          return
        end

        CreatePipelineWorker.perform_async(project.id, current_user.id, project.default_branch, :web, ignore_skip_ci: true, save_on_errors: false)
        flash[:success] = "A new Auto DevOps pipeline has been created, go to <a href=\"#{project_pipelines_path(@project)}\">Pipelines page</a> for details".html_safe
      end

      def define_variables
        define_runners_variables
        define_secret_variables
        define_triggers_variables
        define_badges_variables
        define_auto_devops_variables
      end

68
      def define_runners_variables
69
        @project_runners = @project.runners.ordered
70

71
        @assignable_runners = current_user
72
          .ci_owned_runners
73 74 75 76
          .assignable_for(project)
          .ordered
          .page(params[:page]).per(20)

Mayra Cabrera's avatar
Mayra Cabrera committed
77
        @shared_runners = ::Ci::Runner.shared.active
78

79
        @shared_runners_count = @shared_runners.count(:all)
80

81
        @group_runners = ::Ci::Runner.belonging_to_parent_group_of_project(@project.id)
82 83
      end

84
      def define_secret_variables
Mayra Cabrera's avatar
Mayra Cabrera committed
85
        @variable = ::Ci::Variable.new(project: project)
Shinya Maeda's avatar
Shinya Maeda committed
86 87 88
          .present(current_user: current_user)
        @variables = project.variables.order_key_asc
          .map { |variable| variable.present(current_user: current_user) }
89 90
      end

91
      def define_triggers_variables
92
        @triggers = @project.triggers
Mayra Cabrera's avatar
Mayra Cabrera committed
93
        @trigger = ::Ci::Trigger.new
94 95 96
      end

      def define_badges_variables
97 98
        @ref = params[:ref] || @project.default_branch || 'master'

99
        @badges = [Gitlab::Badge::Pipeline::Status,
100 101 102 103 104 105
                   Gitlab::Badge::Coverage::Report]

        @badges.map! do |badge|
          badge.new(@project, @ref).metadata
        end
      end
106 107 108 109

      def define_auto_devops_variables
        @auto_devops = @project.auto_devops || ProjectAutoDevops.new
      end
110 111 112
    end
  end
end