diff --git a/app/controllers/ci/templates_controller.rb b/app/controllers/ci/templates_controller.rb index 4423bb402..c77a91445 100644 --- a/app/controllers/ci/templates_controller.rb +++ b/app/controllers/ci/templates_controller.rb @@ -6,15 +6,22 @@ class Ci::TemplatesController < ApplicationController def templates_by_stage stage_type = params[:stage_type] - @templates = Ci::Template.where("stage_type = ?", stage_type) - #根据模板类别分组 - @category_templates = @templates.group_by{ |template| template.category } + if stage_type != Ci::PipelineStage::CUSTOMIZE_STAGE_TYPE + @templates = Ci::Template.where("stage_type = ?", stage_type) + # 根据模板类别分组 + @category_templates = @templates.group_by{ |template| template.category } + else + # 自定义阶段,按阶段分类分类返回模板列表 + @templates = Ci::Template.where("stage_type != ?", Ci::PipelineStage::INIT_STAGE_TYPE) + @category_templates = @templates.group_by{ |template| template.parent_category } + end end def create template = Ci::Template.new(template_name: params[:template_name], stage_type: params[:stage_type], category: params[:category], + parent_category: params[:parent_category], content: params[:content] ) template.save! @@ -23,4 +30,27 @@ class Ci::TemplatesController < ApplicationController render_error(ex.message) end + def update + template = Ci::Template.find(params[:id]) + template.update!(template_name: params[:template_name], + stage_type: params[:stage_type], + category: params[:category], + parent_category: params[:parent_category], + content: params[:content] + ) + render_ok + rescue Exception => ex + render_error(ex.message) + end + + def destroy + template = Ci::Template.find(params[:id]) + if template + template.destroy! + end + render_ok + rescue Exception => ex + render_error(ex.message) + end + end diff --git a/app/models/ci/pipeline.rb b/app/models/ci/pipeline.rb index f69b7e46d..c0c15838f 100644 --- a/app/models/ci/pipeline.rb +++ b/app/models/ci/pipeline.rb @@ -4,10 +4,10 @@ # # id :integer not null, primary key # pipeline_name :string(255) not null -# pipeline_status :integer default("0"), not null # file_name :string(255) not null # created_at :datetime not null # updated_at :datetime not null +# pipeline_status :string(50) default("unknown"), not null # class Ci::Pipeline < Ci::LocalBase diff --git a/app/models/ci/pipeline_stage.rb b/app/models/ci/pipeline_stage.rb index 3cf23ad99..b34c3e71b 100644 --- a/app/models/ci/pipeline_stage.rb +++ b/app/models/ci/pipeline_stage.rb @@ -20,5 +20,6 @@ class Ci::PipelineStage < Ci::LocalBase has_many :pipeline_stage_steps, -> { reorder(show_index: :asc) }, foreign_key: "stage_id", :class_name => 'Ci::PipelineStageStep', dependent: :destroy INIT_STAGES = {init:"初始化", build:"编译构建", deploy:"部署", confirm:"确认"}.freeze - + CUSTOMIZE_STAGE_TYPE = 'customize' + INIT_STAGE_TYPE = 'init' end diff --git a/app/models/ci/pipeline_stage_step.rb b/app/models/ci/pipeline_stage_step.rb index 2d7c8721b..5925d9085 100644 --- a/app/models/ci/pipeline_stage_step.rb +++ b/app/models/ci/pipeline_stage_step.rb @@ -7,7 +7,7 @@ # stage_id :integer not null # template_id :integer # content :text(65535) -# show_index :integer +# show_index :integer default("0"), not null # created_at :datetime not null # updated_at :datetime not null # diff --git a/config/routes.rb b/config/routes.rb index f72d0ead3..9b2f09d24 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -31,7 +31,7 @@ Rails.application.routes.draw do end end - resources :templates, only: [:list,:templates_by_stage,:create] do + resources :templates, only: [:list,:templates_by_stage,:create,:update,:destroy] do collection do get :list get :templates_by_stage diff --git a/db/migrate/20210112053516_add_parent_category_to_ci_templates.rb b/db/migrate/20210112053516_add_parent_category_to_ci_templates.rb new file mode 100644 index 000000000..16bcb4636 --- /dev/null +++ b/db/migrate/20210112053516_add_parent_category_to_ci_templates.rb @@ -0,0 +1,5 @@ +class AddParentCategoryToCiTemplates < ActiveRecord::Migration[5.2] + def change + add_column :ci_templates, :parent_category, :string + end +end